I have problem in below code. The code I took is from
Client/Server Encryption plus extras[
^] The weird part is, the output of the encryption gives me this
TOA.Security.BigInteger[] in the textbox. May I know where have I edited wrongly? The generating key part is ok, just the encryption part. Here is the code.
public partial class RSA_01 : System.Web.UI.Page
{
protected BigInteger _keyN, _keyE, _keyD;
protected BigInteger p, q, m;
protected void btnstart_Click(object sender, EventArgs e)
{
GenerateKey();
}
public void GenerateKey()
{
p = BigInteger.genPseudoPrime(16, 10, new System.Random());
do
{
q = BigInteger.genPseudoPrime(16, 10, new System.Random());
}
while (p == q);
_keyN = (p * q);
m = (p - 1) * (q - 1);
_keyE = new BigInteger("10001", 16);
_keyD = _keyE.modInverse(m);
txtP.Text = p.ToString();
txtQ.Text = q.ToString();
txtM.Text = m.ToString();
txtN.Text = _keyN.ToString();
txtE.Text = _keyE.ToString();
txtD.Text = _keyD.ToString();
}
public BigInteger[] Encrypt(string message)
{
String GetKeyE = txtE.Text;
_keyE = Convert.ToInt64(GetKeyE);
String GetKeyN = txtN.Text;
_keyN = Convert.ToInt64(GetKeyN);
if ((_keyE == 0) || (_keyN == 0))
{
throw new ApplicationException("Invalid Key");
};
int i;
byte[] temp = new byte[1];
byte[] digits = System.Text.ASCIIEncoding.ASCII.GetBytes(message);
BigInteger[] bigdigits = new BigInteger[digits.Length];
for (i = 0; i < bigdigits.Length; i++)
{
temp[0] = digits[i];
bigdigits[i] = new BigInteger(temp);
}
BigInteger[] encrypted = new BigInteger[bigdigits.Length];
for (i = 0; i < bigdigits.Length; i++)
{
encrypted[i] = bigdigits[i].modPow(_keyE, _keyN);
}
return encrypted;
}
protected void btnEnc_Click(object sender, EventArgs e)
{
string message = txtMessage.Text;
BigInteger[] encrypted = Encrypt(message);
txtCiphertext.Text = encrypted.ToString();
}
Initially was the problem
NullReferenceException Class in the
encrypted[i] = bigdigits[i].modPow(_keyE, _keyN)
part, so I added the
String GetKeyE = txtE.Text and _keyE = Convert.ToInt64(GetKeyE)
and it works, but it outputs the same. Could it be this problem?
Thank you!
What I have tried:
Tried to change from using the Convert.ToInt64 to other method like BigInteger.Parse & TryParse, but because I'm using TOA.Security.dll, those function is not supported because it causes conflict if used together with system.numerics.
Then tried to change the output place in the encrypt method itself instead of returning value into the Enc_Click function, but the output gives the same.
have tried also
string encryptedM = Encoding.UTF8.GetBytes(encrypted);//encoding doesnt exist in current context (is there any external assembly that I can download and include? it uses TOA.Security.dll for the BigInteger)
BigInteger[] encrypted = Encrypt(message);
string encryptedM = Convert.ToBase64String(encrypted);//invalid argument
BigInteger[] encrypted = Encrypt(message);
txtCiphertext.Text = Convert.ToString(Int64.Parse(encrypted)); //invalid argument
string message = txtMessage.Text;
BigInteger[] encrypted = Encrypt(message);
string encryptedM = Convert.ToString(Int64.Parse(encrypted)); // invalid argument
txtCiphertext.Text = encryptedM.ToString();
string message = txtMessage.Text;
BigInteger[] encrypted = Encrypt(message);
string encryptedM = Convert.ToString(Int64.TryParse(encrypted); // no overload method
txtCiphertext.Text = encryptedM.ToString();
BigInteger[] encrypted = Encrypt(message);
txtCiphertext.Text = Convert.ToString(Int64.Parse(BigInteger[].encrypted));//mention BigInterger is a type
posting what I have tried here, because dont know why i cannot reply to any comments