There is a โGetPasswordDataโ call in the EC2 API, which you can use to get an encrypted data block containing an administrator password. To decrypt it, you need 2 things:
The first is the private key. This is the private half of the key pair that you used to instantiate the instance. The complication is that Amazon typically uses keys in the PEM format ("----- BEGIN" ...), but the Java Crypto API requires keys in the DER format. You can do the conversion yourself - separate the lines ----- BEGIN and ----- END, take a block of text in the middle and decode it with base64.
Secondly, the encryption settings. Data is encrypted using RSA with PKCS1 extension - so the magic call to provide JCE is: Cipher.getInstance("RSA/NONE/PKCS1Padding")
Here is a complete example (which relies on BouncyCastle, but can be modified to use a different crypto engine)
package uk.co.frontiertown; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.ec2.AmazonEC2Client; import com.amazonaws.services.ec2.model.GetPasswordDataRequest; import com.amazonaws.services.ec2.model.GetPasswordDataResult; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Base64; import javax.crypto.Cipher; import java.nio.charset.Charset; import java.security.GeneralSecurityException; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.Security; import java.security.spec.PKCS8EncodedKeySpec; public class GetEc2WindowsAdministratorPassword { private static final String ACCESS_KEY = "xxxxxxxxxxxxxxxxxxxx"; private static final String SECRET_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; private static final String PRIVATE_KEY_MATERIAL = "-----BEGIN RSA PRIVATE KEY-----\n" + "MIIEowIBAAKCAQEAjdD54kJ88GxkeRc96EQPL4h8c/7V2Q2QY5VUiJ+EblEdcVnADRa12qkohT4I\n" +
ObDisclosure: I initially answered this on my blog at http://www.frontiertown.co.uk/2012/03/java-administrator-password-windows-ec2-instance/
source share