Android iOS and Java common AES128 encryption and decryption sample code

  • 2020-05-16 06:59:20
  • OfStack

preface

Mobile devices are becoming more and more popular, and we are always dealing with mobile devices in the development process, such as android and iOS. To make the data interaction more secure, we need to encrypt the data.

This article to share with you AES encryption and decryption, Android and ios common AES encryption algorithm, you can be directly integrated into their own projects, server interface if written in Java, the entire framework is perfect, if it is.NET written background interface, have to change 1 oh

IOS encryption


/* Encryption methods */
 (NSString *)AES256EncryptWithPlainText:(NSString *)plain {
 NSData *plainText = [plain dataUsingEncoding:NSUTF8StringEncoding];
 // ´key´ should be 32 bytes for AES256, will be null-padded otherwise
 char keyPtr[kCCKeySizeAES256 1]; // room for terminator (unused)
 bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
  
 NSUInteger dataLength = [plainText length];

 size_t bufferSize = dataLength kCCBlockSizeAES128;
 void *buffer = malloc(bufferSize);
 bzero(buffer, sizeof(buffer));
 
 size_t numBytesEncrypted = 0;
 
 CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,kCCOptionPKCS7Padding,
           [[NSData AESKeyForPassword:PASSWORD] bytes], kCCKeySizeAES256,
           ivBuff /* initialization vector (optional) */,
           [plainText bytes], dataLength, /* input */
           buffer, bufferSize, /* output */
           &numBytesEncrypted);
 if (cryptStatus == kCCSuccess) {
  NSData *encryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
  return [encryptData base64Encoding];
 }
 
 free(buffer); //free the buffer;
 return nil;
}

IOS decryption


/* Decryption method */
 (NSString *)AES256DecryptWithCiphertext:(NSString *)ciphertexts{
 
 NSData *cipherData = [NSData dataWithBase64EncodedString:ciphertexts];
 // ´key´ should be 32 bytes for AES256, will be null-padded otherwise
 char keyPtr[kCCKeySizeAES256 1]; // room for terminator (unused)
 bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
  
 NSUInteger dataLength = [cipherData length];
 
 size_t bufferSize = dataLength kCCBlockSizeAES128;
 void *buffer = malloc(bufferSize);
  
 size_t numBytesDecrypted = 0;
 CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
           [[NSData AESKeyForPassword:PASSWORD] bytes], kCCKeySizeAES256, 
           ivBuff ,/* initialization vector (optional) */
           [cipherData bytes], dataLength, /* input */
           buffer, bufferSize, /* output */
           &numBytesDecrypted);
 
 if (cryptStatus == kCCSuccess) {
  NSData *encryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
  return [[[NSString alloc] initWithData:encryptData encoding:NSUTF8StringEncoding] init];
 }
 
 free(buffer); //free the buffer;
 return nil;
}

Android encryption


private byte[] encrypt(String cmp, SecretKey sk, IvParameterSpec IV,
  byte[] msg) {
 try {
  Cipher c = Cipher.getInstance(cmp);
  c.init(Cipher.ENCRYPT_MODE, sk, IV);
  return c.doFinal(msg);
 } catch (NoSuchAlgorithmException nsae) {
  Log.e("AESdemo", "no cipher getinstance support for " cmp);
 } catch (NoSuchPaddingException nspe) {
  Log.e("AESdemo", "no cipher getinstance support for padding " cmp);
 } catch (InvalidKeyException e) {
  Log.e("AESdemo", "invalid key exception");
 } catch (InvalidAlgorithmParameterException e) {
  Log.e("AESdemo", "invalid algorithm parameter exception");
 } catch (IllegalBlockSizeException e) {
  Log.e("AESdemo", "illegal block size exception");
 } catch (BadPaddingException e) {
  Log.e("AESdemo", "bad padding exception");
 }
 return null;
}

Android decryption


private byte[] decrypt(String cmp, SecretKey sk, IvParameterSpec IV,
   byte[] ciphertext) {
 try {
  Cipher c = Cipher.getInstance(cmp);
  c.init(Cipher.DECRYPT_MODE, sk, IV);
  return c.doFinal(ciphertext);
 } catch (NoSuchAlgorithmException nsae) {
  Log.e("AESdemo", "no cipher getinstance support for " cmp);
 } catch (NoSuchPaddingException nspe) {
  Log.e("AESdemo", "no cipher getinstance support for padding " cmp);
 } catch (InvalidKeyException e) {
  Log.e("AESdemo", "invalid key exception");
 } catch (InvalidAlgorithmParameterException e) {
  Log.e("AESdemo", "invalid algorithm parameter exception");
 } catch (IllegalBlockSizeException e) {
  Log.e("AESdemo", "illegal block size exception");
 } catch (BadPaddingException e) {
  Log.e("AESdemo", "bad padding exception");
  e.printStackTrace();
 }
 return null;
}

conclusion

The above is the entire content of this article, I hope the content of this article can be helpful to you developers, if you have any questions, you can leave a message to communicate.


Related articles: