Trying to Decrypt Amazon EC2 password using BouncyCastle and it is not working
For O2’s Amazon EC2 Browser, I was adding the ability to decrypt the Instance’s Password (retrieved using the AWSSDK.dll GetPassswordDataRequest) but I can’t seem to decrypt its value, any ideas on how to do it?
Here is my the code that doesn’t work (uses BouncyCastle API)
var bytesToDecrypt = Convert.FromBase64String("...");</pre> AsymmetricCipherKeyPair keyPair; var pemFile = @"C:\...\SI_Key.pem"; using (var reader = File.OpenText(pemFile)) { keyPair = (AsymmetricCipherKeyPair) new PemReader(reader).ReadObject(); var decryptEngine = new Pkcs1Encoding(new RsaEngine()); decryptEngine.Init(false, keyPair.Private); var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length)); return decrypted; }
When that code executes I get the following error:
at Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding.DecodeBlock(Byte[] input, Int32 inOff, Int32 inLen) at Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding.ProcessBlock(Byte[] input, Int32 inOff, Int32 length) at DynamicType.dynamicMethod(Object returnData, Panel panel) StackTrace:</pre> at Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding.DecodeBlock(Byte[] input, Int32 inOff, Int32 inLen) at Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding.ProcessBlock(Byte[] input, Int32 inOff, Int32 length)
(the above code sample was based on this StackOverflow answer: http://stackoverflow.com/questions/243646/how-to-read-a-pem-rsa-private-key-from-net)
[…] Trying to Decrypt Amazon EC2 password using BouncyCastle and it is not working […]
[…] was able to solve the problem I had with decrypting the AmazonEC2 instance’s […]