【解決方法】C#での暗号化に手動のRSA秘密鍵を使用するには?

[ad_1]

I am trying to encrypt a string using RSA algorithm by using the C # built in library
System.Net.Cryptograhy.RSACryptoServiceProvider.

Suppose I have a private key(e.g):

`  -- -- - BEGIN RSA PRIVATE KEY-- -- -
  MIICXQIBAAKBgQCFf3jKXbhn2GOiCzvAxLTmZ34BdFQyNDKt / 7 B3qQ1thmSPG / NK
nrYG8OIHhDqIsGkT1YcEOQKEHPhgmtGj78Fxrv3AEi56VsYfJUTOJn9GTmqPrpk /
  zscIiD5HkWzx8JfOQS5uPHWwGmQpYftDod82nYinlwuZdAVOcz3 + 7 dShlQIDAQAB
AoGAT2sCJQGUTtMAkso0Bf3 / 0 IIJCDx4SlWbQMA37zf28Q1ejXSHTAEXMRHkpawY
VnD8UfxZkj2ILag + atovlWNwpJ + Qgw4 + t3KvPXEQB1LtMDa0q1fA + I3qU / 5 uiOCq
07 y1IWfhJ6UsfhXYV5gOmADeVrwnuGz6YUIBLQ6at3ebHgECQQDPo + 0 UUFC9z6ZX
x2a6UNIFZB9YPmxy599GMjOQOf3tEYH + qn6tYz9oZu7cWrQzkoEJ7Uw7xQsqXADU
ekfaL2f1AkEApJb6G4 + zB8gCWQy6DrqiK4Y6d1CN3s67uuZXMcpQEuju9jNBHwAj
g8zdwNAf98dksRyt7XICROrxv5i7mvFvIQJAPau5eaQVJxsqk720xDfG + TTwnlwE
B + jYhtfYOskOoz / k9UecUlE8 / vw7i5UhGoQz8UfasZHnrBAhavpapvIXlQJBAIPE
q2 + tV / N + 2 QJaAgsQuAyIZf5n62zZrcov5tbh1cGN4uIQnq4m + kfmmBJpjW5dn + t7
vnM + tzpdrjVqHagqaoECQQCAZl + BLRqEQa9cKH5lpJ4k9L + 3 sX4LQjuwNkFyZTr /
  K6Tw5lZv4eBDWtlYToEL3mtgLR7BKEled5cHCZ6pr1nm
  -- -- - END RSA PRIVATE KEY-- -- -`

And I want to use this private key to encrypt the string.

I have seen most of the examples on internet like these

`string data = "ABC";

RSACryptoServiceProvider crypto = new RSACryptoServiceProvider();

string cipherText = Encrypt(data, rsaCryptoServiceProvider.ExportParameters(false));

public static string Encrypt(string data, RSAParameters key) {

    using(var rsa = new RSACryptoServiceProvider()) {
        rsa.FromXmlString(PrivateKey);
        rsa.ImportParameters(key);
        var byteData = Encoding.UTF8.GetBytes(data);
        var encryptData = rsa.Encrypt(byteData, false);
        return Convert.ToBase64String(encryptData);
        }
    }`

But in this method I am not using the Private Key provided.

Can anyone help in encryption using the provided private key ?




I am expecting to get the proper ciphered text from my private key.

Thank You in Advance.Any help is appreciated.

私が試したこと:

I have tried this 

`string data = "ABC";

RSACryptoServiceProvider crypto = new RSACryptoServiceProvider();

string cipherText = Encrypt(data, rsaCryptoServiceProvider.ExportParameters(false));

public static string Encrypt(string data, RSAParameters key) {

    using(var rsa = new RSACryptoServiceProvider()) {
        
        rsa.ImportParameters(key);
        var byteData = Encoding.UTF8.GetBytes(data);
        var encryptData = rsa.Encrypt(byteData, false);
        return Convert.ToBase64String(encryptData);
        }
    }` 

解決策 1

ここから始めてみてください: RSA 秘密鍵暗号化[^]

[ad_2]

コメント

タイトルとURLをコピーしました