Java des encryption and decryption _java

Source: Internet
Author: User

Don't say much nonsense, directly to the code:

Copy Code code as follows:

Package com.eabax.plugin.yundada.utils;
Import java.io.IOException;
Import java.security.InvalidKeyException;
Import java.security.NoSuchAlgorithmException;
Import Java.security.SecureRandom;
Import java.security.spec.InvalidKeySpecException;
Import javax.crypto.BadPaddingException;
Import Javax.crypto.Cipher;
Import javax.crypto.IllegalBlockSizeException;
Import javax.crypto.NoSuchPaddingException;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESKeySpec;
Import org.apache.commons.codec.binary.Base64;
Import Sun.misc.BASE64Decoder;
public class Desencrypthelper {
Private final static String des = "des";
/**
* Generate key
* @param employeecode
*/
public static string Getdeskey (String encryptstr) {
if (! Cachemanager.getcache (). ContainsKey ("Encryptkey_" +encryptstr)) {
Cachemanager.getcache (). Put ("Encryptkey_" +encryptstr, encryptstr+ "Tablemiyaokey");
}
String key = (string) cachemanager.getcache (). Get ("Encryptkey_" +encryptstr);
Return key;
}
/**
* Description is decrypted based on the key value
* @param data
* @param key Encryption key byte array
* @return
* @throws IOException
* @throws Exception
*/
public static string decrypt (string data, String key) throws IOException,
Exception {
if (data = null)
return null;
Base64decoder decoder = new Base64decoder ();
byte[] buf = decoder.decodebuffer (data);
byte[] bt = Decrypt (Buf,key.getbytes ());
return new String (BT);
}
/**
* Encrypt the string
* @param str
* @return
* @throws InvalidKeyException
* @throws illegalblocksizeexception
* @throws badpaddingexception
* @throws invalidkeyspecexception
* @throws nosuchalgorithmexception
* @throws nosuchpaddingexception
*/
public static string Getencryptstr (String str,string encryptstr) throws InvalidKeyException,
Illegalblocksizeexception, Badpaddingexception,
Invalidkeyspecexception, NoSuchAlgorithmException,
nosuchpaddingexception {
Get key
String key = Getdeskey (ENCRYPTSTR);
Get key
Secretkeyfactory factory = secretkeyfactory.getinstance ("DES");
Deskeyspec Keyspec = new Deskeyspec (Key.getbytes ());
Secretkey Deskey = Factory.generatesecret (Keyspec);
Cipher is responsible for completing the encryption or decryption work
Cipher C = cipher.getinstance ("DES");
Initializes the cipher object based on the key, Decrypt_mode represents the encryption mode
C.init (Cipher.encrypt_mode, Deskey);
byte[] src = str.getbytes ();
The byte array is responsible for saving the results of the encryption
byte[] cipherbyte = c.dofinal (src);
String enstr = new String (Base64.encodebase64 (cipherbyte));
return enstr;
}
/**
* Description is decrypted based on the key value
* @param data
* @param key Encryption key byte array
* @return
* @throws Exception
*/
private static byte[] Decrypt (byte[] data, byte[] key) throws Exception {
Generate a trustworthy random number source
securerandom sr = new SecureRandom ();
Create a Deskeyspec object from raw key data
Deskeyspec DKs = new Deskeyspec (key);
Create a key factory and use it to convert Deskeyspec to Secretkey objects
Secretkeyfactory keyfactory = secretkeyfactory.getinstance (DES);
Secretkey SecureKey = Keyfactory.generatesecret (DKS);
The Cipher object actually completes the decryption operation
Cipher Cipher = cipher.getinstance (DES);
Initializing a Cipher object with a key
Cipher.init (Cipher.decrypt_mode, SecureKey, SR);
return cipher.dofinal (data);
}
}

The above is the article about DES Encryption and decryption code, I hope to learn Java to help you.

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.