예제 #1
0
        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);
        }
예제 #2
0
        /// <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));
        }
예제 #3
0
        /// <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));
            }
        }
    }
예제 #5
0
        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);
        }
예제 #6
0
 /// <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);
 }