17/12/2013

[Java] AES encryption and decryption

To perform AES encryption and decryption, without implementing your own algorithms - since it is usually unsafe - you may use the standard Java crypto libraries. A more complete crypto reference is found in the JCA reference guide.


Basic imports:

 import javax.crypto.Cipher;  
 import javax.crypto.SecretKey;  
 import javax.crypto.spec.IvParameterSpec;  
 import javax.crypto.spec.SecretKeySpec;  
 import javax.xml.bind.DatatypeConverter;  

Auxiliary functions such as string2Hex can be found here.

Sample CBC encryption with PKCS5 padding:

 public static void testCBCEncrypt() throws Exception{  
      String hexKey = "YOUR_KEY";  
      String hexIv = "YOUR_IV";  
      String text="Some sample text";  
      String hexEncodedText=string2Hex(text);//convert our text in HEX  
        
      //define the AES secret key  
      SecretKey key = new SecretKeySpec(DatatypeConverter.parseHexBinary(hexKey), "AES");  
        
      //define the AES IV  
      IvParameterSpec ivspec = new IvParameterSpec(DatatypeConverter.parseHexBinary(hexIv));  
   
      //define the cipher mode we're using  
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");  
      cipher.init(Cipher.ENCRYPT_MODE, key, ivspec);  
   
      //apply the cipher to our text  
      byte[] result = cipher.doFinal(DatatypeConverter.parseHexBinary(hexEncodedText));  
   
      String out = DatatypeConverter.printHexBinary(result);  
      String hexOut=string2Hex(out);  
        
      //print the encryption result  
      System.out.println(hex2String(hexOut));  
 }  

And its decryption:

 public static void testCBCDecrypt() throws Exception{  
      String hexKey = "YOUR_KEY";  
      String hexIv = "YOUR_IV";  
      String hexEncodedText = "THE_ENCODED_TEXT";  
   
      //create AES key  
      SecretKey key = new SecretKeySpec(DatatypeConverter.parseHexBinary(hexKey), "AES");  
        
      //create AES IV  
      IvParameterSpec ivspec = new IvParameterSpec(DatatypeConverter.parseHexBinary(hexIv));  
   
      //define the cipher mode  
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");  
      cipher.init(Cipher.DECRYPT_MODE, key, ivspec);  
   
      //decrypt  
      byte[] result = cipher.doFinal(DatatypeConverter.parseHexBinary(hexEncodedText));  
   
      String out = DatatypeConverter.printHexBinary(result);  
      //print out the decrypted text  
      System.out.println(hex2String(out));  
 }  

CTR encryption without padding:

 //AES, CTR, no padding  
 public static void testCTREncrypt() throws Exception{  
      String hexKey = "YOUR_KEY";  
      String hexIv = "YOUR_IV";  
      String text = "Sample text";  
      String hexEncodedText = string2Hex(text);//convert it to HEX  
   
      //create AES key  
      SecretKey key = new SecretKeySpec(DatatypeConverter.parseHexBinary(hexKey), "AES");  
        
      //create AES IV  
      IvParameterSpec ivspec = new IvParameterSpec(DatatypeConverter.parseHexBinary(hexIv));  
   
      //define cipher mode  
      Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");  
      cipher.init(Cipher.ENCRYPT_MODE, key, ivspec);  
   
      //encode  
      byte[] result = cipher.doFinal(DatatypeConverter.parseHexBinary(hexEncodedText));  
   
      String out = DatatypeConverter.printHexBinary(result);  
      String hexOut=string2Hex(out);  
      //print out the encrypted text  
      System.out.println(hex2String(hexOut));  
 }  

And its decryption:

 public static void testCTRDecrypt() throws Exception{  
      String hexKey = "YOUR_KEY";  
      String hexIv = "YOUR_IV";  
      String hexEncodedText = "THE_ENCODED_TEXT";  
   
      //define AES key  
      SecretKey key = new SecretKeySpec(DatatypeConverter.parseHexBinary(hexKey), "AES");  
        
      //define AES IV  
      IvParameterSpec ivspec = new IvParameterSpec(DatatypeConverter.parseHexBinary(hexIv));  
   
      //define cipher mode  
      Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");  
      cipher.init(Cipher.DECRYPT_MODE, key, ivspec);  
   
      //decode  
      byte[] result = cipher.doFinal(DatatypeConverter.parseHexBinary(hexEncodedText));  
   
      String out = DatatypeConverter.printHexBinary(result);  
      //print out the decrypted text  
      System.out.println(hex2String(out));  
 }  

No comments:

Post a Comment

With great power comes great responsibility