Asp.net and java are used to encrypt and decrypt des, asp. netjavades
Recently, a new project was developed in java. The old project is asp.net, and the interface transmission requires des encryption and decryption. I checked some information on the Internet and most of the information can't be used, after debugging and processing, the specific code is as follows:
The key must be 8 bits.
/// <Summary> /// use the DES encryption algorithm to encrypt the string (decrypted) /// </summary> /// <param name = "pToEncrypt"> encrypted string </param> /// <param name = "key"> key (only supports 8-byte keys) </param> // <returns> the encrypted string </returns> public static string DESEnCode (string pToEncrypt, string key) {try {DESCryptoServiceProvider provider = new DESCryptoServiceProvider (); provider. key = Encoding. ASCII. getBytes (key. substring (0, 8); provider. IV = Encoding. ASCII. getBytes (key. substring (0, 8); byte [] bytes = Encoding. getEncoding ("GB2312 "). getBytes (pToEncrypt); MemoryStream stream = new MemoryStream (); CryptoStream stream2 = new CryptoStream (stream, provider. createEncryptor (), CryptoStreamMode. write); stream2.Write (bytes, 0, bytes. length); stream2.FlushFinalBlock (); StringBuilder builder = new StringBuilder (); foreach (byte num in stream. toArray () {builder. appendFormat ("{0: X2}", num);} stream. close (); return builder. toString () ;}catch (Exception) {return "xxxx ";}} /// <summary> /// decrypt /// </summary> /// <param name = "plaintext"> encrypted string </param> /// <param name = "key"> key (only 8-byte key is supported) </param> // <returns> decrypted string </returns> public static string Decode (string str, string key, string encLangue) {try {// str = Ruijie. pcfg. utils. DESEncrypt. hexTostring (str); DESCryptoServiceProvider provider = new DESCryptoServiceProvider (); provider. key = Encoding. ASCII. getBytes (key. substring (0, 8); provider. IV = Encoding. ASCII. getBytes (key. substring (0, 8); byte [] buffer = new byte [str. length/2]; for (int I = 0; I <(str. length/2); I ++) {int num2 = Convert. toInt32 (str. substring (I * 2, 2), 0x10); buffer [I] = (byte) num2;} MemoryStream stream = new MemoryStream (); cryptoStream stream2 = new CryptoStream (stream, provider. createDecryptor (), CryptoStreamMode. write); stream2.Write (buffer, 0, buffer. length); stream2.FlushFinalBlock (); stream. close (); if (encLangue = "java") {return Encoding. getEncoding ("UTF-8 "). getString (stream. toArray ();} else {return Encoding. getEncoding ("gb2312 "). getString (stream. toArray () ;}} catch (Exception) {return "";}}
The corresponding java method is as follows:
Package com. testspring;
Import javax. crypto. Cipher;
Import javax. crypto. SecretKey;
Import javax. crypto. SecretKeyFactory;
Import javax. crypto. spec. DESKeySpec;
Import javax. crypto. spec. IvParameterSpec;
Public class DesHelper {
/**
* Encryption
*
*
***/
Public String encrypt (String message, String key)
{
Return toHexString (encryptByte (message, key). toUpperCase ();
}
/**
* Plaintext encrypted Array
*
*
***/
Public byte [] encryptByte (String message, String key ){
Byte [] s = {};
Try
{
Cipher cipher = Cipher. getInstance ("DES/CBC/PKCS5Padding ");
DESKeySpec desKeySpec = new DESKeySpec (key. getBytes ("UTF-8 "));
SecretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DES ");
SecretKey secretKey = keyFactory. generateSecret (desKeySpec );
IvParameterSpec iv = new IvParameterSpec (key. getBytes ("UTF-8 "));
Cipher. init (Cipher. ENCRYPT_MODE, secretKey, iv );
Return cipher. doFinal (message. getBytes ("UTF-8 "));
}
Catch (Exception ex ){
}
Return s;
}
/**
* Convert an array to hexadecimal format
*
*
***/
Public static String toHexString (byte B []) {
StringBuffer hexString = new StringBuffer ();
For (int I = 0; I <B. length; I ++ ){
String plainText = Integer. toHexString (0xff & B [I]);
If (plainText. length () <2)
PlainText = "0" + plainText;
HexString. append (plainText );
}
Return hexString. toString ();
}
/**
* Decryption
* Ciphertext: Encrypted string, key, and encLangue
*
***/
Public String decrypt (String ciphertext, String key, String encLangue ){
Try {
Byte [] bytesrc = convertHexString (ciphertext );
Cipher cipher = Cipher. getInstance ("DES/CBC/PKCS5Padding ");
DESKeySpec desKeySpec = new DESKeySpec (key. getBytes ("UTF-8 "));
SecretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DES ");
SecretKey secretKey = keyFactory. generateSecret (desKeySpec );
IvParameterSpec iv = new IvParameterSpec (key. getBytes ("UTF-8 "));
Cipher. init (Cipher. DECRYPT_MODE, secretKey, iv );
Byte [] retByte = cipher. doFinal (bytesrc );
If (encLangue = "java ")
{
Return new String (retByte, "UTF-8 ");
}
Else
{
Return new String (retByte );
}
}
Catch (Exception ex)
{
}
Return "";
}
/**
* Convert a hexadecimal string to a byte array.
*
***/
Public static byte [] convertHexString (String ss ){
Byte digest [] = new byte [ss. length ()/2];
For (int I = 0; I <digest. length; I ++ ){
String byteString = ss. substring (2 * I, 2 * I + 2 );
Int byteValue = Integer. parseInt (byteString, 16 );
Digest [I] = (byte) byteValue;
}
Return digest;
}
}