Java implementation DES encryption and decryption algorithm analysis _java

Source: Internet
Author: User
Tags decrypt

The example of this article describes the Java implementation des Encryption and decryption algorithm analysis. Share to everyone for your reference, specific as follows:

Brief introduction:

Data encryption Algorithm (encryption Algorithm,dea) is a symmetric encryption algorithm, most likely the most widely used key system, especially in the security of financial data protection, the first developed DEA is embedded in the hardware. Often, ATMs (automated Teller MACHINE,ATM) use the DEA. It comes from IBM's research work, and IBM has several years of patent rights for it, but is in the public domain when it expires in 1983, allowing royalties to be waived under certain conditions. 1977 was formally adopted by the United States Government.

After 1998 years of practical des deciphering the emergence of a completely declared DES algorithm has no security, 1999 NIST promulgated a new standard, the DES algorithm can only be used for legacy encryption systems, but not limited to the use of Desede algorithm. Today's DES algorithm is the launch of the historical arena, AES algorithm is called his replacement.

Encryption principle:

DES uses a 56-bit key and an additional 8-bit parity bit to produce the largest 64-bit packet size. This is an iterative grouping cipher, using a technique called Feistel, in which the encrypted text block is divided into two halves. Use a sub key to apply the loop function to half of them, then "XOR" the output to the other half, and then swap the two halves, the process continues, but the last loop does not exchange. DES uses 16 loops, using different or, permutations, substitutions, and shifts to operate four basic operations.

JDK support for DES algorithms

Key length: 56-bit

Working mode: Ecb/cbc/pcbc/ctr/cts/cfb/cfb8 to Cfb128/ofb/obf8 to OFB128

Fill mode: nopadding/pkcs5padding/iso10126padding/

Symmetric encryption algorithm of Java encryption and decryption Desede

Desede is the Triple DES encryption algorithm, also known as 3DES or Triple DES. Use three (or two) different keys to carry out three (or two) DES encryption on a block of data (faster than three times for normal encryption). The strength of the Triple DES is approximately the same as that of the 112-bit key. The number of iterations increases the security, but it also creates a low encryption efficiency problem. Due to Desede algorithm efficiency problem, AES algorithm was born.

So far, no one has given an effective way to attack Sanchong des. The brute-search of the key in its key space is not feasible because of the large space. In the case of a differential attack, complexity increases exponentially in relation to single DES.

There are four models of Triple DES

    • DES-EEE3, using three different keys, the sequence of three times encryption transform.
    • DES-EDE3, use three different keys, followed by encryption-decryption-encryption transform.
    • Des-eee2, where the key k1=k3, the order of three times encryption transformation.
    • Des-ede2, where the key K1=k3, followed by encryption-decryption-encryption transformation.

Java encryption code for the DES algorithm

Package com.favccxx.codelib;
Import Java.security.SecureRandom;
Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESKeySpec;
           
  public class Encryptcoder {private final static String des = "des"; public static byte[] Encrypt (byte[] src, byte[] key throws Exception {//des algorithm requires a trustworthy random number source securerandom sr = NE
    W SecureRandom ();
    Create a Deskeyspec object from the original key data deskeyspec DKs = new Deskeyspec (key);
    Create a key factory and then use it to convert Deskeyspec to a Secretkey object secretkeyfactory keyfactory = Secretkeyfactory.getinstance (DES);
    Secretkey SecureKey = Keyfactory.generatesecret (DKS);
    The Cipher object actually completes the cryptographic operation Cipher Cipher = cipher.getinstance (DES);
    Initializes the cipher object with the key Cipher.init (Cipher.encrypt_mode, SecureKey, SR);
  Official execution of cryptographic operations return cipher.dofinal (SRC); /** * * @param password Password * @param key Encryption String * @return/public final static string enc RyPT (string password, string key) {try {return byte2string (Encrypt (Password.getbytes (), Key.getbytes ()));
  catch (Exception e) {} return null;
    public static string byte2string (byte[] b) {String hs = ' ";
    String stmp = "";
      for (int n = 0; n < b.length n++) {stmp = (java.lang.Integer.toHexString (b[n) & 0XFF));
      if (stmp.length () = = 1) HS = HS + "0" + stmp;
    else HS = hs + stmp;
  return Hs.touppercase (); public static void Main (string[] args) {String encryptstring = Encrypt (' Is Zhang ', "Test in English miscellaneous seven rotten Eight mix @123654{")
    ;
  System.out.println (encryptstring);
 //output: B00542E93695F4CFCE34FC4393C2F4BF}

Java implementation of DES decryption algorithm

Package com.favccxx.codelib;
Import Java.security.SecureRandom;
Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESKeySpec;
     
  public class Descryptcoder {private final static String des = "des";  /** * * @param src data source * @param key key, the length must be a multiple of 8 * @return * @throws Exception/public static byte[]
    Decrypt (byte[] src, byte[] key) throws Exception {//des algorithm requires a trustworthy random number source SecureRandom sr = new SecureRandom ();
    Create a Deskeyspec object from the original key data deskeyspec DKs = new Deskeyspec (key);
    Create a key factory and then use it to convert the Deskeyspec object into a Secretkey object secretkeyfactory keyfactory = Secretkeyfactory.getinstance (DES);
    Secretkey SecureKey = Keyfactory.generatesecret (DKS);
    The Cipher object actually completes the decryption operation Cipher Cipher = cipher.getinstance (DES);
       
    Initializes the cipher object with the key Cipher.init (Cipher.decrypt_mode, SecureKey, SR);
  The official execution of the decryption Operation return cipher.dofinal (SRC); } public FinaL static string decrypt (string data, string key) {try {return new string (Decrypt (String2byte) (Data.getbytes ()),
    Key.getbytes ()));
    catch (Exception e) {e.printstacktrace ();
  return null; public static byte[] String2byte (byte[] b) {if (b.length% 2)!= 0) throw new Illegalargumentexcepti
    On ("length is not an even number");
    byte[] B2 = new BYTE[B.LENGTH/2];
      for (int n = 0; n < b.length n = 2) {String item = new String (b, N, 2);
    B2[N/2] = (byte) integer.parseint (item, 16);
  return B2; public static void Main (string[] args) {String desencryptstring = Decrypt ("B00542E93695F4CFCE34FC4393C2F4BF"
    , "Test Chinese and English miscellaneous Seven rotten Eight Mix @123654");
  System.out.println (desencryptstring);
 //output: is Zhang}

I hope this article will help you, Java implementation des Encryption and decryption algorithm to explain the content of the introduction here. I hope you will continue to pay attention to our website! Want to learn Java can continue to focus on this site.

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.