Applications with high security requirements must avoid unsafe or weak cryptographic algorithms, and the computational power of modern computers allows attackers to break down weak algorithms by brute force. For example, the Data Encryption Standard Algorithm des is extremely restless, using a computer similar to the EFF (Electronic Frontier foundaton) deep crack to be able to violently hack messages encrypted by Des in one day.
[Code Demo sample that does not meet security requirements]
The following code encrypts a string using a weak des algorithm:
Secretkey key = Keygenerator.getinstance ("DES"). GenerateKey (); Cipher Cipher = cipher.getinstance ("DES"); Cipher.init (Cipher.encrypt_mode, key);//encode bytes as UTF8; strtobeencrypted contains//The input string is encryptedbyte[] encoded = strtobeencrypted.getbytes ("UTF-8");// Perform encryptionbyte[] encrypted = cipher.dofinal (encoded);
[Security-compliant solutions]
This scenario uses a more secure AES encryption algorithm to encrypt the string
Cipher Cipher = cipher.getinstance ("AES"); Keygenerator KGen = keygenerator.getinstance ("AES"); Kgen.init (+);//192 and-bits may unavailablesecretkey skey = Kgen.generatekey (); byte[] raw = skey.getencoded (); Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES") Cipher.init (Cipher.encrypt_mode, skeyspec);//Encode bytes as UTF8; strtobeencrypted contains the//input string that was to be encryptedbyte[] encoded = strtobeencrpyted.getbytes ("UTF-8");// Perform encryptionbyte[] encrypted = cipher.dofinal (encoded);
[Availability]
The use of mathematical and computational insecure cryptographic algorithms can lead to the disclosure of sensitive information. Weak encryption algorithms can be enabled in Java SE 7, and they can be used in scenarios where encryption agrees to be cracked. For example, the ROT13 encryption algorithm is widely used in electronic bulletin boards and Web pages, where the purpose of encryption is to protect people from the interference of information, rather than protect the information is not known to people.
--Welcome reprint, please indicate the source http://blog.csdn.net/asce1885 , do not use for commercial purposes without my permission, thank you--
"Java Coding Guidelines" の#12 do not use insecure or weak encryption algorithms