Using the AES algorithm, you will find that the following code produces certain results each time in Windows, but Linux is different, causing the decryption to be incorrect.
Public Staticstring Encrypt (string content, string password) {Try{keygenerator KGen= Keygenerator.getinstance ("AES"); Kgen.init (128,New SecureRandom (password.getbytes ())); Secretkey Secretkey=Kgen.generatekey (); byte[] Encodeformat =secretkey.getencoded (); Secretkeyspec Key=NewSecretkeyspec (Encodeformat, "AES"); Cipher Cipher= Cipher.getinstance ("AES");//Create a password device byte[] bytecontent = Content.getbytes ("Utf-8"); Cipher.init (Cipher.encrypt_mode, key);//Initialize byte[] result =cipher.dofinal (bytecontent); SecureRandom Random= Securerandom.getinstance ("Sha1prng"); Random.setseed (Password.getbytes ()); Kgen.init (128, Random); returnPARSEBYTE2HEXSTR (result);//Encrypt}Catch(Exception e) {System.out.print (e); } return NULL;}
The reason is that the red part of the Secureraom generation, Linux under the default algorithm is "nativeprng", and Windows default is "Sha1prng" (sun-provided algorithm)
For both of these algorithms
same point: 1. All pseudo-random algorithms, 2. The default is blocking different points: 1. The seed used by SHA1PRNG is specified at system startup, and NATIVEPRNG is randomly obtained in the kernel (which is why each result is different under Linux) 2. It's because the nativeprng cost a lot more for each random fetch.
While Linux considers the best random algorithm to be nativeprng (security), the use of Radom that changes every time is not decrypted correctly, so it is not suitable for this scenario.
Solutions
securerandom.getinstance ("sha1prng" ); Securerandom.setseed (Password.getbytes ()); Kgen.init (securerandom);
In addition, both of these algorithms are blocking algorithms that read/var/random and, if they want to be non-blocking, start with the following parameters
Djava.security=file:/dev/urandom
Reference:
Http://www.cjsdn.net/Doc/JDK50/java/security/SecureRandom.html
Https://docs.oracle.com/javase/7/docs/api/java/security/SecureRandom.html
Http://calvin1978.blogcn.com/articles/securerandom.html
Https://stackoverflow.com/questions/27622625/securerandom-with-nativeprng-vs-sha1prng
About SecureRandom in Java generates different results each time in Linux