Esempio n. 1
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));
        }
Esempio n. 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)
        {
#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
        }
Esempio n. 3
0
        public byte[] Encrypt(byte[] data, Dictionary <string, byte[]> publicKeyIdAndPublicKeyMap, out Dictionary <string, byte[]> publicKeyIdAndEncryptedKeyMap)
        {
            publicKeyIdAndEncryptedKeyMap = new Dictionary <string, byte[]>();
            if (publicKeyIdAndPublicKeyMap == null || publicKeyIdAndPublicKeyMap.Count < 1)
            {
                return(data);
            }

            IAsymmetricKeyAlgorithmProvider provider = GetAsymmetricKeyAlgorithm();

            byte[] randomAesKey  = WinRTCrypto.CryptographicBuffer.GenerateRandom(AesKeySize);
            byte[] iv            = WinRTCrypto.CryptographicBuffer.GenerateRandom(AesIvSize); //not sure it makes sence to encrypt iv as well.
            byte[] encryptedData = Aes.Encrypt(randomAesKey, data, iv);

            foreach (var item in publicKeyIdAndPublicKeyMap)
            {
                var publicKeyId = item.Key;
                var publicKey   = item.Value;

                try
                {
                    ICryptographicKey encryptionKey = provider.ImportPublicKey(publicKey, CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey);
                    byte[]            messageKey    = WinRTCrypto.CryptographicEngine.Encrypt(encryptionKey, randomAesKey);

                    var keyBytes = new byte[messageKey.Length + iv.Length];
                    Buffer.BlockCopy(messageKey, 0, keyBytes, 0, messageKey.Length);
                    Buffer.BlockCopy(iv, 0, keyBytes, messageKey.Length, iv.Length);

                    publicKeyIdAndEncryptedKeyMap[publicKeyId] = keyBytes;
                }
                catch (Exception)
                {
                    publicKeyIdAndEncryptedKeyMap[publicKeyId] = new byte[] { 0 };
                }
            }

            return(encryptedData);
        }
Esempio n. 4
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);
 }