Java using DES encryption and decryption sample sharing _java

Source: Internet
Author: User
Tags base64 decrypt log log

Copy Code code as follows:

Import Java.security.Key;
Import Java.security.SecureRandom;
Import Java.security.spec.AlgorithmParameterSpec;

Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESKeySpec;
Import Javax.crypto.spec.IvParameterSpec;

Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;


public class Des
{
public static final String algorithm_des = "des/cbc/pkcs5padding";
private static log log = Logfactory.getlog (Des.class);

/**
* des algorithm, encryption
*
* @param data to be encrypted string
* @param key Encryption private key, length can not be less than 8 bits
* @return encrypted byte array, generally combined with BASE64 encoding
* @throws Cryptexception exception
*/
public static string encode (string key,string data) throws Exception
{
Return encode (Key, data.getbytes ());
}
/**
* des algorithm, encryption
*
* @param data to be encrypted string
* @param key Encryption private key, length can not be less than 8 bits
* @return encrypted byte array, generally combined with BASE64 encoding
* @throws Cryptexception exception
*/
public static string Encode (String key,byte[] data) throws Exception
{
Try
{
Deskeyspec DKs = new Deskeyspec (Key.getbytes ());

Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");
Key cannot be less than 8 bytes in length
Key Secretkey = Keyfactory.generatesecret (DKS);
Cipher Cipher = cipher.getinstance (algorithm_des);
Ivparameterspec IV = new Ivparameterspec ("********". GetBytes ());
Algorithmparameterspec Paramspec = IV;
Cipher.init (Cipher.encrypt_mode, Secretkey,paramspec);

byte[] bytes = cipher.dofinal (data);
Return Base64.encode (bytes);
catch (Exception e)
{
throw new Exception (e);
}
}

/**
* des algorithm, decryption
*
* @param data to decrypt string
* @param key to decrypt the private key, the length can not be less than 8 bits
* @return the decrypted byte array
* @throws Exception exception
*/
public static byte[] Decode (String key,byte[] data throws Exception
{
Try
{
securerandom sr = new SecureRandom ();
Deskeyspec DKs = new Deskeyspec (Key.getbytes ());
Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");
Key cannot be less than 8 bytes in length
Key Secretkey = Keyfactory.generatesecret (DKS);
Cipher Cipher = cipher.getinstance (algorithm_des);
Ivparameterspec IV = new Ivparameterspec ("********". GetBytes ());
Algorithmparameterspec Paramspec = IV;
Cipher.init (Cipher.decrypt_mode, Secretkey,paramspec);
return cipher.dofinal (data);
catch (Exception e)
{
E.printstacktrace ();
throw new Exception (e);
}
}

/**
* Get the encoded value
* @param key
* @param data
* @return
* @throws Exception
* @throws Exception
*/
public static string Decodevalue (string key,string data) throws Exception
{
Byte[] datas;
String value = null;

Datas = Decode (key, Base64.decode (data));

Value = new String (datas);
if (Value.equals ("")) {
throw new Exception ();
}
return value;
}
}

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.