Solve the problem that the AES encryption algorithm fails to be decrypted in linux.

Source: Internet
Author: User

Solve the problem that the AES encryption algorithm fails to be decrypted in linux.

Some time ago, when the project was to be deployed on linux, Baidu found a solution and shared it here:

Public class RSAEncrypt {// Key private static key KEY; // Key seed private static String KEY_STR = "keyString"; // constant public static final String UTF_8 = "UTF-8 "; public static final String AES = "AES"; // static initialization static {try {// KEY generator KeyGenerator generator = KeyGenerator. getInstance (AES); // The initialization algorithm, which is set to "SHA1PRNG" to prevent random generation of SecureRandom secureRandom = SecureRandom in linux. getInstance ("SHA1PRNG"); secureRandom. setSeed (KEY_STR.getBytes (UTF_8); // 128,192,256 generator. init (128, secureRandom); // generate key = generator. generateKey (); generator = null;} catch (Exception e) {throw new RuntimeException (e) ;}/ *** encrypt the source string, return the BASE64 encoded encrypted String ** @ param source * source String, plaintext * @ return ciphertext String */public static String encode (String source) {try {// obtain byte array byte [] sourceBytes = source according to the encoding format. getBytes (UTF_8); // encryption mode Cipher cipher = Cipher. getInstance (AES); cipher. init (Cipher. ENCRYPT_MODE, key); // The encrypted byte array byte [] encryptSourceBytes = cipher. doFinal (sourceBytes); // Base64 encoder BASE64Encoder base64Encoder = new BASE64Encoder (); return base64Encoder. encode (encryptSourceBytes);} catch (Exception e) {// throw is also a return path throw new RuntimeException (e);}/*** encode () for this tool class () method: decodes/decrypts the encrypted String ** @ param encrypted *, that is, the ciphertext * @ return plaintext String */public static String decode (String encrypted) {// Base64 decoder BASE64Decoder base64Decoder = new BASE64Decoder (); try {// base64 decoding byte [] cryptedBytes = base64Decoder. decodeBuffer (encrypted); // decryption mode Cipher cipher = Cipher. getInstance (AES); cipher. init (Cipher. DECRYPT_MODE, key); // decoded byte array byte [] decryptStrBytes = cipher. doFinal (cryptedBytes); // convert the byte array into a String return new String (decryptStrBytes, UTF_8) using the given encoding format;} catch (Exception e) {// This form is indeed suitable for processing tool class throw new RuntimeException (e );}}

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.