Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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()
        {            
            //generate random prime number
            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)
        
        {
            //need to declare what is E and N
            String GetKeyE = txtE.Text;
            _keyE = Convert.ToInt64(GetKeyE);
            String GetKeyN = txtN.Text;
            _keyN = Convert.ToInt64(GetKeyN);
            
            //if keyE and N is empty
            if ((_keyE == 0) || (_keyN == 0))
            {
                throw new ApplicationException("Invalid Key");
            };

            int i;
            byte[] temp = new byte[1];
            //get the message in ASCII encode into byte
            byte[] digits = System.Text.ASCIIEncoding.ASCII.GetBytes(message);
            //calculate the byte length
            BigInteger[] bigdigits = new BigInteger[digits.Length];
            for (i = 0; i < bigdigits.Length; i++)
            {
                temp[0] = digits[i];
                bigdigits[i] = new BigInteger(temp);
            }
            //encrypt each byte with the key
            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
Posted
Updated 27-Feb-16 20:38pm
v5
Comments
F-ES Sitecore 17-Feb-16 7:36am    
You are asking a textbox to show an array of BigInteger, so how does your array of class objects look as a string? If something doesn't support getting converted to a string then calling ToString just returns the type name. Your encrypt function isn't designed to create a string of text. If you want to store your encrypted data somewhere that accepts text, or want to use it as a string of text, then you'll need to Base 64 encode the numbers in the BigNumber array. Google for how to base64 encode something, but how you get the actual number from the BigNumber class depends on how it's implemented.
HitsugayaHisagi 21-Feb-16 10:00am    
have tried many ways like in the above, but still cant get it, what should I do? (sorry for late replying, not sure why for 4 days cant post any comments)

C#
BigInteger[] encrypted = Encrypt(message);
txtCiphertext.Text = encrypted.ToString();

While executing ToString on a BigInteger will return the value formatted as string, the same can not be told for an ARRAY of BigInteger values...As there is no implementation for that the ToString method fails back to the default implementation - prints the type name...
 
Share this answer
 
Comments
HitsugayaHisagi 17-Feb-16 9:03am    
May I know what should I do? Like converting array into string? or print as array? I tried both, but with invalid argument.
Kornfeld Eliyahu Peter 17-Feb-16 9:09am    
That depends on what kind of output you expect to get...A list of the values? A continuous string of all the numbers?
HitsugayaHisagi 21-Feb-16 9:59am    
I would prefer a continuous string of numbers, and have tried various ways as in above, but I still cant get it. What should I do?
Kornfeld Eliyahu Peter 21-Feb-16 12:24pm    
A loop with a string builder in it...Append every ToString of a single element to that string builder...
HitsugayaHisagi 23-Feb-16 8:56am    
tried a few, and this
StringBuilder sb = new StringBuilder();
foreach (BigInteger item in encrypted)
{
sb.Append(encrypted.ToString());
}
is the only one that gives me output, but it printed the method name for 5 times (for 5 character in plaintext). I'm really getting very confused now. What should I do?
So here is the solution that worked for me, of course only at the encryption part (only done till that part).

C#
public partial class RSA_01 : System.Web.UI.Page
   {
       protected BigInteger _keyN, _keyE, _keyD;
       protected BigInteger p, q, m;

       protected void Page_Load(object sender, EventArgs e)
       {

       }

       protected void btnstart_Click(object sender, EventArgs e)
       {
           GenerateKey();
       }

       public void GenerateKey()
       {
           //generate random prime number
           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 GenPrime()
       {
           BigInteger prime = BigInteger.genPseudoPrime(16, 10, new System.Random());
           return prime;
       }*/


       public void Encrypt(string message)
       {
           //need to declare what is E and N
           String GetKeyE = txtE.Text;
           _keyE = Convert.ToInt64(GetKeyE);
           String GetKeyN = txtN.Text;
           _keyN = Convert.ToInt64(GetKeyN);

           //if keyE and N is empty
           if ((_keyE == 0) || (_keyN == 0))
           {
               throw new ApplicationException("Invalid Key");
           };

           int i;
           byte[] temp = new byte[1];
           //get the message in ASCII encode into byte
           byte[] digits = System.Text.ASCIIEncoding.ASCII.GetBytes(message);
           //calculate the byte length

           BigInteger[] bigdigits = new BigInteger[digits.Length];
           for (i = 0;     i < bigdigits.Length; i++)
           {
               temp[0] = digits[i];
               bigdigits[i] = new BigInteger(temp);
           }
           //encrypt each byte with the key
           BigInteger[] encrypted = new BigInteger[bigdigits.Length];

           for (i = 0; i < bigdigits.Length; i++)
           {
              encrypted[i] = bigdigits[i].modPow(_keyE, _keyN);
           }
           StringBuilder sb = new StringBuilder();
           foreach (BigInteger item in encrypted)
               {
                   sb.Append(item.ToString());
               }

           txtCiphertext.Text = sb.ToString();
       }


       protected void btnEnc_Click(object sender, EventArgs e)
       {
           string message = txtMessage.Text;
           Encrypt(message);
    }


Thank you so much Kornfeld Eliyahu Peter for your help and guidance!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900