rsa加密中的输出问题

rsa加密中的输出问题

本文介绍了rsa加密中的输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中遇到问题。我的代码来自 []奇怪的部分是,加密的输出给了我这个



TOA.Security。文本框中的BigInteger [] 。我可以知道我在哪里编错了吗?生成关键部分是好的,只是加密部分。这是代码。



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()
        {
            //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();
        }





最初是
$ b $中的问题 NullReferenceException Class b 加密[i] = bigdigits [i] .modPow(_keyE,_keyN)

部分,所以我添加了

字符串GetKeyE = txtE.Text和_keyE = Convert.ToInt64(GetKeyE)

它可以工作,但输出相同。可能是这个问题吗?



谢谢!



我的尝试:



试图将Convert.ToInt64改为其他方法,如BigInteger.Parse& TryParse,但是因为我正在使用TOA.Security.dll,所以不支持这些功能,因为如果与system.numerics一起使用会引起冲突。



然后尝试更改加密方法本身的输出位置,而不是将值返回到Enc_Click函数,但输出给出相同的结果。



也尝试了



string encryptedM = Encoding.UTF8.GetBytes(encrypted); //编码在当前上下文中不存在(是否有任何我可以下载和包含的外部程序集?它使用TOA.Security.dll对于BigInteger)



BigInteger [] encrypted =加密(消息);

string encryptedM = Convert.ToBase64String(encrypted); //无效参数



BigInteger [] encrypted =加密(消息);

txtCiphertext.Text = Convert.ToString(Int64.Parse(加密) ); //无效的参数



string message = txtMessage.Text;

BigInteger [] encrypted = Encrypt(message);

string encryptedM = Convert.ToString(Int64.Parse(encrypted)); //无效的参数

txtCiphertext.Text = encryptedM.ToString();



string message = txtMessage.Text;

BigInteger [] encrypted =加密(消息);

string encryptedM = Convert.ToString(Int64.TryParse(encrypted); //无重载方法

txtCiphertext。 Text = encryptedM.ToString();



BigInteger [] encrypted =加密(消息);

txtCiphertext.Text = Convert.ToString( Int64.Parse(BigInteger []。encrypted)); //提到BigInterger是一种类型



发布我在这里尝试的内容,因为不知道为什么我无法回复任何评论

推荐答案

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



在BigInteger上执行ToString时,将返回格式化为字符串的值,同样不能告知BigInteger值的ARRAY。 ..由于没有实施f或者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...


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);
    }





非常感谢Kornfeld Eliyahu Peter的帮助和指导!



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


这篇关于rsa加密中的输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:27