如何为RSA算法获取私钥 [英] how to get private kye for RSA algorithm
本文介绍了如何为RSA算法获取私钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< how to =" get =" private =" kye =" for =" rsa =" algorithm =">
<how to="" get="" private="" kye="" for="" rsa="" algorithm="">
推荐答案
您必须使用私钥进行解密.您正在使用公共密钥进行加密和解密.
RSA是不对称的.
You have to use the private key to decrypt. You''re using public key for encrypting and decrypting.
RSA is asymetric.
我举了一个小例子,其中所有的魔术仅通过一种方法发生.
我正在使用3个TextBox
es来显示从通过加密的纯文本到解密的文本的路径.
文本框分别命名为textBoxPlain
,textBoxEncrypted
和textBoxDecrypted
.
因此,您可以轻松地打开一个新项目,只需在表单上放置3个文本框即可.
textBoxEncrypted设置为多行.加密和解密全部发生在textBoxPlain_TextChanged
I set up a small example where all the magic happens in just one method.
I''m using 3TextBox
es to show the path from plain text via encryption to the decrypted text.
TextBoxes are namedtextBoxPlain
,textBoxEncrypted
andtextBoxDecrypted
.
So you can quite easily open a new project and just put 3 textboxes on the form.
textBoxEncrypted is set to multiline. Encrypting and decrypting all happens intextBoxPlain_TextChanged
using System;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Collections;
namespace EncryptionRSA
{
public partial class Form1 : Form
{
private RSACryptoServiceProvider rsaSender;
private RSACryptoServiceProvider rsaReceiver;
private string publicKey;
private string privateKey;
private int keySize = 1024;
public Form1()
{
InitializeComponent();
rsaReceiver = new RSACryptoServiceProvider(keySize);
// public key which can be spread to all senders
publicKey = rsaReceiver.ToXmlString(false);
// private key. Keep it secret!
// the private key XML always contains public and private key
privateKey = rsaReceiver.ToXmlString(true);
}
private void textBoxPlain_TextChanged(object sender, EventArgs e)
{
// sender using the public key to encrypt data
rsaSender = new RSACryptoServiceProvider();
rsaSender.FromXmlString(publicKey);// "loading" public key
// code from your sample
byte[] bytes = Encoding.UTF32.GetBytes(textBoxPlain.Text);
int maxLength = keySize/8 - 42;
int dataLength = bytes.Length;
int iterationsenc = dataLength / maxLength;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i <= iterationsenc; i++)
{
byte[] tempBytes = new byte[(dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i];
Buffer.BlockCopy(bytes, maxLength * i, tempBytes, 0, tempBytes.Length);
byte[] encryptedBytes = rsaSender.Encrypt(tempBytes, true);
Array.Reverse(encryptedBytes);
stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
}
// display encrypted data
textBoxEncrypted.Text = stringBuilder.ToString();
// receiver using his privat key
rsaReceiver = new RSACryptoServiceProvider();
rsaReceiver.FromXmlString(privateKey);
// code from your sample
int base64BlockSize = ((keySize / 8) % 3 != 0) ? (((keySize / 8) / 3) * 4) + 4 : ((keySize / 8) / 3) * 4;
int iterationsdec = textBoxEncrypted.Text.Length / base64BlockSize;
ArrayList arrayList = new ArrayList();
for (int i = 0; i < iterationsdec; i++)
{
byte[] encryptedBytes = Convert.FromBase64String(textBoxEncrypted.Text.Substring(base64BlockSize * i, base64BlockSize));
Array.Reverse(encryptedBytes);
arrayList.AddRange(rsaReceiver.Decrypt(encryptedBytes, true));
}
// display decrypted data
textBoxDecrypted.Text = Encoding.UTF32.GetString(arrayList.ToArray(Type.GetType("System.Byte")) as byte[]);
}
}
}
这篇关于如何为RSA算法获取私钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文