Java encryption and decryption notes (ii) symmetric encryption

Source: Internet
Author: User
Tags base64 decrypt

The front is just a code or a summary, below to see the real encryption technology.

Des
 Public classDesutil {Static FinalString algorithm = "DES"; /*** Generate a text Format des Key *@return     * @throwsException*/     Public StaticString GetKey ()throwsexception{keygenerator Generator=keygenerator.getinstance (algorithm); Generator.init (NewSecureRandom ());//Add Salt        returnBase64util.encode (Generator.generatekey (). getencoded ()); }        /*** Convert from Text Format des key to Secretkey object *@paramKey *@return     */     Public StaticSecretkey parsekeyfromstring (String key)throwsexception{Deskeyspec Deskeyspec=NewDeskeyspec (Base64util.decode (key)); Secretkeyfactory Factory=secretkeyfactory.getinstance (algorithm); Secretkey Secretkey=Factory.generatesecret (DESKEYSPEC); returnSecretkey; }        /*** DES Encryption *@paramData *@paramKey *@return     * @throwsException*/     Public StaticString Encrypt (string data,string key)throwsexception{Secretkey Secretkey=parsekeyfromstring (key); Cipher Cipher=cipher.getinstance (algorithm);        Cipher.init (Cipher.encrypt_mode, Secretkey); byte[] bytes = cipher.dofinal (data.getbytes ("UTF-8")); returnBase64util.encode (bytes); }        /*** DES Decryption *@paramData *@paramKey *@return     * @throwsException*/     Public StaticString decrypt (string data,string key)throwsexception{Secretkey Secretkey=parsekeyfromstring (key); Cipher Cipher=cipher.getinstance (algorithm);        Cipher.init (Cipher.decrypt_mode, Secretkey); byte[] bytes =cipher.dofinal (Base64util.decode (data)); return NewString (Bytes, "UTF-8"); }         Public Static voidMain (string[] args)throwsException {String str= "Hello,des"; String Key=GetKey (); System.out.println ("Original:" +str); System.out.println ("Key:" +key); String Encryptedstr=Encrypt (str, key); System.out.println ("After encryption:" +encryptedstr); String Decryptedstr=Decrypt (ENCRYPTEDSTR, key); System.out.println ("After decryption:" +decryptedstr); }    }

Similar to many *KEYSPEC classes in Deskeyspec,java implementation of the KEYSPEC NULL interface, Java only care about whether a class is a key implementation, as to how specifically to implement the specific encryption and decryption algorithm to be concerned about.

Again see Base64 figure, from this can be seen, Base64 actually act as a string and byte array between the converter, the real key data is actually a byte type, but in the reading and transmission process of the string is more convenient, So many of the key information that we can see are actually BASE64 encoded.

Ciper is a redaction tool class, it is a very advanced abstract class, there are many kinds of cipher implementations, can be obtained through the Cipher.getinstance () method.

The GetInstance method receives very many algorithm names:

Test:

Original: Hello,des key: 8epqx0m1i4k= after encryption: bm06wr8oil2sqob8susxra== after decryption: Hello,des 
Aes

AES is an upgraded version of DES, which solves a lot of the disadvantages of DES, and the change of the above des Code to AES is very small:

Static Final String algorithm = "AES";        /**      * Convert from Text Format AES key to Secretkey object      @param  key     @return     */ public    static secretkey parsekeyfromstring (String key)throws  exception{        new  Secretkeyspec (Base64util.decode (key), algorithm);         return secretkey;    }    

Pbe

Pbe and DES, AES are symmetric encryption, the difference is that the key can be managed by the user, that is, the key can not be generated through the keygenerator, can be any character. In order to enhance security, PBE mandatory must have parameters (Java.security.spec.AlgorithmParameterSpec), can be understood as forced salt, otherwise will be an error.

 Public classPbeutil {Static FinalString algorithm = "Pbewithmd5anddes"; Private StaticSecretkey parsekeyfromstring (String key)throwsexception{Pbekeyspec Pbekey=NewPbekeyspec (Key.tochararray ()); Secretkeyfactory Factory=secretkeyfactory.getinstance (algorithm); returnFactory.generatesecret (Pbekey); }        /*** PBE Encryption *@paramData *@paramKey *@return     * @throwsException*/     Public StaticString Encrypt (String data,string key,byte[] salt)throwsexception{Secretkey Secretkey=parsekeyfromstring (key); Cipher Cipher=cipher.getinstance (algorithm); Pbeparameterspec params=NewPbeparameterspec (Salt, 100);        Cipher.init (Cipher.encrypt_mode, secretkey,params); byte[] bytes = cipher.dofinal (data.getbytes ("UTF-8")); returnBase64util.encode (bytes); }        /*** DES Decryption *@paramData *@paramKey *@return     * @throwsException*/     Public StaticString decrypt (String data,string key,byte[] salt)throwsexception{Secretkey Secretkey=parsekeyfromstring (key); Cipher Cipher=cipher.getinstance (algorithm); Pbeparameterspec params=NewPbeparameterspec (Salt, 100);        Cipher.init (Cipher.decrypt_mode, secretkey,params); byte[] bytes =cipher.dofinal (Base64util.decode (data)); return NewString (Bytes, "UTF-8"); }         Public Static voidMain (string[] args)throwsException {String str= "HELLO,PBE"; String Key= "ABC"; byte[] Salt =New byte[8]; NewRandom (). Nextbytes (salt); System.out.println ("Original:" +str); System.out.println ("Key:" +key); String Encryptedstr=Encrypt (str, key,salt); System.out.println ("After encryption:" +encryptedstr); String Decryptedstr=Decrypt (ENCRYPTEDSTR, Key,salt); System.out.println ("After decryption:" +decryptedstr); }    }

Resources:

http://snowolf.iteye.com/blog/380034

http://snowolf.iteye.com/blog/380761

Java encryption and decryption notes (ii) symmetric encryption

Related Article

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.