Decrypt String Issue

S

-Steve-

I'm being passed a string that has been encrypted using 3DES. I'm writing a
function to decrypt that string.

All is well except I get garbarage before the decrypted string I'm supposed
to be retrieving.

For example I should get back 12345678, but instead I'm getting back
?l???#?K12345678 (everything before the real string is random though)
Anything jump out at anybody on what I should be doing?


Here is the function (that I stole of the web by the way) I'm using to
decrypt the string:

byte[] arrToDecrypt = Convert.FromBase64String(strToDecrypt);
// Create Key and IV
byte[] tdesKey = ASCIIEncoding.ASCII.GetBytes(key);

byte[] tdesIV = Encoding.ASCII.GetBytes(strToDecrypt.Substring(0, 8));
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
MemoryStream ms = new MemoryStream(arrToDecrypt);
CryptoStream decStream =
new CryptoStream(ms, tdes.CreateDecryptor(tdesKey, tdesIV),
CryptoStreamMode.Read);
StreamReader sr = new StreamReader(decStream, Encoding.ASCII);
return sr.ReadToEnd();
 
J

Jon Skeet [C# MVP]

-Steve- said:
I'm being passed a string that has been encrypted using 3DES. I'm writing a
function to decrypt that string.

All is well except I get garbarage before the decrypted string I'm supposed
to be retrieving.

You're using the string to decrypt as basis of the IV. That sounds very
odd to me. How are you encrypting this to start with, if you're using
the *result* of the encryption as one of the crypto parameters?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

(One thing to add: using ASCII to encode strings is generally short-
sighted. UTF-8 covers Unicode completely, without adding any overhead
for pure-ASCII strings. I'd also suggest using Base64 to represent the
key, rather than ASCII.
 
D

Damien

I'm being passed a string that has been encrypted using 3DES. I'm writing a
function to decrypt that string.

All is well except I get garbarage before the decrypted string I'm supposed
to be retrieving.

For example I should get back 12345678, but instead I'm getting back
?l???#?K12345678 (everything before the real string is random though)
Anything jump out at anybody on what I should be doing?

Here is the function (that I stole of the web by the way) I'm using to
decrypt the string:

byte[] arrToDecrypt = Convert.FromBase64String(strToDecrypt);
// Create Key and IV
byte[] tdesKey = ASCIIEncoding.ASCII.GetBytes(key);

byte[] tdesIV = Encoding.ASCII.GetBytes(strToDecrypt.Substring(0, 8));
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
MemoryStream ms = new MemoryStream(arrToDecrypt);
CryptoStream decStream =
new CryptoStream(ms, tdes.CreateDecryptor(tdesKey, tdesIV),
CryptoStreamMode.Read);
StreamReader sr = new StreamReader(decStream, Encoding.ASCII);
return sr.ReadToEnd();

Grab your IV from arrToDecrypt rather than through ASCII, as Jon
points out. And then REMOVE those 8 bytes from the array before you
decrypt the rest. At present, the first block that you're decrypting
is the IV.

Damien
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top