SoFunction
Updated on 2025-04-07

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


package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .BASE64Decoder;
import .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> Modify history </p>
* <p> Serial number Date Modifier Reason for Modification</p>
* <p> 1 </p>
*/
public class Endecrypt {
/**
* Perform MD5 encryption
* @param String Original SPKEY
* @return byte[] Specify the encryption method of md5 byte[]
*/
private byte[] md5(String strSrc)
{
byte[] returnByte = null;
try
{
MessageDigest md5 = ("MD5");
returnByte = (("GBK"));
}
catch(Exception e)
{
();
}
return returnByte;
}
/**
* Get the key of 3-DES
* According to the interface specification, the key is 24 bytes, and md5 encrypted with 16 bytes, so the 8 bytes are added to the 0.
* @param String Original SPKEY
* @return byte[] Specify the encryption method of md5 byte[]
*/
private byte[] getEnKey(String spKey)
{
byte[] desKey=null;
try
{
byte[] desKey1 = md5(spKey);
desKey = new byte[24];
int i = 0;
while (i < && i < 24) {
desKey[i] = desKey1[i];
i++;
}
if (i < 24) {
desKey[i] = 0;
i++;
}
}
catch(Exception e){
();
}
return desKey;
}
/**
* 3-DES encryption
* @param byte[] src byte[] to perform 3-DES encryption
* @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 = ("DESede");
SecretKey key = (dks);
Cipher cipher = ("DESede");
(Cipher.ENCRYPT_MODE, key);
encryptedData = (src);
}
catch(Exception e)
{
();
}
return encryptedData;
}
/**
* Base64 encoding of strings
* @param byte[] src Characters to be encoded
*
* @return String Encoded string
*/
public String getBase64Encode(byte[] src)
{
String requestValue="";
try{
BASE64Encoder base64en = new BASE64Encoder();
requestValue=(src);
//(requestValue);
}
catch(Exception e){
();
}

return requestValue;
}
/**
* Remove the newline symbol of the string
* When base64 encodes 3-DES data, the obtained string has a newline symbol
* , it must be removed, otherwise the uni-wise platform parsing the ticket stub will not succeed.
* Prompt "sp verification failed". During the development process, I was helpless because of this problem.
* A friend told me that I could ask China Unicom to have an encrypted text and then compare it with the string generated by myself.
* This is a good debugging method. I finally compared and found that the only difference between the string I generated is that there are more line breaks.
* I also wrote the ticket stub request program in C# language and did not find this problem.
*
*/
private String filter(String str)
{
String output = null;
StringBuffer sb = new StringBuffer();
for(int i = 0; i < (); i++)
{
int asc = (i);
if(asc != 10 && asc != 13)
((i, i + 1));
}
output = new String(sb);
return output;
}
/**
* Encoding the string (strEncoding)
* @param String src string to encode
*
* @return String Encoded string
*/
public String getURLEncode(String src)
{
String requestValue="";
try{

requestValue = (src);
}
catch(Exception e){
();
}

return requestValue;
}
/**
* 3-DES encryption
* @param String src String to perform 3-DES encryption
* @param String spkey allocated SPKEY
* @return String 3-DES encrypted String
*/
public String get3DESEncrypt(String src,String spkey)
{
String requestValue="";
try{


//Get the key of 3-DES
byte[] enKey = getEnKey(spkey);
//The content to be encrypted with 3-DES is being performed and "UTF-16LE" is being used to get bytes
byte[] src2 = ("UTF-16LE");
//Bytes of content after 3-DES encryption
byte[] encryptedData = Encrypt(src2,enKey);


//Base64 encoding of content after 3-DES encryption
String base64String = getBase64Encode(encryptedData);
//After BASE64 encoding, remove line breaks,
String base64Encrypt = filter(base64String);

//The process of escaping HTML control code in BASE64 encoding
requestValue=getURLEncode(base64Encrypt);
//(requestValue);
}
catch(Exception e){
();
}

return requestValue;
}
/**
* Decode the string (strEncoding)
* @param String src string to decode
*
* @return String Decoded string
*/
public String getURLDecoderdecode(String src)
{
String requestValue="";
try{

requestValue = (src);
}
catch(Exception e){
();
}

return requestValue;
}
/**
*
* Perform 3-DES decryption (the key is equivalent to the encrypted key).
* @param byte[] src To perform 3-DES decryption byte[]
* @param String spkey allocated SPKEY
* @return String 3-DES decrypted String
*/
public String deCrypt(byte[] debase64,String spKey)
{
String strDe = null;
Cipher cipher = null;
try
{
cipher=("DESede");
byte[] key = getEnKey(spKey);
DESedeKeySpec dks = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = ("DESede");
SecretKey sKey = (dks);
(Cipher.DECRYPT_MODE, sKey);
byte ciphertext[] = (debase64);
strDe = new String(ciphertext,"UTF-16LE");
}
catch(Exception ex)
{
strDe = "";
();
}
return strDe;
}
/**
* 3-DES decryption
* @param String src String to perform 3-DES decryption
* @param String spkey allocated SPKEY
* @return String 3-DES encrypted String
*/
public String get3DESDecrypt(String src,String spkey)
{
String requestValue="";
try{


//Get the key of 3-DES

//The process of escaping control code
String URLValue=getURLDecoderdecode(src);

//Base64 encoding of content after 3-DES encryption

BASE64Decoder base64Decode = new BASE64Decoder();
byte[] base64DValue = (URLValue);

//The content to be encrypted with 3-DES is being performed and "UTF-16LE" is being used to get bytes
requestValue = deCrypt(base64DValue,spkey);
}
catch(Exception e){
();
}
return requestValue;
}
public static void main(String[] args) {
Endecrypt test = new Endecrypt();
String oldString = "toxin hair";
String SPKEY = "1234";
("1. The allocated SPKEY is: "+SPKEY);
("2. The content is: "+oldString);
String reValue = test.get3DESEncrypt(oldString,SPKEY);
reValue = ().intern();
("Content after 3-DES encryption: "+reValue);
String reValue2 = test.get3DESDecrypt(reValue,SPKEY);
("Content after 3-DES decryption: "+reValue2);
}
}