Java Plus decryption

Source: Internet
Author: User

Several Java encryption and decryption techniques are described below:

1:base64 decryption: Use of dependencies

<dependency>    <groupId>commons-codec</groupId>        <artifactid>commons-codec</ artifactid>        <version>1.10</version>    </dependency>

Encryption Code:

 Public String Base64en () {        new  Base64 ();         byte [] encode = Base64.encode (name.getbytes ());        System.out.println (new  String (encode));         return New String (encode);    }

Decryption Code:

 Public string Base64de (String encodestr) {                new  Base64 ();         byte [] Decodestr = base64.decodebase64 (encodestr);        System.out.println (new  String (DECODESTR));         return New String (DECODESTR);    }

2:MD5 Digest algorithm: The main application scenario of MD5 Digest algorithm: the password information of the user stored in the database needs to be encrypted by MD5, and then compared with the user's MD5 encrypted password, if it is consistent

Consistent business processing, inconsistent business logic processing if inconsistent

The encryption code is as follows:

 Public voidMD5DE ()throwsnosuchalgorithmexception{messagedigest MD5= Messagedigest.getinstance ("MD5"); BASE64 Base64=NewBase64 (); byte[] name =md5.digest (Name.getbytes ()); byte[] City =md5.digest (City.getbytes ()); String str1=base64.encodebase64string (name); String str2=base64.encodebase64string (city); if(Str1.equals (str2)) {System.out.println ("Two results Same"); }Else{System.out.println ("Two results are different"); }    }

3:aes Plus decryption Technology:

Advanced Encryption Standard (English:encryptionStandard, abbreviation:AES), is a block encryption standards; Des is an upgraded version, Des uses a 56-bit key, easy to hack ;

AES has 128 192 and 256-bit keys, and the 128-bit packet encryption and decryption data, theoretically can not be cracked;

Cryptographic algorithm Code:

Private Static FinalString Name= "This is Beppe Zhang"; Content that needs to be encryptedPrivate Static FinalString salt= "tttaaeuhruiyr9ur33rb3g4###"; The salt caller only knows the salt to decrypt it. Public byte[] Aesencode ()throwsnosuchalgorithmexception, Nosuchpaddingexception, InvalidKeyException, Illegalblocksizeexception, Badpaddingexception, unsupportedencodingexception{//get the AES keyKeygenerator generator = keygenerator.getinstance ("AES"); Generator.init (128,NewSecureRandom (Salt.getbytes ()));//Add SaltSecretkey key =Generator.generatekey (); Secretkeyspec Spec=NewSecretkeyspec (key.getencoded (), "AES"); //Create a password deviceCipher Cipher = cipher.getinstance ("AES");            Cipher.init (Cipher.encrypt_mode, spec); Fill method is the encryption Fill methodbyte[] dofinal = Cipher.dofinal (Name.getbytes ("Utf-8")); returndofinal; }

The code for the decryption algorithm is as follows:

Public String Aesdecode (byte[] encodestr) throws NoSuchAlgorithmException, Nosuchpaddingexception, InvalidKeyException, Illegalblocksizeexception, Badpaddingexception, unsupportedencodingexception{// Get the AES key keygenerator generator = Keygenerator.getinstance ("AES"); Generator.init (128,new securerandom (Salt.getbytes ( ))); Secretkey key = Generator.generatekey (); Secretkeyspec spec = new Secretkeyspec (key.getencoded (), "AES"),//cipher Cipher = Cipher.getinstance ("AES"); Cipher.init (Cipher.decrypt_mode, spec); byte[] dofinal = cipher.dofinal (ENCODESTR); return new String (dofinal);}

Note:

If in the decryption algorithm:

Generator.init (128,new securerandom (Salt.getbytes ())); The corresponding salt is not added, then an error is given: the algorithm is wrong;

But the encryption algorithm does not have the salt to be able to encrypt, the decryption is not possible;

Java Plus decryption

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.