About SecureRandom in Java generates different results each time in Linux

Source: Internet
Author: User

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

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.