The full text of the procedure is as follows:
Copy Code code 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) neusoft</p>
*
* <p>Company:neusoft</p>
*
* @author Mengk
* @version 1.00
*
* <p>------------------------------------------------------------</p>
* <p> Modify History </p>
* <p> Serial Date modification reason </p>
* <p> 1 </p>
*/
public class Endecrypt {
/**
* For MD5 encryption
* @param String Original Spkey
* @return byte[] Specifies the byte[of the encryption method after MD5]
*/
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;
}
/**
* Get the 3-des key
* According to the interface specification, the secret key is 24 bytes, and the MD5 is 16 bytes, so the 0 of the following 8 bytes
* @param String Original Spkey
* @return byte[] Specifies the byte[of the encryption method after MD5]
*/
Private byte[] Getenkey (String spkey)
{
Byte[] Deskey=null;
Try
{
Byte[] DesKey1 = MD5 (Spkey);
Deskey = new BYTE[24];
int i = 0;
while (I < deskey1.length && I < 24) {
Deskey[i] = Deskey1[i];
i++;
}
if (I < 24) {
Deskey[i] = 0;
i++;
}
}
catch (Exception e) {
E.printstacktrace ();
}
return deskey;
}
/**
* 3-des Encryption
* @param byte[] src to be 3-des encrypted byte[]
* @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[] src characters to encode
*
* String encoded by @return 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 newline symbol for string
* Base64 encoded 3-des data, the resulting string has a newline symbol
*, must be removed, otherwise the Uni-wise platform resolution stub will not succeed,
* Prompt for SP validation failure. In the process of development, because this problem let me at my wits ' end,
* A friend told me to ask Unicom to have an encrypted text, and then to compare it to the string you generated,
* This is a good way to debug. My last comparison found that the only difference between the strings I generated was the number of newline.
* I also wrote a stub request in C # language, and I didn't 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!= && ASC!= 13)
Sb.append (Str.subsequence (i, i + 1));
}
Output = new String (SB);
return output;
}
/**
* Urldecoder.encode (strencoding) Encoding of strings
* @param string src strings to encode
*
* String encoded by @return 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 3-des encrypted
* @param String Spkey Allocated Spkey
* @return String 3-des the encrypted string
*/
public string Get3desencrypt (String src,string spkey)
{
String requestvalue= "";
try{
Get 3-des's secret key.
byte[] Enkey = Getenkey (Spkey);
The content to be 3-des encrypted is in the \ "Utf-16le\" byte
byte[] Src2 = src.getbytes ("Utf-16le");
Bytes for 3-des encrypted content
byte[] EncryptedData = Encrypt (Src2,enkey);
BASE64 encoding for 3-des encrypted content
String base64string = Getbase64encode (EncryptedData);
BASE64 code to remove line breaks
String Base64encrypt = filter (base64string);
The process of escaping the HTML control code in the BASE64 encoding
Requestvalue=geturlencode (Base64encrypt);
System.out.println (Requestvalue);
}
catch (Exception e) {
E.printstacktrace ();
}
return requestvalue;
}
/**
* Urldecoder.decode (strencoding) decoding of strings
* @param string src strings to be decoded
*
* The string to decode @return string
*/
public string Geturldecoderdecode (string src)
{
String requestvalue= "";
try{
Requestvalue = Urldecoder.decode (src);
}
catch (Exception e) {
E.printstacktrace ();
}
return requestvalue;
}
/**
*
* 3-des decryption (The secret key is equivalent to the encrypted key).
* @param byte[] src to be 3-des decrypted byte[]
* @param String Spkey Allocated 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 3-des decrypted
* @param String Spkey Allocated Spkey
* @return String 3-des the encrypted string
*/
public string Get3desdecrypt (String src,string spkey)
{
String requestvalue= "";
try{
Get 3-des's secret key.
The process of escaping urldecoder.decodetml control code
String Urlvalue=geturldecoderdecode (SRC);
BASE64 encoding for 3-des encrypted content
Base64decoder Base64decode = new Base64decoder ();
byte[] Base64dvalue = Base64decode.decodebuffer (Urlvalue);
The content to be 3-des encrypted is in the \ "Utf-16le\" byte
Requestvalue = DeCrypt (Base64dvalue,spkey);
}
catch (Exception e) {
E.printstacktrace ();
}
return requestvalue;
}
public static void Main (string[] args) {
Endecrypt test = new Endecrypt ();
String oldstring = "toxin hair";
String Spkey = "1234";
System.out.println ("1"). The assigned Spkey is: "+spkey";
System.out.println ("2"). The content is: "+oldstring);
String revalue = Test.get3desencrypt (Oldstring,spkey);
Revalue = Revalue.trim (). Intern ();
System.out.println ("3-des after the encrypted content:" +revalue);
String reValue2 = Test.get3desdecrypt (Revalue,spkey);
System.out.println ("3-des after the decryption of the content:" +revalue2);
}
}