I'm having a difficult time getting this to work. I've been given a PGP private key with which to decrypt a file. No problem. That works fine. However, I need to use this same private key to decrypt an encrypted RSA string (encrypted with the same public key as the file) and I'm struggling with getting it to work. I'm am unable to read the PGP key from a file and convert it into anything usable by the RSA decryption. The current error that I'm getting is:
unknown tag 13 encountered
The string encryption/decryption I am attempting to use was borrowed from here and converted from Java to C#. At this point I'm trying to test encrypting and decrypting my own string
public static string EncryptS( string publicKeyFilename, string inputData )
{
string encryptedData = null;
try
{
//Security.addProvider( new org.bouncycastle.jce.provider.BouncyCastleProvider() );
byte[] key = File.ReadAllBytes( publicKeyFilename );
AsymmetricKeyParameter publicKey = PublicKeyFactory.CreateKey( key );
Pkcs1Encoding e = new Pkcs1Encoding( new RsaEngine() );
e.Init( true, publicKey );
byte[] messageBytes = Encoding.UTF8.GetBytes( inputData );
byte[] hexEncodedCipher = e.ProcessBlock( messageBytes, 0, messageBytes.Length );
// This was a hex encoded string...
encryptedData = Convert.ToBase64String( hexEncodedCipher );
}
catch ( Exception e )
{
}
return encryptedData;
}
public static string DecryptS( string privateKeyFilename, string encryptedData )
{
String outputData = null;
try
{
byte[] key = File.ReadAllBytes( privateKeyFilename );
AsymmetricKeyParameter privateKey = PrivateKeyFactory.CreateKey( key );
Pkcs1Encoding e = new Pkcs1Encoding( new RsaEngine() );
e.Init( false, privateKey );
byte[] messageBytes = Convert.FromBase64String( encryptedData ); // May need to be converted from a hex encoded string instead of b64
byte[] hexEncodedCipher = e.ProcessBlock( messageBytes, 0, messageBytes.Length );
outputData = hexEncodedCipher.ToString();
}
catch ( Exception e )
{
}
return outputData;
}
This is what the private key looks like (lines removed for brevity).
-----BEGIN PGP PRIVATE KEY BLOCK-----
lQOYBF1wF3IBCAC4gg6Q355wn7o5odhMBErn6Nkk1fX8mgHtoNUbbp4YgEXwlB3y
0DK1vr9SFz1ZJ8IYuQ4YUDhSkVpaSgU8GW1HCsKKSgmHHTJ6FqUcE7Xii69XXeIu
vKMF+rcwVXLLlw5adPu/pLQDWCcll/tK+0E0Dmcj606XBNDn3zvvx6pzVKxODrdE
Zv0m/qDSA8feB17WH6k0opRCt5CPV47iigXVWOBh/SnussDe4qhkQ2vS8WojubWn
blSbC/c/AMwpVZT4HALP8t+bF2IxFVgYDeOPlZJhK63LUyXFcRQBcVUMHSyUI19i
wqZV/0qPGQzdK3y7
=M/Yn
-----END PGP PRIVATE KEY BLOCK-----
I thought all RSA keys were created equal....
So is it possible to use the PGP private key to decrypt an RSA encrypted string? If so, can someone please point me in the right direction to get it to work?