1. md5 encryption. the encryption algorithm is one-way encryption, that is, encrypted data cannot be restored through decryption. The related classes are included in the java. security. MessageDigest package.
2. 3-DES encryption. the encryption algorithm is reversible. The decryption party can decrypt the data by using the secret key agreed with the encryption party. The related classes are included in the javax. crypto. * package.
3. base64 encoding is the most common encoding method used to transmit 8-bit bytes of code. The related classes are in sun. misc. BASE64Decoder and sun. misc. BASE64Encoder.
4. URLEncoder encoding is a character encoding that ensures that transmitted parameters are composed of compliant texts. The related classes are in the java.net. URLEncoder package.
Details:
1. Perform MD5 encryption to obtain byte []
Copy codeThe Code is as follows:
/**
* 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;
}
2. Obtain the password of 3-DES.
Copy codeThe Code is as follows:
/**
* Obtain the password of 3-DES.
* As needed, if the key is set to 24 bytes, 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. 3-DES encryption
Copy codeThe Code is as follows:
/**
* 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;
}
4. encode the string with Base64
Copy codeThe Code is as follows:
/**
* 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;
}