2017-06-29 11 views
0

私はC#dotnet coreでRSAをテストしています。私は2つのRSAオブジェクトを作成します。一つは暗号化用、もう一つは解読用です。最初のrsaオブジェクトから公開鍵をエクスポートし、それを他のオブジェクトにインポートします。 2番目が暗号配列を解読すると、Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicExceptionがスローされます。 コードは以下の通りです:C#でRSAを復号化するときのInternal.Cryptography.CryptoThrowHelper.WindowsCryptographicException

 String plainstr = "Hello World"; 

     RSA rsa1 = RSA.Create(); 
     RSA rsa2 = RSA.Create(); 
     rsa1.KeySize = 1024; 
     rsa2.KeySize = 1024; 

     byte[] cipherbytes = rsa1.Encrypt(Encoding.ASCII.GetBytes(plainstr), RSAEncryptionPadding.Pkcs1); 
     //If the parameter is true, it works well. But when I use it in an actual project, I won't pass the private key. 
     RSAParameters parameters = rsa1.ExportParameters(false); 
     rsa2.ImportParameters(parameters); 
     //Exception is here. 
     byte[] plaintbytes = rsa2.Decrypt(cipherbytes, RSAEncryptionPadding.Pkcs1); 
     Console.WriteLine(Encoding.ASCII.GetString(plaintbytes)); 
     Console.ReadKey(); 

答えて

5

これは、RSA暗号化の仕組みです。 Encryptに公開鍵を使用できますが、Decryptと秘密鍵のみを使用できます。

例では、rsa1オブジェクトの秘密鍵で文字列を暗号化していますが、その公開パラメータをrsa2にコピーしていて、それを解読しようとしています。

多分あなたはその逆をしたいですか?

+0

ああ私の神!私はそれを知っている。しかし、コード化すると混乱しました。ありがとうございました! –

+0

@boyangあなたが見つけたことをうれしく思います!あなたは答えとしてそれをマークできますか? – GeorgeChond

+0

ええ、しかし方法。私は新しく、そのようなボタンを見つけられません。 –

関連する問題