使用 RSA public 密钥加密文本文件
Encrypt text file using RSA public key
我得到了一个 Public 密钥(key.pub 文件) 并且使用相同的密钥,我想加密 XML 文件使用我的 C# 应用程序。
文件结构如下:
-----BEGIN PUBLIC KEY-----
xxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxx
aaaaaaaaaaaaaaabbbbbbbbbbbbbbzzzzzzzzzzzzzzzzbbbbbbbbbbbbbbbbbbb
yyyyyyyyyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzuuuuuuuuuuuuuuuuaaaaaaauuu
pppppppppppppppaao==
-----END PUBLIC KEY-----
public string Encryption(string strText)
{
var publicKey = "XXXXXXXXXXXXX The Key Value XXXXXXXXXXXXX";
var testData = Encoding.UTF8.GetBytes(strText);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
// client encrypting data with a public key issued by server
rsa.FromXmlString(publicKey.ToString());
var encryptedData = rsa.Encrypt(testData, true);
var base64Encrypted = Convert.ToBase64String(encryptedData);
string retval = base64Encrypted.ToString();
if (HaxVal1.Equals(HaxVal))
{
return retval;
}
else
{
return "InvalidSignature";
}
}
我得到了一个 Public 密钥(key.pub 文件) 并且使用相同的密钥,我想加密 XML 文件使用我的 C# 应用程序。
文件结构如下:
-----BEGIN PUBLIC KEY-----
xxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxx aaaaaaaaaaaaaaabbbbbbbbbbbbbbzzzzzzzzzzzzzzzzbbbbbbbbbbbbbbbbbbb yyyyyyyyyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzuuuuuuuuuuuuuuuuaaaaaaauuu pppppppppppppppaao==
-----END PUBLIC KEY-----
public string Encryption(string strText)
{
var publicKey = "XXXXXXXXXXXXX The Key Value XXXXXXXXXXXXX";
var testData = Encoding.UTF8.GetBytes(strText);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
// client encrypting data with a public key issued by server
rsa.FromXmlString(publicKey.ToString());
var encryptedData = rsa.Encrypt(testData, true);
var base64Encrypted = Convert.ToBase64String(encryptedData);
string retval = base64Encrypted.ToString();
if (HaxVal1.Equals(HaxVal))
{
return retval;
}
else
{
return "InvalidSignature";
}
}