Java Common Tools class encapsulates--des encryption and decryption tool class __java

Source: Internet
Author: User
Tags base64 decrypt

The title, for reference.

Import java.io.UnsupportedEncodingException;
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.KeyGenerator;
Import javax.crypto.NoSuchPaddingException;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
 
Import Javax.crypto.spec.DESKeySpec;
/** * des encryption and decryption. * * @author Jiangshuai * @date October 03, 2016/public class Desutil {/** security key/private String KeyData = "ABCDE
 
    Fghijklmnopqrstwxyzabcdefghijklmnopqrstwxyz0123456789-_. ";
     /** * Function: Construction * * @author Jiangshuai * @date October 03, 2016/Public Desutil () {}/** 
    * Function: Construction * * @author Jiangshuai * @date October 03, 2016 * @param keyData * Key * * Public desutil (String keY) {this.keydata = key;            /** * Features: Encryption (UTF-8) * * @author Jiangshuai * @date October 03, 2016 * @param source *
     SOURCE String * @param charSet * encoded * @return String * @throws unsupportedencodingexception * Encoding exception/public string encrypt (String source) throws Unsupportedencodingexception {RET
    Urn Encrypt (source, "UTF-8"); /** * * Features: Decryption (UTF-8) * * @author Jiangshuai * @date October 03, 2016 * @param encryptedd
     ATA * Encrypted String * @return String * @throws unsupportedencodingexception * Encoding exception */public string decrypt (string EncryptedData) throws Unsupportedencodingexception {return de
    Crypt (EncryptedData, "UTF-8");            /** * Features: Encryption * * @author Jiangshuai * @date October 03, 2016 * @param source *       SOURCE String * @param charSet *     Code * @return String * @throws unsupportedencodingexception * Code exception/Public Strin
        G Encrypt (string source, String charSet) throws Unsupportedencodingexception {string encrypt = null;
        byte[] ret = Encrypt (Source.getbytes (charSet));
        Encrypt = new String (Base64.encode (ret));
    return encrypt;
     /** * * Function: Decryption * * @author Jiangshuai * @date October 03, 2016 * @param EncryptedData * Encrypted String * @param charSet * code * @return String * @throws unsupportedencoding Exception * Encoding exception/public string decrypt (string EncryptedData, string charSet) thro
        WS Unsupportedencodingexception {String descrypteddata = null;
        byte[] ret = Descrypt (Base64.decode (Encrypteddata.tochararray ()));
        Descrypteddata = new String (ret, charSet);
    return descrypteddata; }/** * Encrypt data Encrypt raw data with generated key * * @param primarydata * RAW data * @return byte[] * @author Jiangshuai * @date October 03, 2016/private byte[] Encrypt (byte[] primarydata) {/** obtain security key/byte Rawkeyd
 
        Ata[] = Getkey ();
 
        The/** des algorithm requires a trustworthy random number source/securerandom sr = new SecureRandom ();
        /** uses raw key data to create a Deskeyspec object/deskeyspec DKs = null;
        try {dks = new Deskeyspec (Keydata.getbytes ());
        catch (InvalidKeyException e) {e.printstacktrace ();
        /** Create a key factory */secretkeyfactory keyfactory = null;
        try {keyfactory = secretkeyfactory.getinstance ("DES");
        catch (NoSuchAlgorithmException e) {e.printstacktrace ();
        /** convert Deskeyspec to a Secretkey object with a key factory * * Secretkey key = null;
        try {key = Keyfactory.generatesecret (DKS);
      catch (Invalidkeyspecexception e) {      E.printstacktrace ();
        The/** Cipher object actually completes the cryptographic operation */Cipher Cipher = null;
        try {cipher = cipher.getinstance ("DES");
        catch (NoSuchAlgorithmException e) {e.printstacktrace ();
        catch (Nosuchpaddingexception e) {e.printstacktrace ();
        /** initialize cipher object with key/try {Cipher.init (Cipher.encrypt_mode, Key, SR);
        catch (InvalidKeyException e) {e.printstacktrace ();
        /** official execution of cryptographic operations */byte encrypteddata[] = null;
        try {EncryptedData = cipher.dofinal (Primarydata);
        catch (IllegalStateException e) {e.printstacktrace ();
        catch (Illegalblocksizeexception e) {e.printstacktrace ();
        catch (Badpaddingexception e) {e.printstacktrace ();
    /** Return Encrypted data * * ENCRYPTEDDATA; /** * Decrypt data with key * * @paramEncryptedData * Encrypted data * @return byte[] * @author Jiangshuai * @date October 03, 2016 * * Private byte[] Descrypt (byte[] encrypteddata) {/** des algorithm requires a trustworthy random number source/securerandom sr = new Secure
 
        Random ();
 
        /** obtains the security key * */byte rawkeydata[] = Getkey ();
        /** uses raw key data to create a Deskeyspec object/deskeyspec DKs = null;
        try {dks = new Deskeyspec (Keydata.getbytes ());
        catch (InvalidKeyException e) {e.printstacktrace ();
        /** Create a key factory */secretkeyfactory keyfactory = null;
        try {keyfactory = secretkeyfactory.getinstance ("DES");
        catch (NoSuchAlgorithmException e) {e.printstacktrace ();
        /** convert Deskeyspec to a Secretkey object with a key factory * * Secretkey key = null;
        try {key = Keyfactory.generatesecret (DKS);
 catch (Invalidkeyspecexception e) {e.printstacktrace ();       The/** Cipher object actually completes the cryptographic operation */Cipher Cipher = null;
        try {cipher = cipher.getinstance ("DES");
        catch (NoSuchAlgorithmException e) {e.printstacktrace ();
        catch (Nosuchpaddingexception e) {e.printstacktrace ();
        /** initialize cipher object with key/try {Cipher.init (Cipher.decrypt_mode, Key, SR);
        catch (InvalidKeyException e) {e.printstacktrace ();
        /** Official decryption operation */byte decrypteddata[] = null;
        try {decrypteddata = cipher.dofinal (EncryptedData);
        catch (IllegalStateException e) {e.printstacktrace ();
        catch (Illegalblocksizeexception e) {e.printstacktrace ();
        catch (Badpaddingexception e) {e.printstacktrace ();
    return decrypteddata; /** * Obtaining a security key This method is invalid because each key generation is different, which causes decryption encryption keys to be different, resulting in given final block not * PROperly padded error.
 
        * * @return byte array * @author Jiangshuai * @date October 03, 2016/private byte[] Getkey () {
 
        The/** des algorithm requires a trustworthy random number source/securerandom sr = new SecureRandom ();
        /** generates a key generator object for our selected des algorithm */keygenerator kg = null;
        try {kg = keygenerator.getinstance ("DES");
        catch (NoSuchAlgorithmException e) {e.printstacktrace ();
 
        } kg.init (SR);
 
        /** Generate Key Tool class * * Secretkey key = Kg.generatekey ();
 
        /** generate key byte array */byte rawkeydata[] = key.getencoded ();
    return rawkeydata; }
 
}


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.