I'm new to using this part of the framework, so I'm hoping I've done
something obviously stupid, which someone will be able to point out in
an obvious manner.
Most of the samples I've seen involved encrypting and decrypting to and
from a file, but that's not what I want. I want to be able to insert a
string into an encryption function which outputs that encrypted string,
which could then be sent into a decryption function and spit out the
original string.
As you might expect, ultimately, I want to be able to put this string
into a database table.
Anyways, I set up a simple test page where I enter a string in a
textbox, and then run it through an encrypt, then decrypt, function, and
print out to some labels to make sure it is doing it correctly.
The encrypt part seems to work (at least the function doesn't fail), but
the decrypt function fails, with an error saying that the length of the
encrypted string is invalid. Here are the functions:
*************** *************** *************** *************** **
public static string Encrypt(string StringToEncrypt ){
string EncryptedString ;
UTF8Encoding utf8encoder = new UTF8Encoding();
byte[] inputInBytes = utf8encoder.Get Bytes(StringToE ncrypt);
TripleDESCrypto ServiceProvider tdesProvider = new
TripleDESCrypto ServiceProvider ();
ICryptoTransfor m cryptoTransform =
tdesProvider.Cr eateEncryptor(t deskey, tdesIV);
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(en cryptedStream,
cryptoTransform , CryptoStreamMod e.Write);
cryptStream.Wri te(inputInBytes , 0, inputInBytes.Le ngth);
cryptStream.Flu shFinalBlock();
encryptedStream .Position = 0;
byte[] result = new byte[encryptedStream .Length - 1];
encryptedStream .Read(result, 0, (int)(encrypted Stream.Length - 1));
cryptStream.Clo se();
UTF8Encoding myutf = new UTF8Encoding();
EncryptedString = myutf.GetString (result);
return EncryptedString ;
}//end Encrypt
public static string Decrypt(string StringToDecrypt ){
string DecryptedString ;
UTF8Encoding utf8encoder = new UTF8Encoding();
byte[] inputInBytes = utf8encoder.Get Bytes(StringToD ecrypt);
TripleDESCrypto ServiceProvider tdesProvider = new
TripleDESCrypto ServiceProvider ();
ICryptoTransfor m cryptoTransform =
tdesProvider.Cr eateDecryptor(t deskey, tdesIV);
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(de cryptedStream,
cryptoTransform , CryptoStreamMod e.Write);
cryptStream.Wri te(inputInBytes , 0, (inputInBytes.L ength));
cryptStream.Flu shFinalBlock();
decryptedStream .Position = 0;
byte[] result = new byte[decryptedStream .Length - 1];
decryptedStream .Read(result, 0, (int)(decrypted Stream.Length - 1));
cryptStream.Clo se();
DecryptedString = result.ToString ();
return DecryptedString ;
}//end Decrypt
*************** *************** *************** *************** ************
cryptStream.Flu shFinalBlock() in the decrypt function is what fails.
Any ideas would be greatly welcomed.
jdn
ki******@earthl ink.net