Go back end +ios end +android end of AES256 encryption Scheme (Android side)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The mobile-to-server communication requires AES 256 encryption.

The implementation code for the Androd client is:


Import Android.util.base64;import Java.io.unsupportedencodingexception;import Java.security.key;import Java.security.messagedigest;import Java.security.nosuchalgorithmexception;import Javax.crypto.Cipher;import Javax.crypto.spec.secretkeyspec;public class Aesencrypt {private static final String Key_algorithm = "AES"; private static final String Cipher_algorithm = "aes/ecb/pkcs7padding"; public static string Encrypt (string key, String data) {try {final key KeySpec = CreateKey (key); Final Cipher Cipher = cipher.getinstance (Cipher_algorithm, "BC"); Cipher.init (Cipher.encrypt_mode, KeySpec); Final byte[] encoded = cipher.dofinal (Data.getbytes ("UTF-8")); return base64.encodetostring (encoded, base64.default); } catch (Exception e) {e.printstacktrace (); } return null; } public static string decrypt (string key, String data) {try {final key keySpec = CreateKey (key); Final Cipher Cipher = cipher.getinstance (cipher_algorithm); Cipher.init (Cipher.decrypt_mode, KeySpec); Final byte[] bytes = Base64.decode (data.getbytes ("UTF-8"), Base64.default); return new String (cipher.dofinal (bytes)); } catch (Exception e) {e.printstacktrace (); } return null; } private static Key CreateKey (String Key) throws Unsupportedencodingexception, NoSuchAlgorithmException {final MessageDigest digest = messagedigest.getinstance ("SHA-256"); Final byte[] Keybyte = Digest.digest (Key.getbytes ("UTF-8")); return new Secretkeyspec (Keybyte, key_algorithm); }}

Encrypted results are converted to strings by Base64.default

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.