Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
CryptoStream class and Memory Streams
Message
De
29/10/2003 11:34:19
 
 
À
29/10/2003 10:22:05
Information générale
Forum:
ASP.NET
Catégorie:
Securité
Divers
Thread ID:
00844087
Message ID:
00844118
Vues:
18
Bill,

Here's a few code snippets from what we've been using, where "parm" is a string:
MemoryStream InStream  = new MemoryStream();
MemoryStream OutStream = new MemoryStream();

InStream.Write(System.Text.Encoding.Default.GetBytes(parm), 0, System.Text.Encoding.Default.GetBytes(parm).Length);
InStream.Position = 0;

byte[] buf = new byte[2048];

// Then get your algorithms, and your keys and your CryptoStream as you usually do
SymmetricAlgorith sa = SymmetricAlgorith.Create("whatever algorithm name");
sa.IV = System.Text.Encoding.Default.GetBytes(genIV);
sa.Key = GetKey();

ICryptoTransform ct = sa.CreateEncryptor();
CryptoStream cs = new CryptoStream(OutStream, ct, CryptoStreamMode.Write);

// now process your buffer
int i = InStream.Read(buf, 0, buf.Length);
while (i > 0)
{
  cs.Write(buf, 0, i);
  i = InStream.Read(buf, 0, buf.Length);
}

cs.FlushFinalBlock();

string EncryptedData = System.Convert.ToBase64String(OutStream.ToArray());

// then close all streams, etc.
HTH,
~~Bonnie

>Many of the cryptography API examples I have run into use the CryptoStream class and file streams to encrypt/decrypt files.
>
>I have been able to use file streams with the CryptoStream class successfully. However, in the scenario in which encrypted data is held in a string variable, I would like to use a MemoryStream in conjunction with the CryptoStream class to decrypt the encrypted string. In attempting this I have been getting a "Bad Data" error when the decryption takes place. Has anyone used memory streams with the CryptoStream class and is there some particular way in which the data must be loaded from a string variable into the memory stream in order to be in proper form to be operated on by the CryptoStream class?
>
>Thanks,
>Bill
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform