Des encryption and decryption->java implementation

Source: Internet
Author: User
Tags decrypt
the Java classes involved in DES encryption decryption Cipher

This class provides password functionality for encryption and decryption. It forms the core of the Java cryptographic Extension (JCE) framework .
To create the Cipher object, the application invokes the Cipher getinstance method and passes the name of the requested transformation to it. You can also specify the name of the provider (optional).
A transformation is a string that describes an action (or a set of actions) to be performed on a given input to produce some sort of output. The conversion always includes the name of the cryptographic algorithm (for example, DES), which may be followed by a feedback pattern and a fill scheme.
The conversion has the following form:
Algorithm/mode/fill or algorithm
(in the latter case, the usage pattern and the fill scheme are specific to the provider's default value). For example, the following are valid conversions:
Cipher C = cipher.getinstance ("des/cbc/pkcs5padding");
Using patterns such as CFB and OFB, Cipher blocks can encrypt the data in the actual block size of the Cipher in the cell. When you request such a pattern, you can specify the number of bits to be processed at a time (optional): Add this number to the schema name, as shown in the "Des/cfb8/nopadding" and "des/ofb32/pkcs5padding" transformations. If this number is not specified, the provider-specific default value is used. (for example, the SUNJCE provider uses the default 64-bit for DES). Thus, by using a 8-bit pattern such as CFB8 or OFB8, the Cipher block can be converted to a byte-oriented Cipher stream.
Java Implementation ">
Java Implementation ">
Java implementation "> secretkeyfactory

This class represents a factory of secret keys.
The key factory is used to convert the key (the Opaque encryption key of type key) to the key specification (the transparent representation of the underlying keying material), and vice versa. The secret key factory operates only on Secret (symmetric) keys.
The key factory is duplex mode, which allows you to build an opaque key object based on a given key specification (keying material) or to obtain the underlying keying material for the key object in the appropriate format.
Application developers should refer to their provider documentation to find the key specifications supported by the Generatesecret and Getkeyspec methods. For example, the DES secret key factory provided by the "SUNJCE" provider supports DESKEYSPEC as a transparent representation of the DES Key, and the provider's Triple des Key's secret key factory supports Desedekeyspec as the Triple DES key A transparent representation.
Java implementation "> keyspec

Public Interface Keyspec the (transparent) specification that makes up the key contents of the cryptographic key.
If the key is stored on a hardware device, its specification can contain information that helps identify the key on that device.
A key can be specified using an algorithm-specific method or an encoding format that is independent of the algorithm (for example, ASN.1). For example, the DSA private key can be specified by its components x, p, Q, and G (see DSAPRIVATEKEYSPEC) or by its DER encoding (see PKCS8ENCODEDKEYSPEC).
This interface does not contain any methods or constants. It is used only to group all key specifications and provide type safety for them. This interface must be implemented by all key specifications.

Deskeyspec can be seen in the implementation class.
Java implementation "> secretkey

Public Interface Secretkeyextends key Secret (symmetric) key.
This interface does not contain methods or constants. Its sole purpose is to group secret keys (and provide type safety for them).
The provider implementation of this interface must overwrite the Equals and Hashcode methods inherited from Java.lang.Object in order to compare secret keys based on the underlying keying material rather than on the reference.
The key that implements this interface returns the string raw in its encoded format (see GETFORMAT) and returns the original key byte as the result of the Getencoded method call. (The GetFormat and getencoded methods inherit from the Java.security.Key parent interface.) )
use the following methods to obtain the secret key.

Secretkey Secretkey = Secretkeyfactory.generatesecret (keyspec);

The cipher init method is then used to initialize the cipher object, which can then be encrypted and decrypted.

Cipher.init (Cipher.encrypt_mode, Secretkey,new securerandom ()); Implement class Desencrypttools

Package com.david.des;
Import Java.security.SecureRandom;
Import Java.security.spec.KeySpec;
Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;

Import Javax.crypto.spec.DESKeySpec; /** * * Project Name: Ciphertest * Class Name: Aesencrypttools * Class Description: Des encryption and decryption algorithm * Founder: David * Create time: May 2, 2016 afternoon 8:0 0:21 * Copyright (c) david-All rights reserved * * Public final class Desencrypttools {//encryption is des private static final stri
    ng algorithm = "DES";

    Convert format private static final String transformation = "des/ecb/pkcs5padding";
        Use 8 byte 64-bit key to SRC encryption @SuppressWarnings ("unused") public static byte[] Encrypt (byte[] src,byte[]key) {
            try {//encrypt Cipher Cipher = cipher.getinstance (transformation);
            Secretkeyfactory secretkeyfactory = secretkeyfactory.getinstance (algorithm);
            Keyspec Keyspec = new Deskeyspec (key); Secretkey Secretkey = Secretkeyfactory.generatesecret (kEYSPEC);
            Cipher.init (Cipher.encrypt_mode, Secretkey,new securerandom ());    
            byte[] enmsgbytes = cipher.dofinal (src);
        return enmsgbytes;
        catch (Exception e) {e.printstacktrace ();
    return null; ////Use 8 byte 64-bit key to decrypt SRC @SuppressWarnings ("unused") public static byte[] Decrypt (byte[) Encryptbytes,byte[]ke
            Y) {try {//decrypt//cipher decipher = cipher.getinstance ("des/cbc/pkcs5padding");
            Cipher decipher = cipher.getinstance (transformation);
            Secretkeyfactory dedecretkeyfactory = secretkeyfactory.getinstance (algorithm);
            Keyspec Dekeyspec = new Deskeyspec (key);
            Secretkey Desecretkey = Dedecretkeyfactory.generatesecret (Dekeyspec);
            Decipher.init (Cipher.decrypt_mode, Desecretkey,new securerandom ());
            byte[] demsgbytes = decipher.dofinal (encryptbytes);
        return demsgbytes; catch (Exceptione) {e.printstacktrace ();
    return null; }
}
Test Class
Package com.david.des;

public class Desmaintest {

    private static String key = "12345678";

    public static void Main (string[] args) throws exception{
        String msg = ' Hello World '. Hello, DES ";
        System.out.println ("Before encryption:" +msg);
        byte[] encryptbytes = Desencrypttools.encrypt (Msg.getbytes (), key.getbytes ());
        SYSTEM.OUT.PRINTLN ("Encrypted:" +new String (encryptbytes));
        byte[] demsgbytes = Desencrypttools.decrypt (Encryptbytes,key.getbytes ());
        System.out.println ("After decryption:" +new String (demsgbytes));
    }

Test Results

Java implementation "> summary

Learned the DES encryption and decryption algorithm.

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.