Friday, February 7, 2014

  1. // 'key' should be 32 bytes for AES256, will be null-padded otherwise
  2.     char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
  3.     bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
  4.        
  5. CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
  6.                                      keyPtr, kCCKeySizeAES256,
  7.                                      NULL /* initialization vector (optional) */,
  8.                                      [self bytes], dataLength, /* input */
  9.                                      buffer, bufferSize, /* output */
  10.                                      &numBytesEncrypted);
  11.        
  12. private static final String AES_SECRET = "PreDefinedKey";
  13.  
  14. /**
  15.  * Method for AES encryption
  16.  * @param raw
  17.  * @param plain
  18.  * @return
  19.  * @throws Exception
  20.  */
  21. private static byte[] encrypt(byte[] raw, byte[] plain) throws Exception {
  22.     SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES/ECB/PKCS7Padding");
  23.     Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
  24.     cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  25.     byte[] encrypted = cipher.doFinal(plain);
  26.     return encrypted;
  27. }
  28.  
  29.  
  30.  
  31. /**
  32.  * AES decryption
  33.  * @param encryptMsg
  34.  * @return
  35.  * @throws Exception
  36.  */
  37. public static String AESDecrypt(String encryptMsg)
  38.         throws Exception {          
  39.     byte[] rawKey = getRawKey(AES_SECRET.getBytes());
  40.     //byte[] enc = toByte(encryptMsg);
  41.     byte[] enc = Base64.decode(encryptMsg, 0);
  42.     byte[] result = decrypt(rawKey, enc);
  43.     return new String(result);
  44.  
  45. }
  46.  
  47. /**
  48.  * Method for AES decryption
  49.  * @param raw
  50.  * @param encrypted
  51.  * @return
  52.  * @throws Exception
  53.  */
  54. private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
  55.     SecretKeySpec keySpec = new SecretKeySpec(raw, "AES/ECB/PKCS7Padding");
  56.     Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
  57.     cipher.init(Cipher.DECRYPT_MODE, keySpec);
  58.     byte[] decrypted = cipher.doFinal(encrypted);
  59.     return decrypted;
  60.  
  61. }
  62.  
  63. public static byte[] getRawKey(byte[] seed) throws Exception {
  64.  
  65.     KeyGenerator kgen = KeyGenerator.getInstance("AES");
  66.     SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  67.     sr.setSeed(seed);
  68.     //Init for 256bit AES key
  69.     kgen.init(256);
  70.     SecretKey secret = kgen.generateKey();
  71.     //Get secret raw key
  72.     byte[] raw = secret.getEncoded();
  73.  
  74.     return seed;
  75.  
  76. }
  77.        
  78. // zeros by default
  79. byte[] rawKey = new byte[32];
  80. // if you don't specify the encoding you might get weird results
  81. byte[] keyBytes = AES_SECRET.getBytes("ASCII");
  82. System.arraycopy(keyBytes, 0, rawKey, 0, keyBytes.length);
  83. SecretKey key = new SecretKeySpec(rawKey, "AES");
  84. Cipher cipher = ...
  85. // rest of your decryption code

No comments: