Different solutions for AES encryption and operating results under the Java platform under the Android platform

Source: Internet
Author: User
Tags base64 decrypt
Enable Android and Java to decrypt each other

Perfect support for Chinese

Cross-platform This implementation

It's still a principle.

Do not use default implementations for parameters

Otherwise difficult to exchange

The core functions are as follows, both Android and Java (datapassword length is 16)

[Java]View plain copy public static final String Vipara = "0102030405060708"; public static final String BM = "GBK";
[Java]  View plain copy public static string encrypt (string datapassword, string  cleartext)            throws Exception {       ivparameterspec zeroiv = new ivparameterspec (VIPARA.getBytes ());        secretkeyspec key = new secretkeyspec ( Datapassword.getbytes (),  "AES");       Cipher cipher =  Cipher.getinstance ("aes/cbc/pkcs5padding");       cipher.init (Cipher.ENCRYPT_MODE ,  KEY, ZEROIV);       byte[] encryptedData =  Cipher.dofinal (Cleartext.getbytes (BM));          return  Base64.encode (EncryptedData);  }      Public static string decrypt ( string datapassword, string encrypted)            throws Exception {       byte[] bytemi = base64.decode (encrypted);        ivparameterspec zeroiv = new ivparameterspec (Vipara.getbytes ());        secretkeyspec key = new secretkeyspec (DataPassword.getBytes (),  "AES ");       cipher cipher = cipher.getinstance (" AES/CBC/PKCS5Padding ");       cipher.init (CIPHER.DECRYPT_MODE, KEY, ZEROIV);       byte[] decrypteddata = cipher.dofinal (byteMi);          return new string (DECRYPTEDDATA,BM);  }  


Base64 from the Internet to find the tool class

[Java] view plain copy package Com.bao;   Import Java.io.ByteArrayOutputStream;   Import java.io.IOException;            Import Java.io.OutputStream; public class Base64 {private static final char[] Legalchars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz       0123456789+/". ToCharArray (); /** * data[] Code * @param data * @return/public static String encode (byte[] data) {

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.