当前位置: 移动技术网 > IT编程>开发语言>Java > Android、iOS和Java通用的AES128加密解密示例代码

Android、iOS和Java通用的AES128加密解密示例代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

好听的手机铃音,北京信息港门户网,吉忠春

前言

移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如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, 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加密

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解密

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;
}

总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网