Hello,
I'm trying to work through the following example:
Here is what my Cryptography class looks like:
I have a form I'm using to test this which contains a series of text-boxes and buttons. The first button creates a public and private key and displays them in their respective text-boxes (txtPub, txtPriv). The second button encrypts the data by passing in the text to be encrypted and the private key to CipherText() and then diplays it in a text-box (txtCipher). The third button decrypts the string by passing it in along with the public key value to DecipherText(). The problem is when I try to decipher the text I get an exception with a message that says "bad key."
Any ideas?
-Kevin
Kevin Davie
Consultant
Sogeti USA
I'm trying to work through the following example:
Here is what my Cryptography class looks like:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace Rijndael
{
class RSAWrapper
{
private string _pubk;
public string pubk
{
get { return _pubk; }
set { _pubk = value; }
}
private string _prik;
public string prik
{
get { return _prik; }
set { _prik = value; }
}
public RSAWrapper()
{
CspParameters param = new CspParameters();
param.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(param);
pubk = RSA.ToXmlString(false);
prik = RSA.ToXmlString(true);
}
public string CipherText(string plainText, string xmlString)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(xmlString);
byte[] encryptedStringAsByte = RSA.Encrypt(System.Text.Encoding.Unicode.GetBytes(plainText),false);
string encryptedStringAsString = System.Text.Encoding.Unicode.GetString(encryptedStringAsByte);
return encryptedStringAsString;
}
public string DecipherText(string cipherText, string xmlString)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(xmlString);
byte[] decryptedStringAsByte = RSA.Decrypt(System.Text.Encoding.Unicode.GetBytes(cipherText), false);
string decryptedStringAsString = System.Text.Encoding.Unicode.GetString(decryptedStringAsByte);
return decryptedStringAsString;
}
}
}
I have a form I'm using to test this which contains a series of text-boxes and buttons. The first button creates a public and private key and displays them in their respective text-boxes (txtPub, txtPriv). The second button encrypts the data by passing in the text to be encrypted and the private key to CipherText() and then diplays it in a text-box (txtCipher). The third button decrypts the string by passing it in along with the public key value to DecipherText(). The problem is when I try to decipher the text I get an exception with a message that says "bad key."
Any ideas?
-Kevin
Kevin Davie
Consultant
Sogeti USA