locked
Length of the data to decrypt is invalid. cryptostream -Issue with long string RRS feed

  • Question

  • User293174396 posted

    Hi

    My encryption code is working fine for all data and all data are being encrypted But during decryption, the code decrypt all the data except the long strings.

    Why only long string does not get decrypted.

    My code are as follows

    static

    byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");

    public

    static string Encrypt(stringoriginalString)

    {

    if (String.IsNullOrEmpty(originalString))

    {

    return null;

    }

    else

    {

    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

    MemoryStream memoryStream = new MemoryStream();

    CryptoStream cryptoStream = new CryptoStream(memoryStream,

    cryptoProvider.CreateEncryptor(bytes, bytes),

    CryptoStreamMode.Write);

    StreamWriter writer = new StreamWriter(cryptoStream);

    writer.Write(originalString);

    writer.Flush();

    cryptoStream.FlushFinalBlock();

    writer.Flush();

    return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);

    }

    }

    ********************************

    public

    string Decrypt(stringcryptedString)

    {

    String rval = cryptedString;

    try

    {

    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

    MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString));

    CryptoStream cryptoStream = new CryptoStream(memoryStream,

    cryptoProvider.CreateDecryptor(bytes, bytes),

    CryptoStreamMode.Read);

    StreamReader reader = new StreamReader(cryptoStream);

    rval = reader.ReadToEnd();

    }

    catch (Exception ex)

    {

    }

    return rval;

    }

    Wednesday, October 10, 2012 11:53 AM

All replies

  • User1779161005 posted

    I don't really see anywhere you're managing the keys needed for encryption or decryption. You need a key when doing crypto. Here are some crypto samples that might help:

    http://sdrv.ms/L51R0e

    Wednesday, October 10, 2012 12:04 PM
  • User293174396 posted

    I am using

     

    byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");

    CryptoStream

    cryptoStream = new CryptoStream(memoryStream,

    cryptoProvider.CreateDecryptor(bytes, bytes),

    CryptoStreamMode.Read);

    bytes is passed as key.

    And the issue is this code is working for all data except long strings, example memo field. All other types of fields are working properly.

     

    Wednesday, October 10, 2012 12:40 PM
  • User293174396 posted

    Can anyone not find answer to my query....

    Thursday, October 11, 2012 8:27 AM