This DES encryption and decryption between ASP.

Source: Internet
Author: User

Recently, there is a new project to do with Java, the old project is ASP, interface transmission needs des plus decryption, online search of some information, most can not be used, after debugging and processing, the specific code is as follows:

The key must be 8 bits.

        <summary>/////Use DES encryption algorithm to encrypt strings (can be decrypted)///</summary>//<param name= "Ptoencrypt" > Encrypted string </param>//<param name= "key" > key (only 8-byte key is supported) </param>//&lt            ;returns> Encrypted strings </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>//decryption///</summary>/<param name= "plaintext" > Plus Secret string </param>//<param name= "key" > Key (8-byte key only) </param>//<returns> decrypted Word             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 ();
}
/**
* The array after the plaintext is encrypted
*
*
* **/
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 arrays into 16 binary
*
*
* **/
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 key, Enclangue encryption language
*
* **/
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 16 binary string to 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;
}

}

This DES encryption and decryption between ASP.

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.