Reputation: 717
Here is the code which is working fine when reading file from the txt but when i read it from the string i am getting the error here
public string encrypt(string plainText,string PrivateKey)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
string filepath = path + "\\rsakeys\\pem_public.pem";
string localPath = new Uri(filepath).LocalPath;
PemReader pr = new PemReader(
(StreamReader)File.OpenText(localPath)
);
var reader = new StringReader(PrivateKey);
var pre = new PemReader(reader);
var o = pr.ReadObject();
var os = pre.ReadObject();
RsaKeyParameters keys = (RsaKeyParameters)os; >>> Here i am getting the error where os is the object readed from the string
Upvotes: 4
Views: 9819
Reputation: 961
I encountered the same error. The cause was, using the KeyStore Explorer software, I was encrypting the RSA private key in addition to just exporting it.
So, basically, the code complains that it's reading an asymmetricly encrypted key pair while expecting an RSA private key.
I unchecked the "Encrypt" box, in the "export private key" dialog box befor exporting the .pem, and the exception disappeared.
Upvotes: 0
Reputation: 717
This solve my problem
var o = pr.ReadObject();
AsymmetricCipherKeyPair keyPair=(AsymmetricCipherKeyPair)o;
//As Keypair.public is RsaParamterKey
Pkcs1Encoding eng = new Pkcs1Encoding(new RsaEngine());
eng.Init(true, keyPair.Public);
Upvotes: -1