Amazon.EC2.Model.GetPasswordDataResult.GetDecryptedPassword C# (CSharp) Method

GetDecryptedPassword() public method

Gets the decrypted password using the RSA private key which can be found in the PEM file for the key pair.
public GetDecryptedPassword ( string rsaPrivateKey ) : string
rsaPrivateKey string The RSA private key from the PEM file
return string
        public string GetDecryptedPassword(string rsaPrivateKey)
        {
            RSAParameters rsaParams;
            try
            {
                rsaParams = new PemReader(new StringReader(rsaPrivateKey.Trim())).ReadPrivatekey();
            }
            catch (Exception e)
            {
                throw new AmazonEC2Exception("Invalid RSA Private Key", e);
            }

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaParams);

            byte[] encryptedBytes = Convert.FromBase64String(this.PasswordData);
            var decryptedBytes = rsa.Decrypt(encryptedBytes, false);

            string decrypted = Encoding.UTF8.GetString(decryptedBytes);
            return decrypted;
        }
    }