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 );}}