public static AsymmetricCipherKeyPair GetRsaKeyPair(CryptographicKey key) { var privateKeyBuffer = key.Export(CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey); byte[] privateKeyBytes; CryptographicBuffer.CopyToByteArray(privateKeyBuffer, out privateKeyBytes); var asn1 = (Asn1Sequence) Asn1Object.FromByteArray(privateKeyBytes); var rsa = new RsaPrivateKeyStructure(asn1); var pubKey = new RsaKeyParameters(false, rsa.Modulus, rsa.PublicExponent); var privKey = new RsaPrivateCrtKeyParameters( rsa.Modulus, rsa.PublicExponent, rsa.PrivateExponent, rsa.Prime1, rsa.Prime2, rsa.Exponent1, rsa.Exponent2, rsa.Coefficient); return new AsymmetricCipherKeyPair(pubKey, privKey); }
private void AsymmetricImportExport(CryptographicKey keyPair) { String algName = AlgorithmNames.SelectionBoxItem.ToString(); AsymmetricKeyAlgorithmProvider Algorithm = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(algName); // Export the public key. IBuffer blobOfPublicKey = keyPair.ExportPublicKey(); IBuffer blobOfKeyPair = keyPair.Export(); SignVerifyText.Text += " Key pair was successfully exported.\n"; // Import the public key. CryptographicKey keyPublic = Algorithm.ImportPublicKey(blobOfPublicKey); // Check the key size. if (keyPublic.KeySize != keyPair.KeySize) { SignVerifyText.Text += "ImportPublicKey failed! The imported key's size did not match the original's!"; return; } SignVerifyText.Text += " Public key was successfully imported.\n"; // Import the key pair. keyPair = Algorithm.ImportKeyPair(blobOfKeyPair); // Check the key size. if (keyPublic.KeySize != keyPair.KeySize) { SignVerifyText.Text += "ImportKeyPair failed! The imported key's size did not match the original's!"; return; } SignVerifyText.Text += " Key pair was successfully imported.\n"; }