Cannot use Java generated DES Key in C#
-
Greetings, I am developing an application that reads a DES encrypted password generated using Java in a properties file and decrypts it, using DESCryptoServiceProvider. I am provided with the key string originally generated with Java using DES. Does anyone have an example or sample code that converts/uses a Java generated key to decrypt a Java generated password in C#? After removing the dashes, I get the "Specified key is not a valid size for this algorithm" exception. I've tried both of these lines of code and get the same error: static byte[] key = ASCIIEncoding.ASCII.GetBytes(dashKey.Replace("-", "")); OR static byte[] key = Encoding.ASCII.GetBytes(dashKey.Replace("-", "")); The error occurrs at either of the below lines of code I tried: cryptoProvider.Key = key; OR ICryptoTransform decryptor = cryptoProvider.CreateDecryptor(key, iv); Thanks, Karl
-
Greetings, I am developing an application that reads a DES encrypted password generated using Java in a properties file and decrypts it, using DESCryptoServiceProvider. I am provided with the key string originally generated with Java using DES. Does anyone have an example or sample code that converts/uses a Java generated key to decrypt a Java generated password in C#? After removing the dashes, I get the "Specified key is not a valid size for this algorithm" exception. I've tried both of these lines of code and get the same error: static byte[] key = ASCIIEncoding.ASCII.GetBytes(dashKey.Replace("-", "")); OR static byte[] key = Encoding.ASCII.GetBytes(dashKey.Replace("-", "")); The error occurrs at either of the below lines of code I tried: cryptoProvider.Key = key; OR ICryptoTransform decryptor = cryptoProvider.CreateDecryptor(key, iv); Thanks, Karl
Have you tried with different
CipherMode
s? Are you sure you are not removing to many dashes? Calin -
Greetings, I am developing an application that reads a DES encrypted password generated using Java in a properties file and decrypts it, using DESCryptoServiceProvider. I am provided with the key string originally generated with Java using DES. Does anyone have an example or sample code that converts/uses a Java generated key to decrypt a Java generated password in C#? After removing the dashes, I get the "Specified key is not a valid size for this algorithm" exception. I've tried both of these lines of code and get the same error: static byte[] key = ASCIIEncoding.ASCII.GetBytes(dashKey.Replace("-", "")); OR static byte[] key = Encoding.ASCII.GetBytes(dashKey.Replace("-", "")); The error occurrs at either of the below lines of code I tried: cryptoProvider.Key = key; OR ICryptoTransform decryptor = cryptoProvider.CreateDecryptor(key, iv); Thanks, Karl
I figured it out. The key needed to be converted into an 8 byte array.
public static byte[] toBytes(string text)
{
char[] dash = { '-', '\n', '\r' };
String[] values = text.Split(dash);
byte[] keyArray = new byte[8];
for (int i = 0; i < values.Length; i++)
{
int elem = Convert.ToInt32(values[i]);
elem = elem & 0x00FF;
keyArray[i] = (byte)elem;
}
return keyArray;
}Thanx, Karl