Des algorithm Java implementation, can be used in the project

Source: Internet
Author: User
Tags decrypt

Des algorithm is a symmetric cryptosystem in the cryptography system, also known as the United States data Encryption Standard, is the 1972 U.S. IBM developed symmetric cryptography algorithm. The plaintext is grouped by 64 bits, the key is 64 bits long, the key is in fact 56-bit participating DES operations (8th, 16, 24, 32, 40, 48, 56, 64 bits are check bits, So that each key has an odd number of 1) After grouping the plaintext group and the 56-bit key to the bitwise substitution or Exchange method to form the encryption method of the ciphertext group.


Des algorithm has very high security, so far, in addition to using exhaustive search method to attack des algorithm, has not found a more effective method. While the 56-bit long Key's exhaustive space is 256, which means that if a computer's speed is to detect 1 million keys per second, then it will take nearly 2,285 years to search the complete key, which is difficult to achieve


There are three entry parameters: key, data, mode. Key is the secret key used for encryption and decryption, and data is encrypted

des algorithm ArchitectureDecrypt the data, mode for its operating modes. When the mode is in encryption mode, plaintext is grouped by 64 bits to form a clear text group, key is used to encrypt the data, and when the mode is decryption mode, key is used to decrypt the data. In practice, the key only uses 64 bits of 56 bits, so that it has high security.


1, algorithm main program class SymmetricAlgorithm


Package com.gary.test.ws.test;

Import java.io.UnsupportedEncodingException;
Import java.security.InvalidKeyException;
Import Java.security.Key;
Import java.security.NoSuchAlgorithmException;
Import javax.crypto.BadPaddingException;
Import Javax.crypto.Cipher;
Import javax.crypto.IllegalBlockSizeException;
Import javax.crypto.NoSuchPaddingException;
Import Javax.crypto.spec.SecretKeySpec;

public class SymmetricAlgorithm {
Private String strkey = "&^%$*#@~";
Private String info;

Public SymmetricAlgorithm (String info) {
This.info = info;
}

Public SymmetricAlgorithm (String info, string strkey) {
This.info = info;
This.strkey = strkey;
}

Private Key GetKey () {
byte[] Keybtye = This.strKey.getBytes ();

byte[] _keybyte = new Byte[8];

for (int i = 0; (I < keybtye.length) && (i < _keybyte.length); i++) {
_keybyte[i] = Keybtye[i];
}

return new Secretkeyspec (_keybyte, "DES");
}

Public String Desencrypt () {
Return Desencrypt (This.info, "UTF-8");
}

public string Desencrypt (string origin, string encoding) {
if (origin = = null) | | (encoding = = null))
return null;
try {
Return Encrypt (origin.getbytes (encoding), "DES");
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
return null;
}

Public String Desdecrypt () {
Return Desdecrypt (This.info, "UTF-8");
}

public string Desdecrypt (string ciperdata, string encoding) {
if ((Ciperdata = = null) | | (encoding = = null)) {
return null;
}
Byte[] B = Decrypt (Encrypthelper.hex2byte (Ciperdata), "DES");
try {
return new String (b, encoding);
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
return null;
}

private string Encrypt (byte[] data, string algorithm) {
try {
Key key = GetKey ();

Cipher C1 = cipher.getinstance (algorithm);
C1.init (1, key);
byte[] Cipherbyte = c1.dofinal (data);
Return Encrypthelper.byte2hex (Cipherbyte);
} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
} catch (Nosuchpaddingexception e) {
E.printstacktrace ();
} catch (InvalidKeyException e) {
E.printstacktrace ();
} catch (Illegalblocksizeexception e) {
E.printstacktrace ();
} catch (Badpaddingexception e) {
E.printstacktrace ();
}
return null;
}

Private byte[] Decrypt (byte[] data, String algorithm) {
try {
Key key = GetKey ();
Cipher C1 = cipher.getinstance (algorithm);
C1.init (2, key);
return c1.dofinal (data);
} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
} catch (Nosuchpaddingexception e) {
E.printstacktrace ();
} catch (InvalidKeyException e) {
E.printstacktrace ();
} catch (Illegalblocksizeexception e) {
E.printstacktrace ();
} catch (Badpaddingexception e) {
E.printstacktrace ();
}
return null;
}

public static void Main (string[] args) {
SymmetricAlgorithm s =new symmetricalgorithm ("Gp3adm", "&^%$*#@~");//encryption

String sa_pwd = S.desencrypt ("Gp3adm", "UTF-8");

SYSTEM.OUT.PRINTLN ("After encryption:" +SA_PWD);
String pwd = "84dfa223a4521331";

String Password = (new SymmetricAlgorithm (PWD)). Desdecrypt ();//decryption
System.out.println ("After decryption:" +password);

}
}



2. Assistant Class Encrypthelper


Package com.gary.test.ws.test;

public class Encrypthelper
{
public static final String default_encoding = "UTF-8";

public static String byte2hex (byte[] bytes)
{
StringBuffer retstring = new StringBuffer ();
for (int i = 0; i < bytes.length; i++) {
Retstring.append (Integer.tohexstring (bytes[i] & 0xFF)). SUBSTRING (1));
}
Return retstring.tostring (). toUpperCase ();
}

public static byte[] Hex2byte (String hex)
{
byte[] BTS = new Byte[hex.length ()/2];
for (int i = 0; i < bts.length; i++) {
Bts[i] = (byte) integer.parseint (hex.substring (2 * I, 2 * i + 2), 16);
}
return BTS;
}
}


Des algorithm Java implementation, which can be used in projects

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.