problem storing encrypted string to database
From: Tom Regan (tregan3_at_hotmail.com)
Date: 11/02/04
- Next message: Nicole Calinoiu: "Re: problem storing encrypted string to database"
- Previous message: Marius Groenendijk: "Re: Check for permission does not work"
- Next in thread: Nicole Calinoiu: "Re: problem storing encrypted string to database"
- Reply: Nicole Calinoiu: "Re: problem storing encrypted string to database"
- Reply: Hernan de Lahitte: "Re: problem storing encrypted string to database"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
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();
}
}
}
- Next message: Nicole Calinoiu: "Re: problem storing encrypted string to database"
- Previous message: Marius Groenendijk: "Re: Check for permission does not work"
- Next in thread: Nicole Calinoiu: "Re: problem storing encrypted string to database"
- Reply: Nicole Calinoiu: "Re: problem storing encrypted string to database"
- Reply: Hernan de Lahitte: "Re: problem storing encrypted string to database"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|