views:

240

answers:

1

Hello i have a private and public keys for RSA generated in .net

in this format

string privateKey = "<RSAKeyValue>" +
                         "<Modulus>...kCFhsjB4xMW49mrx5B/Ga...</Modulus>" +
                         "<Exponent>...</Exponent>" +
                         "<P>...7bRCrQVgVIfXdTIH3iY8x...</P>" +
                         "<Q>...4SiQDhrAZADuFDTr7bRCrQVgVIfXdTIH3iY8x...</Q>" +
                         "<DP>...ZADuFDTr7bRCrQVgVIfXdT...</DP>" +
                         "<DQ>...4SiQDhrAZADuFDTr...</DQ>" +
                         "<InverseQ>...sjB4xMW49mrx5B/Ga...</InverseQ>" +
                         "<D>...SiQDhrAZADuFDTr7bRCrQVgVIf...</D>" +
                     "</RSAKeyValue>";

how can i convert this so i can use it in php openssl functions to encrypt and decrypt data? i need both public and private keys converted.

maybe with openssl bash command in linux where i can specify my own modulus, exponent and so on?

any ideas?

thanks

A: 

Unfortunately, I haven't found a solution to the exact problem you've described, however, there are a few work-arounds you may find useful:

  • If you have access to the .NET source, you can export your keys as PEM. (I'm not positive if this is possible through the standard system libraries, but it is certainly supported by BouncyCastle.
  • If you can afford to use another language to translate the key, this can be done relatively easy in perl.

    use Crypt::OpenSSL::RSA;
    use Crypt::OpenSSL::Bignum;
    use MIME::Base64;
    $modulus = Crypt::OpenSSL::Bignum->new_from_bin(decode_base64($XMLModulus));
    $exponent= Crypt::OpenSSL::Bignum->new_from_bin(decode_base64($XMLExponent));
    # You can reading in the remaining parameters is the same as the first two
    # dq, dp, and inverseQ are not needed
    $privateKey = Crypt::OpenSSL::RSA->($modulus, $exponent, $d, $p, $q)
    # Outputs it in a standard binary form (DER) which PHP should support. print $privateKey->get_private_key_string()
    # If you need PEM, openssl can definitely convert it.

Sorry I couldn't get you a solution in PHP proper. Hopefully the C# solution will work though.

Eadwacer
thanks for the reply.i'll try perli only need to do this once, so any method will do.
netuser24