Decrypt problem using Rijndael



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)
  • Re: Decrypt problem using Rijndael
    ... you cannot use Unicode to convert between bytes and strings - replace all the conversions with Convert.From/To Base64String ... I'm using an algo to encrypt/decrypt an xml file using Rijndael. ... of the data to decrypt is invalid" ...
    (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: Failed to decrypt
    ... If you are using the new encrypt/decrypt methods of the IO.File be aware that only the user that encrypts can decrypt. ... "Failed to decrypt using provider 'RsaProtectedConfigurationProvider'. ... Error message from the provider. ...
    (microsoft.public.dotnet.languages.vb)
  • Encrypting/Decrypting XML Documents with Asymmetric Keys
    ... I had a question about encrypting and decrypting XML files using asymmetric ... wasn't because my ASPNET user wasn't able to decrypt the XML file that I ...
    (microsoft.public.dotnet.framework.aspnet)