Android, iOS, and Java Common AES128 encryption and decryption sample code _java

Source: Internet
Author: User

Objective

The moving end is getting more and more hot, and we're always going to have to deal with the mobile end of the development process, like Android and iOS. In order to make data interaction more secure, we need to encrypt the data for transmission.

This article to share AES encryption and decryption, Android and iOS general AES encryption algorithm, you can directly integrate into their own projects, server interface if it is written in Java, the entire framework is perfect, if it is. NET written in the background interface, you have to transform OH

iOS encryption

/* Encryption Method * * (NSString *) Aes256encryptwithplaintext: (NSString *) Plain {NSData *plaintext = [Plain Datausingencoding:nsutf8
 Stringencoding]; ´key´should is bytes for AES256, would 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 AESKeyForP Assword:password] bytes], kCCKeySizeAES256, ivbuff/* initialization vector (optional) * * *, [Plaintex
 T 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 dataWithBase64
 ENCODEDSTRING:CIPHERTEXTS]; ´key´should is bytes for AES256, would 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 Aeskeyfor Password:password] bytes], kCCKeySizeAES256, ivbuff, * initialization vector (optional) * * [cipher
 
 Data 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.get Instance (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 = Cip Her.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;
}

Summarize

The above is the entire content of this article, I hope this article content for the developers can help, if there is doubt you can message exchange.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.