Java implementation of DES Encryption and decryption

Source: Internet
Author: User

Des.java directly on source code

Package Com.demo;

Import Java.security.Key;
Import Java.security.SecureRandom;

Import Javax.crypto.Cipher;
Import Javax.crypto.KeyGenerator;

Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;

public class DES {

Key key;

Public DES (String str) {
Setkey (str);//Generate Key
}

Public DES () {
Setkey ("Siyue_qi");
}

/**
* Generate key based on parameters
*/
public void Setkey (String strkey) {
try {
Keygenerator _generator = keygenerator.getinstance ("DES");
_generator.init (New SecureRandom (Strkey.getbytes ()));
This.key = _generator.generatekey ();
_generator = null;
catch (Exception e) {
throw new RuntimeException ("Error Initializing Sqlmap class." Cause: "+ e);
}
}

/**
* Encrypted string plaintext input, string ciphertext output
*/
public string getencstring (string strming) {
byte[] Bytemi = null;
byte[] byteming = null;
String strmi = "";
Base64encoder base64en = new Base64encoder ();
try {
byteming = Strming.getbytes ("UTF8");
Bytemi = This.getenccode (byteming);
Strmi = Base64en.encode (Bytemi);
catch (Exception e) {
throw New RuntimeException (
"Error initializing Sqlmap class." Cause: "+ e);
finally {
Base64en = null;
byteming = null;
Bytemi = null;
}
return strmi;
}

/**
* Decryption is entered in string cipher text, string plaintext output
* @param strmi
* @return
*/
public string getdesstring (string strmi) {
Base64decoder base64de = new Base64decoder ();
byte[] byteming = null;
byte[] Bytemi = null;
String strming = "";
try {
Bytemi = Base64de.decodebuffer (Strmi);
byteming = This.getdescode (Bytemi);
strming = new String (byteming, "UTF8");
catch (Exception e) {
throw New RuntimeException (
"Error initializing Sqlmap class." Cause: "+ e);
finally {
BASE64DE = null;
byteming = null;
Bytemi = null;
}
return strming;
}

/**
* Encrypt with byte[] plaintext input, byte[] ciphertext output
* @param ByteS
* @return
*/
Private byte[] Getenccode (byte[] byteS) {
byte[] Bytefina = null;
Cipher Cipher;
try {
cipher = Cipher.getinstance ("DES");
Cipher.init (Cipher.encrypt_mode, key);
Bytefina = Cipher.dofinal (ByteS);
catch (Exception e) {
throw New RuntimeException (
"Error initializing Sqlmap class." Cause: "+ e);
finally {
cipher = NULL;
}
return Bytefina;
}

/**
* Decrypt with byte[] ciphertext input, in byte[] Clear text output
* @param byted
* @return
*/
Private byte[] Getdescode (byte[] byted) {
Cipher Cipher;
byte[] Bytefina = null;
try {
cipher = Cipher.getinstance ("DES");
Cipher.init (Cipher.decrypt_mode, key);
Bytefina = Cipher.dofinal (byted);
catch (Exception e) {
throw New RuntimeException (
"Error initializing Sqlmap class." Cause: "+ e);
finally {
cipher = NULL;
}
return Bytefina;
}

public static void Main (String args[]) {
Des des = new des ();
Setting keys
Des.setkey ("12345678 =");

String str1 = "123";
DES encryption
String str2 = des.getencstring (STR1);
String destr = des.getdesstring (STR2);
System.out.println ("Ciphertext:" +STR2);
Des decryption
System.out.println ("plaintext:" +destr);
}
}

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.