Re: Decrypt problem using Rijndael



you cannot use Unicode to convert between bytes and strings - replace all the conversions with Convert.From/To Base64String

---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com

Hi guys

I'm using an algo to encrypt/decrypt an xml file using Rijndael.
However, SOMETIMES (yea that's right, it does not happen all the
time) the decrypt algo fails when I try to read the data in
cryptostream:

'Read the data out of the crypto stream.
csDecrypt.Read(btOriginalData, 0, btOriginalData.Length)
with the System.Security.Cryptography.CryptographicException "Length
of the data to decrypt is invalid"

I've spent hours unsuccessfully trying to know what's the problem.
Everything seems ok. Here's the code snippet for decrypt. Can anyone
help please?

' Retrieves Key, IV, and data from encrypted file
srKeyFile = New System.IO.StreamReader(DecryptKeyFile,
System.Text.Encoding.Unicode)
sKey = srKeyFile.ReadLine()
sKeyByte = EncUnicode.GetBytes(sKey)
sIV = srKeyFile.ReadLine()
sIVByte = EncUnicode.GetBytes(sIV)
sData = srKeyFile.ReadLine()
sDataByte = EncUnicode.GetBytes(sData)
'Get a decryptor that uses the same key and IV as the
encryptor.
objDecryptor = myRijndael.CreateDecryptor(sKeyByte, sIVByte)
'Now decrypt the previously encrypted message using the
decryptor
obtained.
msDecrypt = New System.IO.MemoryStream(sDataByte)
csDecrypt = New
System.Security.Cryptography.CryptoStream(msDecrypt,
objDecryptor, _

System.Security.Cryptography.CryptoStreamMode.Read)
btOriginalData = New Byte(sDataByte.Length) {}

'Read the data out of the crypto stream. EXCEPTION ON THIS LINE
csDecrypt.Read(btOriginalData, 0, btOriginalData.Length)
'Convert the byte array back into a string.
sXML = EncASCII.GetString(btOriginalData).Replace("&", "&")
Thanks
Jeffrey


.



Relevant Pages

  • Re: Encrypt/Decrypt Files
    ... I'm having problem with my simple encrypt/decrypt program. ... of my program is to encrypt a text file into a binary file ... What is 'sTmpString' pointing to? ... use this instead of the previous line, it will encrypt and decrypt ...
    (comp.os.msdos.programmer)
  • Encryption/Decryption on Pocket PC
    ... I can't get the Encrypt and Decrypt functions to work when I convert strings to byte arrays and pass them in the functions. ... Dim byteEncrypted() As Byte ...
    (microsoft.public.pocketpc.developer)
  • Decrypt problem using Rijndael
    ... I'm using an algo to encrypt/decrypt an xml file using Rijndael. ... However, SOMETIMES the decrypt algo fails when I try to read the data in cryptostream: ...
    (microsoft.public.dotnet.security)
  • RE: Decryption using private key from cert store failing with err
    ... The reason we need to do a decrypt/encryptis that that is ... It decrypts with the private key and then the ... The other libraries encrypt/decrypt calls do not ... > invalid cryptograph to decrypt, otherwise we will get invalid data ...
    (microsoft.public.platformsdk.security)
  • Re: Java encryption
    ... SQL ... Server 2005 gives no problems what so ever with storing Java encrypted ... Strings and allowing you to read them out an decrypt them. ...
    (comp.lang.java.programmer)