DES-JAVA Encryption

Source: Internet
Author: User

/* Des Java

Complied OK with eclipse

Create a desencrypt class, copy the following code, and compile it.

*/
Import java. Security. Key;
Import java. Security. securerandom;
Import javax. crypto. cipher;
Import javax. crypto. keygenerator;

Import sun. Misc. base64decoder;
Import sun. Misc. base64encoder;

/**
*
* Use des to encrypt and decrypt byte [] and string types.
* The ciphertext can be stored using string, byte.
*
* Method:
* Void getkey (string strkey) generates a key from the strkey note
*
* String getencstring (string strming) encrypts strming and returns the string ciphertext.
* String getdesstring (string strmi) decrypts strmin and returns the string plaintext.
*
* Byte [] getenccode (byte [] bytes) byte [] Encryption
* Byte [] getdescode (byte [] byted) byte [] type decryption
*/

Public class desencrypt
{
Key key;

/**
* Generate a key based on the parameter
* @ Param strkey
*/
Public void getkey (string strkey)
{
Try {
Keygenerator _ generator = keygenerator. getinstance ("des ");
_ Generator. INIT (New securerandom (strkey. getbytes ()));
This. Key = _ generator. generatekey ();
_ Generator = NULL;
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Encrypted string plaintext input and string ciphertext output
* @ Param strming
* @ Return
*/
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)
{
E. printstacktrace ();
}
Finally
{
Base64en = NULL;
Byteming = NULL;
Bytemi = NULL;
}
Return strmi;
}
/**
* Decryption is input in string ciphertext and output in string plaintext.
* @ 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)
{
E. printstacktrace ();
}
Finally
{
Base64de = NULL;
Byteming = NULL;
Bytemi = NULL;
}
Return strming;
}
/**
* Encryption is input in byte [] plaintext, and byte [] ciphertext is 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)
{
E. printstacktrace ();
}
Finally
{
Cipher = NULL;
}
Return bytefina;
}
/**
* Decryption is input in byte [] ciphertext and output in byte [] plaintext
* @ 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 ){
E. printstacktrace ();
} Finally {
Cipher = NULL;
}
Return bytefina;

}

Public static void main (string [] ARGs ){

System. Out. println ("des Demo ");
Desencrypt des = new desencrypt (); // instantiate an object
Des. getkey ("mykey11"); // generate a key
System. Out. println ("Key = mykey11 ");
String strenc = des. getencstring ("China"); // encrypted string, returns the ciphertext of string
System. Out. println ("ciphertext =" + strenc );

String strdes = des. getdesstring (strenc); // decrypt the string-type ciphertext.
System. Out. println ("plaintext =" + strdes );

}

}

/*

Running result:

Des demo
Key = mykey11
Ciphertext = n2r7f/mmyiy =
Plaintext = China

*/

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chinayaosir/archive/2008/10/20/3111507.aspx

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.