problem storing encrypted string to database

From: Tom Regan (tregan3_at_hotmail.com)
Date: 11/02/04


Date: 2 Nov 2004 05:04:50 -0800

I need to encrypt a string, store it in a database varchar field, then
later fetch the string from the database and decrypt it.

I can encrypt to a byte array and then decrypt the byte array no
problem. But if I change the byte array to a string, then change that
string back to a byte array, all hell breaks loose (I get the error
"Bad Data"). Below is a complete console app that demonstrates my
problem, I'd appreciate any advice.

using System;
using System.Security;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace EncryptDecrypt
{
class CryptoSample8
{
private const string Key = "abcd1234";
private static readonly byte[] DesKey =
ASCIIEncoding.ASCII.GetBytes(Key);
private static readonly byte[] DesIV =
ASCIIEncoding.ASCII.GetBytes(Key);

public static void Main()
{
string Msg="This is the message that gets encrypted.";
byte[] b = ASCIIEncoding.ASCII.GetBytes(Msg);
MemoryStream ms = new MemoryStream(b.Length);
DES des = new DESCryptoServiceProvider() ;

CryptoStream encStream = new
CryptoStream(ms,des.CreateEncryptor(DesKey,
DesIV),CryptoStreamMode.Write);
encStream.Write(b,0,b.Length);
encStream.FlushFinalBlock();

b = new byte[ms.Position];
ms.Position = 0;
b=ms.ToArray();
string EncryptedMsg=ASCIIEncoding.ASCII.GetString(b,0,(int)b.Length);

//here I would store "EncryptedMsg" in a database.
//later I want to pull "EncryptedMsg" from the database
//and decrypt it

//comment out the next line and it works.
//in other words, the byte array is OK if I don't change it to
"EncryptedMsg"
//changing the byte array to "EncryptedMsg" breaks something.
b=ASCIIEncoding.ASCII.GetBytes(EncryptedMsg);
ms = new MemoryStream(b.Length);
des = new DESCryptoServiceProvider() ;

encStream = new CryptoStream(ms,des.CreateDecryptor(DesKey,
DesIV),CryptoStreamMode.Read);
ms.Write(b,0,b.Length);
ms.Position = 0;
string DecryptedMsg = new StreamReader(encStream).ReadToEnd();
encStream.Close();
}
}
}



Relevant Pages

  • Re: a problem with encryption
    ... >> That way I always get the original data I've encrypted. ... I really don't know a way to know how long the string I send to ... > encrypted data in a byte array trough network stream. ... to decrypt in one call, ...
    (microsoft.public.dotnet.general)
  • Re: problem storing encrypted string to database
    ... string type. ... In order to avoid this conversion issue, you may base64 encode the byte ... array with this function and do the reverse process ... > //here I would store "EncryptedMsg" in a database. ...
    (microsoft.public.dotnet.security)
  • Re: DES Decrypt Not Working
    ... > to decrypt it, it returns the exact same byte array that I passed to ... > Function DecryptData(ByVal bData() As Byte) As String ...
    (microsoft.public.dotnet.general)
  • Re: RijndaelManaged decryption leaves strange characters
    ... When you convert the array of decrypted bytes to string, ... how many plain text bytes were returned, only convert these bytes to string. ... > Encrypt the data using RijndaelManaged in CBC Mode. ... > Decrypt using RijndaelManaged in CBC Mode ...
    (microsoft.public.dotnet.security)
  • Re: Java string encryption/decryption
    ... encrypted byte-array to a String before decrypting it. ... I'm trying to decrypt. ... The cyphertext will not make sense as a String and should not be ... Convert the plaintext to a byte array. ...
    (comp.lang.java.programmer)