OWASP O2 Platform Blog

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)

April 16, 2011 - Posted by | EC2

2 Comments »

  1. […] Trying to Decrypt Amazon EC2 password using BouncyCastle and it is not working […]

    Pingback by Script to fetch and present large number of Wordpress.com blog entries « O2Platform.com for Developers | April 16, 2011 | Reply

  2. […] was able to solve the problem I had with decrypting the AmazonEC2 instance’s […]

    Pingback by Solved the problem with decryping AmazonEC2 Instance’s password « O2Platform.com for Developers | April 16, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: