Java password encryption and decryption

Source: Internet
Author: User
Tags crypt

The following two classes can easily encrypt and decrypt strings:

Encryption: crypthelper. Encrypt (password)

Decryption: cryphelper. decrypt (password)

 

The Code is as follows:

Cryptutils. Java

Package COM. gdie. lab. crypt; import Java. io. ioexception; import javax. crypto. cipher; import javax. crypto. keygenerator; import javax. crypto. secretkey; import com.sun.org. apache. xerces. internal. impl. DV. util. base64; public class cryptutils {Private Static string algorithm = "des"; Private Static byte [] default_key = new byte [] {-53,122,-42,-88,-110, -123,-60,-74}; Private Static string value_encoding = "UTF-8 "; /*** generate the key ** @ return byte [] returns the generated key * @ throws exception * throwing an exception. */public static byte [] getsecretkey () throws exception {keygenerator keygen = keygenerator. getinstance (algorithm); secretkey secret ey = keygen. generatekey (); // If (Debug) system. out. println ("generate key:" + byte2hex (Cipher ey. getencoded // (); return response ey. getencoded ();} /*** encrypt the specified data based on the provided key ** @ Param input * data to be encrypted * @ Param key * Key * @ return byte [] encrypted data * @ throws exception */public static byte [] encryptdata (byte [] input, byte [] Key) throws exception {secretkey secret ey = new javax. crypto. spec. secretkeyspec (Key, algorithm); // If (Debug) // {// system. out. println ("binary string before encryption:" + byte2hex (input); // system. out. println ("encrypted string:" + new string (input); //} cipher C1 = cipher. getinstance (algorithm); c1.init (cipher. encrypt_mode, encryption ey); byte [] cipherbyte = c1.dofinal (input); // If (Debug) system. out. println ("encrypted binary string:" + byte2hex (cipherbyte); Return cipherbyte;} public static byte [] encryptdata (byte [] input) throws exception {return encryptdata (input, default_key );} /*** decrypt the specified encrypted data using the specified key. ** @ Param input * data to be decrypted * @ Param key * Key * @ return byte [] data after * @ throws exception */public static byte [] decryptdata (byte [] input, byte [] Key) throws exception {secretkey secret ey = new javax. crypto. spec. secretkeyspec (Key, algorithm); // If (Debug) system. out. println ("decrypted information:" + byte2hex (input); cipher C1 = cipher. getinstance (algorithm); c1.init (cipher. decrypt_mode, encryption ey); byte [] clearbyte = c1.dofinal (input); // If (Debug) // {// system. out. println ("decrypted binary string:" + byte2hex (clearbyte); // system. out. println ("decrypted string:" + (new string (clearbyte); //} return clearbyte;} public static byte [] decryptdata (byte [] input) throws exception {return decryptdata (input, default_key );} /*** convert bytecode to a hexadecimal string ** @ Param byte [] B enter the bytecode to be converted * @ return string returns the converted hexadecimal string */ public static string byte2hex (byte [] bytes) {stringbuilder HS = new stringbuilder (); For (byte B: bytes) HS. append (string. format ("% 1 $ 02x", B); Return HS. tostring ();} public static byte [] hex2byte (string content) {int L = content. length ()> 1; byte [] result = new byte [l]; for (INT I = 0; I <L; I ++) {Int J = I <1; string S = content. substring (J, J + 2); Result [I] = integer. valueof (S, 16 ). bytevalue ();} return result;}/*** converts a byte array to a base64 encoded string * @ Param buffer * @ return */public static string bytestobase64 (byte [] buffer) {// base64encoder en = new base64encoder (); Return base64.encode (buffer); // return encoder. encode (buffer );} /*** decodes a base64 encoded string into a byte array * @ Param value * @ return * @ throws ioexception */public static byte [] base64tobytes (string value) throws ioexception {// return base64.decodetobytearray (value); // system. out. println (decoder. decodebuffer (value); // return decoder. decodebuffer (value); Return base64.decode (value );} /*** encrypted string * @ Param value * @ return encrypted base64 string */public static string encryptstring (string value) {return encryptstring (value, default_key );} /*** encrypt String Based on the given key * @ Param value the string to be encrypted * @ Param key the key that exists in the base64 format * @ return the encrypted base64 string * @ throws ioexception */public static string encryptstring (string value, string key) throws ioexception {return encryptstring (value, base64tobytes (key ));} /*** encrypt the string based on the given key * @ Param value the string to be encrypted * @ Param key a key in the byte array form * @ return encrypted base64 string */public static string encryptstring (string value, byte [] Key) {try {byte [] DATA = value. getbytes (value_encoding); Data = cryptutils. encryptdata (data, key); Return bytestobase64 (data);} catch (exception e) {// todo auto-generated catch blocke. printstacktrace (); return NULL ;}/ *** decrypt string * @ Param value base64 ciphertext * @ return plaintext */public static string decryptstring (string value) {return decryptstring (value, default_key );} /*** decrypt the string * @ Param value ciphertext in base64 form * @ Param key base64 form key * @ return plaintext * @ throws ioexception */public static string decryptstring (string value, string key) throws ioexception {string S = decryptstring (value, base64tobytes (key); Return s ;} /*** decrypt string * @ Param value ciphertext in base64 form * @ Param key in byte data form * @ return plaintext */public static string decryptstring (string value, byte [] Key) {try {byte [] DATA = base64tobytes (value); Data = cryptutils. decryptdata (data, key); return new string (data, value_encoding);} catch (exception e) {e. printstacktrace (); return NULL ;}}}

 

Crypthelper. Java

Package COM. gdie. lab. crypt; import javax. crypto. cipher; import javax. crypto. secretkey; import javax. crypto. secretkeyfactory; import javax. crypto. spec. deskeyspec; import javax. crypto. spec. ivparameterspec; import Org. springframework. util. digestutils; public class crypthelper {Private Static string crypt_key = "zhongqian"; // encrypt Private Static cipher ECIP; // decrypt Private Static cipher dcip; static {try {string key = digestutils. md5digestashex (crypt_key.getbytes ()). touppercase (); Key = key. substring (0, 8); byte [] bytes = key. getbytes (); deskeyspec Ks = new deskeyspec (bytes); secretkeyfactory SKF = secretkeyfactory. getinstance ("des"); secretkey Sk = SKF. generatesecret (KS); ivparameterspec iv2 = new ivparameterspec (bytes); ECIP = cipher. getinstance ("des/CBC/pkcs5padding"); ECIP. init (cipher. encrypt_mode, SK, iv2); dcip = cipher. getinstance ("des/CBC/pkcs5padding"); dcip. init (cipher. decrypt_mode, SK, iv2);} catch (exception ex) {ex. printstacktrace () ;}} public static string encrypt (string content) throws exception {byte [] bytes = ECIP. dofinal (content. getbytes ("ASCII"); Return cryptutils. byte2hex (bytes);} public static string decrypt (string content) throws exception {byte [] bytes = cryptutils. hex2byte (content); bytes = dcip. dofinal (bytes); return new string (bytes, "ASCII") ;}// testpublic static void main (string [] ARGs) throws exception {string Password = "Gly "; string en = encrypt (password); system. out. println (en); system. out. println (decrypt (En ));}}

 

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.