public byte[] RsaDecryptToBytes(CipherString encyptedValue, byte[] privateKey) { if (privateKey == null) { privateKey = PrivateKey; } if (privateKey == null) { throw new ArgumentNullException(nameof(privateKey)); } IAsymmetricKeyAlgorithmProvider provider = null; switch (encyptedValue.EncryptionType) { case EncryptionType.Rsa2048_OaepSha256_B64: provider = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaOaepSha256); break; case EncryptionType.Rsa2048_OaepSha1_B64: provider = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaOaepSha1); break; default: throw new ArgumentException("EncryptionType unavailable."); } var cryptoKey = provider.ImportKeyPair(privateKey, CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo); var decryptedBytes = WinRTCrypto.CryptographicEngine.Decrypt(cryptoKey, encyptedValue.CipherTextBytes); return(decryptedBytes); }
/// <summary> /// Creates a cryptographic key based on the specified RSA parameters. /// </summary> /// <param name="provider">The asymmetric algorithm provider.</param> /// <param name="parameters">The RSA parameters from which to initialize the key.</param> /// <returns>The cryptographic key.</returns> public static ICryptographicKey ImportParameters(this IAsymmetricKeyAlgorithmProvider provider, RSAParameters parameters) { Requires.NotNull(provider, nameof(provider)); byte[] keyBlob = KeyFormatter.Pkcs1.Write(parameters); return(KeyFormatter.HasPrivateKey(parameters) ? provider.ImportKeyPair(keyBlob, CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey) : provider.ImportPublicKey(keyBlob, CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey)); }
/// <summary>Creates a cryptographic key based on the specified RSA parameters.</summary> /// <param name="provider">The asymmetric algorithm provider.</param> /// <param name="parameters">The RSA parameters from which to initialize the key.</param> /// <returns>The cryptographic key.</returns> public static ICryptographicKey ImportParameters(this IAsymmetricKeyAlgorithmProvider provider, RSAParameters parameters) { #if PCL throw new NotImplementedException("Not implemented in reference assembly."); #else Requires.NotNull(provider, "provider"); byte[] keyBlob = KeyFormatter.Pkcs1.Write(parameters); return(KeyFormatter.HasPrivateKey(parameters) ? provider.ImportKeyPair(keyBlob, CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey) : provider.ImportPublicKey(keyBlob, CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey)); #endif }
public void KeyPairRoundTrip(AsymmetricAlgorithm algorithm, int keySize, CryptographicPrivateKeyBlobType format) { IAsymmetricKeyAlgorithmProvider keyAlgorithm = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(algorithm); using (ICryptographicKey key = keyAlgorithm.CreateKeyPair(keySize)) { byte[] keyBlob = key.Export(format); using (var key2 = keyAlgorithm.ImportKeyPair(keyBlob, format)) { byte[] key2Blob = key2.Export(format); Assert.Equal(Convert.ToBase64String(keyBlob), Convert.ToBase64String(key2Blob)); this.logger.WriteLine(Convert.ToBase64String(keyBlob)); } } }
public byte[] RsaDecryptToBytes(CipherString encyptedValue, byte[] privateKey) { if (privateKey == null) { privateKey = PrivateKey; } if (privateKey == null) { throw new ArgumentNullException(nameof(privateKey)); } if (EncKey?.MacKey != null && !string.IsNullOrWhiteSpace(encyptedValue.Mac)) { var computedMacBytes = Crypto.ComputeMac(encyptedValue.CipherTextBytes, EncKey.MacKey); if (!Crypto.MacsEqual(EncKey.MacKey, computedMacBytes, encyptedValue.MacBytes)) { throw new InvalidOperationException("MAC failed."); } } IAsymmetricKeyAlgorithmProvider provider = null; switch (encyptedValue.EncryptionType) { case EncryptionType.Rsa2048_OaepSha256_B64: case EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64: provider = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaOaepSha256); break; case EncryptionType.Rsa2048_OaepSha1_B64: case EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64: provider = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaOaepSha1); break; default: throw new ArgumentException("EncryptionType unavailable."); } var cryptoKey = provider.ImportKeyPair(privateKey, CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo); var decryptedBytes = WinRTCrypto.CryptographicEngine.Decrypt(cryptoKey, encyptedValue.CipherTextBytes); return(decryptedBytes); }
/// <summary> /// Initializes the <see cref="SignatureProvider"/> with the given key pair. /// </summary> /// <param name="myPrivateKey">The local private key.</param> /// <param name="otherPublicKey">The remote public key.</param> public void Initialize(byte[] myPrivateKey, byte[] otherPublicKey) { sign = rsa.ImportKeyPair(myPrivateKey); verify = rsa.ImportPublicKey(otherPublicKey); }