Java encryption Technology (c) symmetric encryption algorithm PBE

Source: Internet
Author: User
Tags decrypt asymmetric encryption

In addition to DES, we also know that there are Desede (TripleDES, 3DES), AES, Blowfish, RC2, RC4 (Arcfour) and other symmetric encryption methods, the implementation of the same way, here is another algorithm of symmetric encryption--PBE

PBE
pbe--password-based Encryption (password-based encryption). It is characterized in that the password is controlled by the user, does not rely on any physical media, using random numbers (here we call salt) hashing multiple encryption methods to ensure the security of the data. is an easy way to encrypt.


The Java code is implemented as follows:

Import Java.security.key;import java.util.arrays;import Java.util.random;import Javax.crypto.cipher;import Javax.crypto.secretkey;import Javax.crypto.secretkeyfactory;import Javax.crypto.spec.pbekeyspec;import Javax.crypto.spec.pbeparameterspec;import org.apache.commons.codec.binary.hex;/** symmetric encryption PBE algorithm * @Title: PBEUtil.java * @Package Com.somnus.cipher * @Description: TODO * @author Somnus * @date June 6, 2015 morning 8:57:52 * @version V1.0 */public C  Lass Pbeutil {/** * supports any of the following algorithms * * <pre> * pbewithmd5anddes * pbewithmd5andtripledes * Pbewithsha1anddesede * PBEWITHSHA1ANDRC2_40 * </pre> * * public static final String algorithm = "PBE      Withmd5anddes ";         /** * Salt Initialization * * @return * @throws Exception * * public static byte[] Initsalt () throws Exception {         Byte[] Salt = new byte[8];         Random random = new random ();         Random.nextbytes (salt);     return salt;  }/** * Convert key <br>   * * @param password * @return * @throws Exception * * private static Key Keygenerator (String passwo         RD) throws Exception {Pbekeyspec KeySpec = new Pbekeyspec (Password.tochararray ());         Create a key factory and use it to convert Pbekeyspec to secretkeyfactory keyfactory = secretkeyfactory.getinstance (algorithm);         Secretkey Secretkey = Keyfactory.generatesecret (KeySpec);     return secretkey; /** * Encrypt * * @param data * @param password * password * @param SA LT * Salt * @return * @throws Exception */public static string encrypt (string data, String PA         ssWOrd, byte[] salt) throws Exception {Key key = Keygenerator (password);        Pbeparameterspec Paramspec = new Pbeparameterspec (salt, 100);        Instantiates the Cipher object, which is used to complete the actual cryptographic operation Cipher Cipher = cipher.getinstance (algorithm);      Initialize the Cipher object, set to cryptographic mode Cipher.init (Cipher.encrypt_mode, Key, Paramspec); byte[] Buff = cipher.dofinal (Data.getbytes ());       System.out.println (arrays.tostring (buff)); Performs a cryptographic operation.   The result of the encryption is usually transmitted with BASE64 encoding return hex.encodehexstring (buff); */** * Decrypt * * @param data * @param password * password * @param SA LT * Salt * @return * @throws Exception */public static string decrypt (string data, String PA         ssWOrd, byte[] salt) throws Exception {Key key = Keygenerator (password);        Pbeparameterspec Paramspec = new Pbeparameterspec (salt, 100);       Instantiates the Cipher object, which is used to complete the actual decryption operation Cipher Cipher = cipher.getinstance (algorithm);       Initializes the cipher object, set to decryption mode Cipher.init (Cipher.decrypt_mode, Key, Paramspec);       Perform decryption operation byte[] buff = cipher.dofinal (Hex.decodehex (Data.tochararray ()));       System.out.println (arrays.tostring (buff));   return new String (buff); public static void Main (string[] args) throws Exception {String inputstr= "Somnus";          System.out.println ("Original:" + inputstr);         String pwd = "EFG";          System.out.println ("Password:" + pwd);          byte[] Salt = initsalt ();         String EncryptData = Encrypt (inputstr, pwd, salt);          SYSTEM.OUT.PRINTLN ("After encryption:" + EncryptData);         String Decryptdata = Decrypt (encryptData, PWD, salt);     System.out.println ("After decryption:" + decryptdata); }}

Console output:

Original: somnus Password: efg[45,-91, 116, 19,-105, 95,-5, 31] After encryption: 2DA57413975FFB1F[83, 111, 109, 110, 117, 115] after decryption: Somnus

we will introduce the asymmetric encryption algorithm, such as RSA, DSA, DH, ECC and so on.

Java encryption Technology (c) symmetric encryption algorithm PBE

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.