0
Answer

cryptographic service provider (CSP) implementation :::RC2CryptoServiceProvider

lkk lkk

lkk lkk

17y
4.8k
1

Hi all, I'm currently learning cryptography (RC2CryptoServiceProvider). i able to encrypted and decrypt the data on the same asp page, But i'm facing some problem on passing the encrypted data to another asp page.

 ///////////////Default.aspx/////////////////
protected void Button2_Click(object sender, EventArgs e)

{

String original= "testing";

String roundtrip;

ASCIIEncoding textConverter = new ASCIIEncoding();

UTF8Encoding textConverter = new UTF8Encoding();

RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();

Byte[] fromEncrypt;

Byte[] encrypted;

Byte[] toEncrypt;

Byte[] key;

Byte[] IV;

rc2CSP.GenerateKey();

rc2CSP.GenerateIV();

key = rc2CSP.Key;

IV = rc2CSP.IV;

ICryptoTransform encryptor = rc2CSP.CreateEncryptor(key, IV);

MemoryStream msEncrypt = new MemoryStream();

CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

toEncrypt = textConverter.GetBytes(original);

csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);

csEncrypt.FlushFinalBlock();

encrypted = msEncrypt.ToArray();Response.Redirect("Default2.aspx?encrypt=" + BitConverter.ToString(encrypted) + "&key=" + BitConverter.ToString(key) + "&IV=" + BitConverter.ToString(IV));

 


 

/////////Default2aspx///////////

protected void Page_Load(object sender, EventArgs e)

{

Byte[] fromEncrypt;

Byte[] encrypted;

String roundtrip;

Byte[] key;

Byte[] IV;

 

TextBox1.Text = Request.QueryString["encrypt"];

rc2CSP.key = Convert.FromBase64String(Request.QueryString["key"]);

rc2CSP.IV = Convert.FromBase64String(Request.QueryString["IV"]);

key = rc2CSP.key;

IV = rc2CSP.IV;

ICryptoTransform decryptor = rc2CSP.CreateDecryptor(key, IV);

// MemoryStream msDecrypt = new MemoryStream(encrypted);

MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(Request.QueryString["encrypt"]));

CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

fromEncrypt = new byte[Convert.FromBase64String(Request.QueryString["encrypt"]).Length];

//Read the data out of the crypto stream.

csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

//Convert the byte array back into a string.

roundtrip = textConverter.GetString(fromEncrypt);

TextBox2.Text = roundtrip;

}

 

I'm not sure how to pass the "key" and "IV" to Default2.aspx. And the encrypted data, i able to pass to over, but it is a "String", how to convert back to array of btye?

Anybody help?? Thanks:)