S
SenseiHitokiri
I have some code that converts a string into base64 for some
encryption.
It was written on the 1.1 framework but I am trying to get it to work
on 2.0. It throws exceptions on the cryptStream.FlushFinalBlock() and
cryptStream.Clear(). Can anyone tell me what is up with this?
Thanks ahead of time for any help you can offer.
public static string EncryptAndBase64( string val )
{
byte[] buf = UnicodeEncoding.Unicode.GetBytes(val);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(memStream, enc3des,
CryptoStreamMode.Write);
cryptStream.Write(buf, 0, buf.Length);
cryptStream.FlushFinalBlock();
cryptStream.Clear();
Array.Clear(buf, 0, buf.Length);
buf = memStream.GetBuffer();
val = Convert.ToBase64String(buf, 0, (int)memStream.Length);
Array.Clear(buf, 0, (int)memStream.Length);
return val;
}
public static object DecryptFromBase64( object val )
{
if( val.Equals( DBNull.Value ) )
return DBNull.Value;
return DecryptFromBase64( (string)val );
}
public static string DecryptFromBase64( string base64str )
{
byte[] buf = Convert.FromBase64String(base64str);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(memStream, dec3des,
CryptoStreamMode.Write );
cryptStream.Write(buf, 0, buf.Length);
cryptStream.FlushFinalBlock();
cryptStream.Clear();
Array.Clear(buf, 0, buf.Length);
buf = memStream.GetBuffer();
string ret = UnicodeEncoding.Unicode.GetString(buf, 0,
(int)memStream.Length);
Array.Clear(buf, 0, (int)memStream.Length);
memStream.Close();
return ret;
}
encryption.
It was written on the 1.1 framework but I am trying to get it to work
on 2.0. It throws exceptions on the cryptStream.FlushFinalBlock() and
cryptStream.Clear(). Can anyone tell me what is up with this?
Thanks ahead of time for any help you can offer.
public static string EncryptAndBase64( string val )
{
byte[] buf = UnicodeEncoding.Unicode.GetBytes(val);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(memStream, enc3des,
CryptoStreamMode.Write);
cryptStream.Write(buf, 0, buf.Length);
cryptStream.FlushFinalBlock();
cryptStream.Clear();
Array.Clear(buf, 0, buf.Length);
buf = memStream.GetBuffer();
val = Convert.ToBase64String(buf, 0, (int)memStream.Length);
Array.Clear(buf, 0, (int)memStream.Length);
return val;
}
public static object DecryptFromBase64( object val )
{
if( val.Equals( DBNull.Value ) )
return DBNull.Value;
return DecryptFromBase64( (string)val );
}
public static string DecryptFromBase64( string base64str )
{
byte[] buf = Convert.FromBase64String(base64str);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(memStream, dec3des,
CryptoStreamMode.Write );
cryptStream.Write(buf, 0, buf.Length);
cryptStream.FlushFinalBlock();
cryptStream.Clear();
Array.Clear(buf, 0, buf.Length);
buf = memStream.GetBuffer();
string ret = UnicodeEncoding.Unicode.GetString(buf, 0,
(int)memStream.Length);
Array.Clear(buf, 0, (int)memStream.Length);
memStream.Close();
return ret;
}