Java easy-to-use MD5 encryption (can be run directly) (2)

Source: Internet
Author: User

The full text of the program is as follows:
Copy codeThe Code is as follows:
Package com. neusoft. test. util. crypt;
Import java. io. IOException;
Import java. io. UnsupportedEncodingException;
Import java.net. URLDecoder;
Import java.net. URLEncoder;
Import java. security. MessageDigest;
Import java. text. SimpleDateFormat;
Import java. util. Calendar;
Import javax. crypto. Cipher;
Import javax. crypto. SecretKey;
Import javax. crypto. SecretKeyFactory;
Import javax. crypto. spec. DESedeKeySpec;
Import sun. misc. BASE64Decoder;
Import sun. misc. BASE64Encoder;
/**
* <P> Title: encryption and decryption test </p>
*
* <P> Description: encryption and decryption </p>
*
* <P> Date: 2005-08-11 </p>
*
* <P> Copyright: Copyright (c) 2005 neusoft </p>
*
* <P> Company: neusoft </p>
*
* @ Author mengk
* @ Version 1.00
*
* <P> ------------------------------------------------------------ </p>
* <P> modification history </p>
* <P> reason for modifying the sequence number date modifier </p>
* <P> 1 </p>
*/
Public class Endecrypt {
/**
* Perform MD5 Encryption
* @ Param String original SPKEY
* @ Return byte [] specifies the md5 byte after the encryption method []
*/
Private byte [] md5 (String strSrc)
{
Byte [] returnByte = null;
Try
{
MessageDigest md5 = MessageDigest. getInstance ("MD5 ");
ReturnByte = md5.digest (strSrc. getBytes ("GBK "));
}
Catch (Exception e)
{
E. printStackTrace ();
}
Return returnByte;
}
/**
* Obtain the password of 3-DES.
* According to the interface specification, the key secret is set to 24 bytes, and md5 is encrypted to 16 bytes. Therefore, the next 8 bytes are supplemented with 0.
* @ Param String original SPKEY
* @ Return byte [] specifies the md5 byte after the encryption method []
*/
Private byte [] getEnKey (String spKey)
{
Byte [] cipher ey = null;
Try
{
Byte [] desKey1 = md5 (spKey );
Optional ey = new byte [24];
Int I = 0;
While (I <desKey1.length & I <24 ){
Required ey [I] = desKey1 [I];
I ++;
}
If (I <24 ){
Required ey [I] = 0;
I ++;
}
}
Catch (Exception e ){
E. printStackTrace ();
}
Return another ey;
}
/**
* 3-DES Encryption
* @ Param byte [] the byte [] That src needs to encrypt with 3-DES
* @ Param byte [] enKey 3-DES encryption key
* @ Return byte [] 3-DES encrypted byte []
*/
Public byte [] Encrypt (byte [] src, byte [] enKey)
{
Byte [] encryptedData = null;
Try
{
DESedeKeySpec dks = new DESedeKeySpec (enKey );
SecretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DESede ");
SecretKey key = keyFactory. generateSecret (dks );
Cipher cipher = Cipher. getInstance ("DESede ");
Cipher. init (Cipher. ENCRYPT_MODE, key );
EncryptedData = cipher. doFinal (src );
}
Catch (Exception e)
{
E. printStackTrace ();
}
Return encryptedData;
}
/**
* Base64 encoding of strings
* @ Param byte [] characters to be encoded by src
*
* @ Return String the encoded String
*/
Public String getBase64Encode (byte [] src)
{
String requestValue = "";
Try {
BASE64Encoder base64en = new BASE64Encoder ();
RequestValue = base64en. encode (src );
// System. out. println (requestValue );
}
Catch (Exception e ){
E. printStackTrace ();
}

Return requestValue;
}
/**
* Remove the newline character of the string.
* When base64 encoded 3-DES data, the resulting string contains a line break symbol.
*, You must remove it. Otherwise, the uni-wise platform will not successfully resolve the ticket root,
* "Sp Verification Failed" is displayed ". In the development process, I was helpless due to this problem,
* A friend told me that he could ask China Unicom for a piece of encrypted text and then compare it with the self-generated string,
* This is a good debugging method. I finally found that the only difference between the string I generated is that there is a line break.
* I used the c # language to write the ticket root request program. I did not find this problem.
*
*/
Private String filter (String str)
{
String output = null;
StringBuffer sb = new StringBuffer ();
For (int I = 0; I <str. length (); I ++)
{
Int asc = str. charAt (I );
If (asc! = 10 & asc! = 13)
Sb. append (str. subSequence (I, I + 1 ));
}
Output = new String (sb );
Return output;
}
/**
* Encode the string using URLDecoder. encode (strEncoding ).
* @ Param String src the String to be encoded
*
* @ Return String the encoded String
*/
Public String getURLEncode (String src)
{
String requestValue = "";
Try {

RequestValue = URLEncoder. encode (src );
}
Catch (Exception e ){
E. printStackTrace ();
}

Return requestValue;
}
/**
* 3-DES Encryption
* @ Param String src: String to be encrypted with 3-DES
* @ Param String spkey assigned by SPKEY
* @ Return String 3-DES encrypted String
*/
Public String get3DESEncrypt (String src, String spkey)
{
String requestValue = "";
Try {


// Obtain the password of 3-DES
Byte [] enKey = getEnKey (spkey );
// The content to be 3-DES encrypted is in byte \ "UTF-16LE \"
Byte [] src2 = src. getBytes ("UTF-16LE ");
// Bytes of the content encrypted with 3-DES
Byte [] encryptedData = Encrypt (src2, enKey );


// Perform BASE64 encoding for the 3-DES encrypted content
String base64String = getBase64Encode (encryptedData );
// BASE64 encoding after linefeed Removal
String base64Encrypt = filter (base64String );

// Escape the HTML control code in BASE64 encoding
RequestValue = getURLEncode (base64Encrypt );
// System. out. println (requestValue );
}
Catch (Exception e ){
E. printStackTrace ();
}

Return requestValue;
}
/**
* Perform URLDecoder. decode (strEncoding) decoding on the string
* @ Param String src the String to be decoded
*
* @ Return String the decoded String
*/
Public String getURLDecoderdecode (String src)
{
String requestValue = "";
Try {

RequestValue = URLDecoder. decode (src );
}
Catch (Exception e ){
E. printStackTrace ();
}

Return requestValue;
}
/**
*
* Perform 3-des decryption (the key is equivalent to the Encrypted Key ).
* @ Param byte [] src: Perform 3-DES decryption on byte []
* @ Param String spkey assigned by SPKEY
* @ Return String 3-DES the decrypted String
*/
Public String deCrypt (byte [] debase64, String spKey)
{
String strDe = null;
Cipher cipher = null;
Try
{
Cipher = Cipher. getInstance ("DESede ");
Byte [] key = getEnKey (spKey );
DESedeKeySpec dks = new DESedeKeySpec (key );
SecretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DESede ");
SecretKey sKey = keyFactory. generateSecret (dks );
Cipher. init (Cipher. DECRYPT_MODE, sKey );
Byte ciphertext [] = cipher. doFinal (debase64 );
StrDe = new String (ciphertext, "UTF-16LE ");
}
Catch (Exception ex)
{
StrDe = "";
Ex. printStackTrace ();
}
Return strDe;
}
/**
* 3-DES decryption
* @ Param String src: String to be decrypted by 3-DES
* @ Param String spkey assigned by SPKEY
* @ Return String 3-DES encrypted String
*/
Public String get3DESDecrypt (String src, String spkey)
{
String requestValue = "";
Try {


// Obtain the password of 3-DES

// URLDecoder. decodeTML control code escape Process
String URLValue = getURLDecoderdecode (src );

// Perform BASE64 encoding for the 3-DES encrypted content

BASE64Decoder base64Decode = new BASE64Decoder ();
Byte [] base64DValue = base64Decode. decodeBuffer (URLValue );

// The content to be 3-DES encrypted is in byte \ "UTF-16LE \"
RequestValue = deCrypt (base64DValue, spkey );
}
Catch (Exception e ){
E. printStackTrace ();
}
Return requestValue;
}
Public static void main (String [] args ){
Endecrypt test = new Endecrypt ();
String oldString = "toxin ";
String spkeys = "1234 ";
System. out. println ("1. Allocated SPKEY: "+ SPKEY );
System. out. println ("2. The content is: "+ oldString );
String reValue = test. get3DESEncrypt (oldString, SPKEY );
ReValue = reValue. trim (). intern ();
System. out. println ("content after 3-DES encryption:" + reValue );
String reValue2 = test. get3DESDecrypt (reValue, SPKEY );
System. out. println ("content after 3-DES decryption:" + reValue2 );
}
}

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.