Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android、iOS和Java通用的AES128加密解密示例代码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如android和iOS的打交道。为了让数据交互更安全,我们需要对数据进行加密传输。

这篇文章给大家分享AES的加密和解密、Android和ios通用的AES加密算法、大家可以直接集成到自己的项目、服务器接口如果是用Java写的话、整个框架都完美了、如果是.NET编写的后台接口的话、得改造一下哦

IOS加密

/*加密方法*/
 (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解密

/*解密方法*/
 (NSString *)AES256DecryptWithCiphertext:(NSString *)ciphertexts{
 
 NSData *cipherData = [NSData dataWithBase64EncodedString:ciphertexts];
 // ´key´ should be 32 bytes for AES256,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,ivBuff,/* initialization vector (optional) */
           [cipherData bytes],/* 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加密

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 n@R_301_2486@) {
  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解密

private byte[] decrypt(String cmp,byte[] ciphertext) {
 try {
  Cipher c = Cipher.geTinstance(cmp);
  c.init(Cipher.DECRYPT_MODE,IV);
  return c.doFinal(ciphertext);
 } catch (NoSuchAlgorithmException n@R_301_2486@) {
  Log.e("AESdemo","bad padding exception");
  e.printStackTrace();
 }
 return null;
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位开发者们能有所帮助,如果有疑问大家可以留言交流。

大佬总结

以上是大佬教程为你收集整理的Android、iOS和Java通用的AES128加密解密示例代码全部内容,希望文章能够帮你解决Android、iOS和Java通用的AES128加密解密示例代码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。