Decrypting Java des encrypted strings using C # des

Source: Internet
Author: User
Tags decrypt

Go from Microallen recently need to use C # des decryption tool class to decrypt the string, but to decrypt the string is to use Java for DES encryption, to the online check on the C # and Java on the DES Encryption and decryption of the data, found that can encrypt each other decryption, Java for DES encryption will generally be written as follows:
public static byte[] Encrypt (string message, String key) throws Exception {
Cipher Cipher = cipher.getinstance ("des/cbc/pkcs5padding");

Deskeyspec Deskeyspec = new Deskeyspec (key.getbytes ("UTF-8"));

Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");
Secret key
Secretkey Secretkey = Keyfactory.generatesecret (Deskeyspec);
Offset amount
Ivparameterspec IV = new Ivparameterspec (key.getbytes ("UTF-8"));
Cipher.init (Cipher.encrypt_mode, Secretkey, iv);

Return cipher.dofinal (Message.getbytes ("UTF-8"));
}

Because the key and offset are set when initializing the cipher, the DESCryptoServiceProvider class of C # can be easily decrypted. The C # decryption code is as follows:
public static string Decode (String str, string key)
{
Try
{

DESCryptoServiceProvider Provider = new DESCryptoServiceProvider ();
Secret key
Provider. Key = Encoding.ASCII.GetBytes (key. Substring (0, 8));
Offset amount
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 ();
Return encoding.getencoding ("GB2312"). GetString (stream. ToArray ());
}
catch (Exception) {return "";}
}

Now the problem is that Java's DES encryption has more than one notation, and the string I need to decrypt is encrypted using the following Java code:
public static byte[] Encrypt (string message, String key) throws Exception {
securerandom sr = new SecureRandom ();
Cipher Cipher = cipher.getinstance ("DES");

Deskeyspec Deskeyspec = new Deskeyspec (key.getbytes ("UTF-8"));

Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");
Secretkey Secretkey = Keyfactory.generatesecret (Deskeyspec);
Another init method is used here.
Cipher.init (Cipher.encrypt_mode, Secretkey, SR);

Return cipher.dofinal (Message.getbytes ("UTF-8"));
}
In this case, the Java encryption offset is not set, and the test finds that even if the offset is set above, Cipher Cipher = cipher.getinstance ("DES"), and Cipher Cipher = cipher.getinstance ("des/cbc/pkcs5padding"); The results are different after encryption,
So the use of C # decryption when you do not know the corresponding offset should be how to set, trouble you master guidance, thank you

Supplementary questions:

DES encryption and decryption results generally note the place: Key, offset, block cipher mode, fill mode Java DES encryption when: If you use this method, Cipher Cipher = cipher.getinstance ("DES"); At this time block cipher mode with the ECB mode, the C#des class default mode is CBC mode, so if Java uses the above method to initialize, when using C # decryption to remember to set the Mode property to the ECB, in addition, if there is no set offset, C # When decrypting the key and IV are set to the same can be normal decryption. So for the 2nd des encryption method above Java, use C # decryption only need to add provider before decryption. Mode = CIPHERMODE.ECB; it's ready.

Decrypting Java des encrypted strings using C # des

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.