AES encrypted CBC mode Ios-java interoperability sharing

Source: Internet
Author: User
Tags decrypt null null



AES encryption mode and fill mode


Algorithm/mode/padding 16 bytes After encrypted data length less than 16 bytes after encrypted length
Aes/cbc/nopadding 16 Not supported
Aes/cbc/pkcs5padding 32 16
Aes/cbc/iso10126padding 32 16
Aes/cfb/nopadding 16 Raw data length
Aes/cfb/pkcs5padding 32 16
Aes/cfb/iso10126padding 32 16
Aes/ecb/nopadding 16 Not supported
Aes/ecb/pkcs5padding 32 16
Aes/ecb/iso10126padding 32 16
Aes/ofb/nopadding 16 Raw data length
Aes/ofb/pkcs5padding 32 16
Aes/ofb/iso10126padding 32 16
Aes/pcbc/nopadding 16 Not supported
Aes/pcbc/pkcs5padding 32 16
Aes/pcbc/iso10126padding 32 16





AES--Java implementation


Package com.csnt.csb2b.test;


Import Javax.crypto.Cipher;
Import Javax.crypto.spec.IvParameterSpec;
Import Javax.crypto.spec.SecretKeySpec;

Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;

/*******************************************************************************
* AES Plus decryption algorithm
*
* @author arix04
*
*/

public class Aesutil {

Encryption
public static string Encrypt (String sSrc, String SKey) throws Exception {
if (SKey = = null) {
System.out.print ("key is null null");
return null;
}
Determine if key is 16-bit
if (Skey.length ()! = 16) {
System.out.print ("Key length is not 16 bits");
return null;
}
Byte[] raw = skey.getbytes ();
Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES");
Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");//"algorithm/Mode/complement Method"
Ivparameterspec IV = new Ivparameterspec ("0102030405060708". GetBytes ());//using CBC mode, a vector IV is required to increase the strength of the encryption algorithm
Cipher.init (Cipher.encrypt_mode, Skeyspec, iv);
Byte[] encrypted = Cipher.dofinal (Ssrc.getbytes ());

return new Base64encoder (). Encode (encrypted);//The use of BASE64 to do transcoding function, at the same time can play 2 times the role of encryption.
}

Decrypt
public static string Decrypt (String sSrc, String SKey) throws Exception {
try {
Determine if key is correct
if (SKey = = null) {
System.out.print ("key is null null");
return null;
}
Determine if key is 16-bit
if (Skey.length ()! = 16) {
System.out.print ("Key length is not 16 bits");
return null;
}
Byte[] Raw = skey.getbytes ("UTF-8");
Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES");
Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");
Ivparameterspec IV = new Ivparameterspec ("0102030405060708"
. GetBytes ());
Cipher.init (Cipher.decrypt_mode, Skeyspec, iv);
byte[] encrypted1 = new Base64decoder (). Decodebuffer (SSRC);//First use Base64 to decrypt
try {
byte[] Original = cipher.dofinal (encrypted1);
String originalstring = new String (original);
return originalstring;
} catch (Exception e) {
System.out.println (E.tostring ());
return null;
}
} catch (Exception ex) {
System.out.println (Ex.tostring ());
return null;
}
}

public static void Main (string[] args) throws Exception {
/*
* Encryption key can be composed of 26 letters and numbers, it is best not to use reserved characters, although not wrong, as to how the ruling, the individual depends on the situation
* AES-128-CBC encryption mode is used here and key needs to be 16 bits.
*/
String ckey = "1234567890abcdef";
Strings that need to be encrypted
String  = "user=15063142270,pwd=110120";
System.out.println ;
Encryption
Long Lstart = System.currenttimemillis ();
String enstring = Aesutil.encrypt (Ckey);
System.out.println ("Encrypted string is:" + enstring);

Long lusetime = System.currenttimemillis ()-Lstart;
SYSTEM.OUT.PRINTLN ("Encryption Time:" + Lusetime + "milliseconds");
Decrypt
Lstart = System.currenttimemillis ();
String destring = Aesutil.decrypt ("tojsolgvnmr0gv9wwt1buctfmdkhkuq16kqiylkrmq4=", Ckey);
System.out.println ("Decrypted string is:" + destring);
Lusetime = System.currenttimemillis ()-Lstart;
System.out.println ("Decryption time:" + Lusetime + "milliseconds");
}
}








AES IOS Implementation






Concrete implementation see Baidu Cloud Disk Share link:



Http://pan.baidu.com/s/1ntidzAT



AES encrypted CBC mode Ios-java interoperability sharing


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.