Examples of the encapsulation and use of DES plus decryption

Source: Internet
Author: User
Tags key requires return
Encapsulation | decryption
Original: zhouxianli1978@msn.com, welcome to like Java netizens add me



DES encryption Package

Package Org.jtool.desutils;import Java.security.securerandom;import Javax.crypto.*;import Javax.crypto.spec.deskeyspec;public class Desencrypt {    private byte[] deskey;    Public Desencrypt (byte[] deskey) {        This.deskey = desKey;    }    public byte[] Doencrypt (byte[] plaintext) throws Exception {      & nbsp The       des algorithm requires a trustworthy random number source         securerandom sr = new SecureRandom ();        byte rawkeydata[] = deskey;/* obtains key data in some way */        //Create Deskeyspec object from raw key data         deskeyspec DKs = New Deskeyspec (Rawkeydata);       //Create a key factory and use it to convert Deskeyspec to         //A Secretkey object         SecretkeyFactory keyfactory = secretkeyfactory.getinstance ("DES");        SecretKey key = Keyfactory.generatesecret (DKS);       //cipher objects actually complete cryptographic operations          Cipher Cipher = cipher.getinstance ("DES");       // Initializes the cipher object with the key         Cipher.init (Cipher.encrypt_mode, Key, SR);        //Now, get data and Encrypt         byte data[] = plaintext;/* Get data */       //formally perform cryptographic operations         byte in some way Encrypteddata[] = cipher.dofinal (data);        return encrypteddata;    }}



Des decryption Encapsulation

Package Org.jtool.desutils;import Java.security.securerandom;import Javax.crypto.*;import Javax.crypto.spec.deskeyspec;public class Desdecrypt {    private byte[] deskey;    Public Desdecrypt (byte[] deskey) {        This.deskey = desKey;    }    public byte[] Dodecrypt (byte[) encrypttext) throws Exception {      The   //      des algorithm requires a trustworthy random number source         securerandom sr = new SecureRandom ();        byte rawkeydata[] = DesKey; /* Use some method to get raw key data */       //Create a Deskeyspec object from raw key data          deskeyspec DKs = new Deskeyspec (rawkeydata);       //Create a key factory and use it to convert the Deskeyspec object to        //a Secretkey object         Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");        Secretkey key = Keyfactory.generatesecret (DKS);       //Cipher object actually completes decryption operation          Cipher Cipher = cipher.getinstance ("DES");        //initialization of cipher object         cipher.init (Cipher.decrypt_mode, Key, SR) with key;        //Now, get data and decrypt         byte Encrypteddata[] = encrypttext;/* obtains encrypted data */       //official decryption Operation          byte decrypteddata[] = cipher.dofinal (EncryptedData);         return decrypteddata;   }}



Des encryption and decryption using samples

Package org.jtool.desutils;/** *  */public class destest {    public static void Main (string[) args) throws Exception {        String key = "ABCDEFGH";         String value = "Aabbccddee";        desencrypt desencrypt = new Desencrypt (Key.getbytes ());        byte[] Encrypttext = DesEncrypt.doEncrypt ( Value.getbytes ());        System.out.println ("doencrypt-" + tohexstring ( Encrypttext));        System.out.println ("Doencrypt-" + new String (Encrypttext) );        Desdecrypt desdecrypt = new Desdecrypt (Key.getbytes ());         byte[] Decrypttext = Desdecrypt.dodecrypt (encrypttext);         System.out.println ("Dodecrypt-" + new String (Decrypttext)); &NBSP;&Nbsp;      System.out.println ("dodecrypt-" + tohexstring (decrypttext));    }   /**     * 16 Feed display Data      *      * param value byte array      * @return      */    public static String Tohexstring (byte[] value) {        String newstring = "";         for (int i = 0; i < value.length i++) {         & nbsp;  byte B = value[i];            String str = Integer.tohexstring (b);            if (Str.length () > 2) {                str = str.substring ( Str.length ()-2);            }            if (Str.length () < 2) {                 str = "0" + str;            }            NewString + str;       }        return Newstring.touppercase ();   }}


Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.