private static bool VerifyRSAKeyPairSign( RSA rsaPublicKey, RSA rsaPrivateKey) { Opc.Ua.Test.RandomSource randomSource = new Opc.Ua.Test.RandomSource(); int blockSize = RsaUtils.GetPlainTextBlockSize(rsaPrivateKey, true); byte[] testBlock = new byte[blockSize]; randomSource.NextBytes(testBlock, 0, blockSize); byte[] signature = rsaPrivateKey.SignData(testBlock, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); return(rsaPublicKey.VerifyData(testBlock, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); }
private static bool VerifyRSAKeyPairCrypt( RSA rsaPublicKey, RSA rsaPrivateKey) { Opc.Ua.Test.RandomSource randomSource = new Opc.Ua.Test.RandomSource(); int blockSize = RsaUtils.GetPlainTextBlockSize(rsaPrivateKey, true); byte[] testBlock = new byte[blockSize]; randomSource.NextBytes(testBlock, 0, blockSize); byte[] encryptedBlock = rsaPublicKey.Encrypt(testBlock, RSAEncryptionPadding.OaepSHA1); byte[] decryptedBlock = rsaPrivateKey.Decrypt(encryptedBlock, RSAEncryptionPadding.OaepSHA1); if (decryptedBlock != null) { return(Utils.IsEqual(testBlock, decryptedBlock)); } return(false); }
/// <summary> /// Verify RSA key pair of two certificates. /// </summary> private static bool VerifyRSAKeyPair( X509Certificate2 certWithPublicKey, X509Certificate2 certWithPrivateKey, bool throwOnError = false) { bool result = false; try { // verify the public and private key match using (RSA rsaPrivateKey = certWithPrivateKey.GetRSAPrivateKey()) { using (RSA rsaPublicKey = certWithPublicKey.GetRSAPublicKey()) { Opc.Ua.Test.RandomSource randomSource = new Opc.Ua.Test.RandomSource(); int blockSize = RsaUtils.GetPlainTextBlockSize(rsaPrivateKey, true); byte[] testBlock = new byte[blockSize]; randomSource.NextBytes(testBlock, 0, blockSize); byte[] encryptedBlock = rsaPublicKey.Encrypt(testBlock, RSAEncryptionPadding.OaepSHA1); byte[] decryptedBlock = rsaPrivateKey.Decrypt(encryptedBlock, RSAEncryptionPadding.OaepSHA1); if (decryptedBlock != null) { result = Utils.IsEqual(testBlock, decryptedBlock); } } } } catch (Exception e) { if (throwOnError) { throw e; } } finally { if (!result && throwOnError) { throw new CryptographicException("The public/private key pair in the certficates do not match."); } } return(result); }