当我的私钥在XML文件中时,如何加密文本? [英] How to encrypt text, when my private key is in the XML file?

查看:86
本文介绍了当我的私钥在XML文件中时,如何加密文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,你能帮帮我吗?

当我的私钥在privat.xml文件中时,我想加密文本。我的代码不起作用。



非常感谢您的帮助和时间。



< b>我尝试了什么:



private void button2_Click(object sender,EventArgs e)

{

//将字符串转换为字节数组

byte [] str = ASCIIEncoding.Unicode.GetBytes(textBox1.Text);



//读取私钥

string privat = System.IO.File.ReadAllText(@C:\Users\Gretaa\Desktop\RSA\privat.xml) ;



//使用任何哈希算法计算哈希值

SHA1Managed sha1hash = new SHA1Managed();

byte [] hashdata = sha1hash.ComputeHash(str);



//用私钥签名哈希数据

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ();

rsa.FromXmlString(privat);



//签名保存明文的符号数据,由私钥签名

byte [] signature = rsa.SignData(str,SHA1);



textBox14.Text = signature.ToString();

}

解决方案

简单:privat.xml不是你的关键,它是一个xml文件,其中包含嵌入在xml结构中的密钥。

您必须将文件读取为xml文件,然后获取结构中的密钥。


Hey,can you help me?
I want to encrypt text, when my private key is in the privat.xml file. My code doesn't work.

Thank you so much for help and your time.

What I have tried:

private void button2_Click(object sender, EventArgs e)
{
// convert the string into byte array
byte[] str = ASCIIEncoding.Unicode.GetBytes(textBox1.Text);

// read the private key
string privat = System.IO.File.ReadAllText(@"C:\Users\Gretaa\Desktop\RSA\privat.xml");

// compute the hash with any hash algorithm
SHA1Managed sha1hash = new SHA1Managed();
byte[] hashdata = sha1hash.ComputeHash(str);

// sign the hash data with private key
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privat);

// signature hold the sign data of plaintext , signed by private key
byte[] signature = rsa.SignData(str, "SHA1");

textBox14.Text = signature.ToString();
}

解决方案

Simple: privat.xml is not your key, it is an xml file that contain your key embedded in the xml structure.
You have to read the file as an xml file and then get the key in the structure.


这篇关于当我的私钥在XML文件中时,如何加密文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆