public byte[][] Encrypt(byte[] aad, byte[] plainText, byte[] cek)
 {
     byte[] array;
     Ensure.BitSize(cek, this.keyLength, string.Format("AES-CBC with HMAC algorithm expected key of size {0} bits, but was given {1} bits", this.keyLength, (int)cek.Length * 8), new object[0]);
     byte[] numArray  = Arrays.FirstHalf(cek);
     byte[] numArray1 = Arrays.SecondHalf(cek);
     byte[] numArray2 = Arrays.Random(128);
     try
     {
         using (Aes ae = Aes.Create())
         {
             ae.Key = numArray1;
             ae.IV  = numArray2;
             using (MemoryStream memoryStream = new MemoryStream())
             {
                 using (ICryptoTransform cryptoTransform = ae.CreateEncryptor(ae.Key, ae.IV))
                 {
                     using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
                     {
                         cryptoStream.Write(plainText, 0, (int)plainText.Length);
                         cryptoStream.FlushFinalBlock();
                         array = memoryStream.ToArray();
                     }
                 }
             }
         }
     }
     catch (CryptographicException cryptographicException)
     {
         throw new EncryptionException("Unable to encrypt content.", cryptographicException);
     }
     byte[] numArray3 = this.ComputeAuthTag(aad, numArray2, array, numArray);
     return(new byte[][] { numArray2, array, numArray3 });
 }
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            var sharedPassphrase = Ensure.Type <string>(key, "Pbse2HmacShaKeyManagementWithAesKeyWrap management algorithm expectes key to be string.");

            byte[] sharedKey = Encoding.UTF8.GetBytes(sharedPassphrase);
            byte[] algId     = Encoding.UTF8.GetBytes((string)header["alg"]);

            int iterationCount = 8192;

            byte[] saltInput = Arrays.Random(96); //12 bytes

            header["p2c"] = iterationCount;
            header["p2s"] = Base64Url.Encode(saltInput);

            byte[] salt = Arrays.Concat(algId, Arrays.Zero, saltInput);

            byte[] kek;

            using (var prf = PRF)
            {
                kek = PBKDF2.DeriveKey(sharedKey, salt, iterationCount, keyLengthBits, prf);
            }

            return(aesKW.WrapNewKey(cekSizeBits, kek, header));
        }
 public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
 {
     byte[] numArray = Ensure.Type <byte[]>(key, "AesKeyWrap management algorithm expectes key to be byte[] array.", new object[0]);
     Ensure.BitSize(numArray, this.kekLengthBits, string.Format("AesKeyWrap management algorithm expected key of size {0} bits, but was given {1} bits", this.kekLengthBits, (int)numArray.Length * 8), new object[0]);
     byte[] numArray1 = Arrays.Random(cekSizeBits);
     byte[] numArray2 = AesKeyWrap.Wrap(numArray1, numArray);
     return(new byte[][] { numArray1, numArray2 });
 }
Example #4
0
        private bool useRsaOaepPadding; //true for RSA-OAEP, false for RSA-PKCS#1 v1.5

        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            var publicKey = Ensure.Type <RSACryptoServiceProvider>(key, "RsaKeyManagement alg expects key to be of RSACryptoServiceProvider type.");

            var cek          = Arrays.Random(cekSizeBits);
            var encryptedCek = publicKey.Encrypt(cek, useRsaOaepPadding);

            return(new[] { cek, encryptedCek });
        }
Example #5
0
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            var publicKey = Ensure.Type <RSACryptoServiceProvider>(key, "RsaKeyManagement alg expects key to be of RSACryptoServiceProvider type.");

            var cek = Arrays.Random(cekSizeBits);

            return(useSha256 ? new[] { cek, RsaOaep.Encrypt(cek, RsaKey.New(publicKey.ExportParameters(false)), CngAlgorithm.Sha256) }
                             : new[] { cek, publicKey.Encrypt(cek, useRsaOaepPadding) });
        }
Example #6
0
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            byte[] numArray = Arrays.Random(cekSizeBits);
            RSACryptoServiceProvider rSACryptoServiceProvider = Ensure.Type <RSACryptoServiceProvider>(key, "RsaKeyManagement alg expects key to be of RSACryptoServiceProvider type.", new object[0]);

            if (!this.useSha256)
            {
                return(new byte[][] { numArray, rSACryptoServiceProvider.Encrypt(numArray, this.useRsaOaepPadding) });
            }
            return(new byte[][] { numArray, RsaOaep.Encrypt(numArray, RsaKey.New(rSACryptoServiceProvider.ExportParameters(false)), CngAlgorithm.Sha256) });
        }
Example #7
0
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            var sharedKey = Ensure.Type <byte[]>(key, "AesKeyWrap management algorithm expectes key to be byte[] array.");

            Ensure.BitSize(sharedKey, kekLengthBits, string.Format("AesKeyWrap management algorithm expected key of size {0} bits, but was given {1} bits", kekLengthBits, sharedKey.Length * 8));

            var cek          = Arrays.Random(cekSizeBits);
            var encryptedCek = AesKeyWrap.Wrap(cek, sharedKey);

            return(new[] { cek, encryptedCek });
        }
 public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
 {
     byte[] numArray = Ensure.Type <byte[]>(key, "AesGcmKeyWrapManagement alg expectes key to be byte[] array.", new object[0]);
     Ensure.BitSize(numArray, this.keyLengthBits, string.Format("AesGcmKeyWrapManagement management algorithm expected key of size {0} bits, but was given {1} bits", this.keyLengthBits, (int)numArray.Length * 8), new object[0]);
     byte[]   numArray1 = Arrays.Random(96);
     byte[]   numArray2 = Arrays.Random(cekSizeBits);
     byte[][] numArray3 = AesGcm.Encrypt(numArray, numArray1, null, numArray2);
     header["iv"]  = Base64Url.Encode(numArray1);
     header["tag"] = Base64Url.Encode(numArray3[1]);
     return(new byte[][] { numArray2, numArray3[0] });
 }
Example #9
0
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            var cek = Arrays.Random(cekSizeBits);

        #if NET40
            if (key is CngKey)
            {
                var publicKey = Ensure.Type <CngKey>(key, "RsaOaep256KeyManagement alg expects key to be of CngKey type.");

                return(new[] { cek, RsaOaep.Encrypt(cek, publicKey, CngAlgorithm.Sha256) });
            }

            if (key is RSACryptoServiceProvider)
            {
                //This is for backward compatibility only with 2.x
                //To be removed in 3.x
                var publicKey = RsaKey.New(((RSACryptoServiceProvider)key).ExportParameters(false));

                return(new[] { cek, RsaOaep.Encrypt(cek, publicKey, CngAlgorithm.Sha256) });
            }

            throw new ArgumentException("RsaKeyManagement algorithm expects key to be of CngKey type.");
        #elif NET461
            if (key is CngKey)
            {
                var publicKey = (CngKey)key;

                return(new[] { cek, RsaOaep.Encrypt(cek, publicKey, CngAlgorithm.Sha256) });
            }

            if (key is RSACryptoServiceProvider)
            {
                //This is for backward compatibility only with 2.x
                //To be removed in 3.x
                var publicKey = RsaKey.New(((RSACryptoServiceProvider)key).ExportParameters(false));

                return(new[] { cek, RsaOaep.Encrypt(cek, publicKey, CngAlgorithm.Sha256) });
            }

            if (key is RSA)
            {
                var publicKey = (RSA)key;

                return(new[] { cek, publicKey.Encrypt(cek, RSAEncryptionPadding.OaepSHA256) });
            }

            throw new ArgumentException("RsaKeyManagement algorithm expects key to be of either CngKey or RSA types.");
#elif NETSTANDARD
            var publicKey = Ensure.Type <RSA>(key, "RsaKeyManagement algorithm expects key to be of RSA type.");

            return(new[] { cek, publicKey.Encrypt(cek, RSAEncryptionPadding.OaepSHA256) });
#endif
        }
Example #10
0
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            var cek = Arrays.Random(cekSizeBits);

        #if NET40
            var publicKey = Ensure.Type <CngKey>(key, "RsaOaep256KeyManagement alg expects key to be of CngKey type.");

            return(new[] { cek, RsaOaep.Encrypt(cek, publicKey, CngAlgorithm.Sha256) });
        #elif NETSTANDARD1_4
            var publicKey = Ensure.Type <RSA>(key, "RsaKeyManagement alg expects key to be of RSA type.");

            return(new[] { cek, publicKey.Encrypt(cek, RSAEncryptionPadding.OaepSHA256) });
        #endif
        }
        public byte[] WrapKey(byte[] cek, object key, IDictionary <string, object> header)
        {
            byte[] sharedKey = Ensure.Type <byte[]>(key, "AesGcmKeyWrapManagement alg expectes key to be byte[] array.");
            Ensure.BitSize(sharedKey, keyLengthBits, string.Format("AesGcmKeyWrapManagement management algorithm expected key of size {0} bits, but was given {1} bits", keyLengthBits, sharedKey.Length * 8L));

            byte[] iv = Arrays.Random(96);

            byte[][] cipherAndTag = AesGcm.Encrypt(sharedKey, iv, null, cek);

            header["iv"]  = Base64Url.Encode(iv);
            header["tag"] = Base64Url.Encode(cipherAndTag[1]);

            return(cipherAndTag[0]);
        }
Example #12
0
 public byte[][] Encrypt(byte[] aad, byte[] plainText, byte[] cek)
 {
     byte[][] numArray;
     Ensure.BitSize(cek, this.keyLength, string.Format("AES-GCM algorithm expected key of size {0} bits, but was given {1} bits", this.keyLength, (int)cek.Length * 8), new object[0]);
     byte[] numArray1 = Arrays.Random(96);
     try
     {
         byte[][] numArray2 = AesGcm.Encrypt(cek, numArray1, aad, plainText);
         numArray = new byte[][] { numArray1, numArray2[0], numArray2[1] };
     }
     catch (CryptographicException cryptographicException)
     {
         throw new EncryptionException("Unable to encrypt content.", cryptographicException);
     }
     return(numArray);
 }
Example #13
0
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            var cek = Arrays.Random(cekSizeBits);

#if NET40 || NET461
            var publicKey = Ensure.Type <RSACryptoServiceProvider>(key, "RsaKeyManagement alg expects key to be of RSACryptoServiceProvider type.");

            return(new[] { cek, publicKey.Encrypt(cek, useRsaOaepPadding) });
#elif NETSTANDARD1_4
            var publicKey = Ensure.Type <RSA>(key, "RsaKeyManagement alg expects key to be of RSA type.");

            var padding = useRsaOaepPadding ? RSAEncryptionPadding.OaepSHA1 :
                          RSAEncryptionPadding.Pkcs1;

            return(new[] { cek, publicKey.Encrypt(cek, padding) });
#endif
        }
Example #14
0
        public byte[][] Encrypt(byte[] aad, byte[] plainText, byte[] cek)
        {
            Ensure.BitSize(cek, keyLength, string.Format("AES-GCM algorithm expected key of size {0} bits, but was given {1} bits", keyLength, cek.Length * 8L));

            byte[] iv = Arrays.Random(96);

            try
            {
                byte[][] cipherAndTag = AesGcm.Encrypt(cek, iv, aad, plainText);

                return(new[] { iv, cipherAndTag[0], cipherAndTag[1] });
            }
            catch (CryptographicException e)
            {
                throw new EncryptionException("Unable to encrypt content.", e);
            }
        }
Example #15
0
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            var cek = Arrays.Random(cekSizeBits);

#if NET40
            var publicKey = Ensure.Type <RSACryptoServiceProvider>(key, "RsaKeyManagement alg expects key to be of RSACryptoServiceProvider type.");

            return(new[] { cek, publicKey.Encrypt(cek, useRsaOaepPadding) });
#elif NET461
            if (key is CngKey)
            {
                var publicKey = new RSACng((CngKey)key);

                var padding = useRsaOaepPadding ? RSAEncryptionPadding.OaepSHA1 :
                              RSAEncryptionPadding.Pkcs1;

                return(new[] { cek, publicKey.Encrypt(cek, padding) });
            }

            if (key is RSACryptoServiceProvider)
            {
                var publicKey = (RSACryptoServiceProvider)key;

                return(new[] { cek, publicKey.Encrypt(cek, useRsaOaepPadding) });
            }

            if (key is RSA)
            {
                var publicKey = (RSA)key;

                var padding = useRsaOaepPadding ? RSAEncryptionPadding.OaepSHA1 :
                              RSAEncryptionPadding.Pkcs1;

                return(new[] { cek, publicKey.Encrypt(cek, padding) });
            }

            throw new ArgumentException("RsaKeyManagement algorithm expects key to be of either CngKey, RSACryptoServiceProvider or RSA types.");
#elif NETSTANDARD
            var publicKey = Ensure.Type <RSA>(key, "RsaKeyManagement alg expects key to be of RSA type.");

            var padding = useRsaOaepPadding ? RSAEncryptionPadding.OaepSHA1 :
                          RSAEncryptionPadding.Pkcs1;

            return(new[] { cek, publicKey.Encrypt(cek, padding) });
#endif
        }
        public byte[][] Encrypt(byte[] aad, byte[] plainText, byte[] cek)
        {
            Ensure.BitSize(cek, keyLength, string.Format("AES-CBC with HMAC algorithm expected key of size {0} bits, but was given {1} bits", keyLength, cek.Length * 8L));

            byte[] hmacKey = Arrays.FirstHalf(cek);
            byte[] aesKey  = Arrays.SecondHalf(cek);

            byte[] iv = Arrays.Random();

            byte[] cipherText;

            try
            {
                using (Aes aes = Aes.Create())
                {
                    aes.Key     = aesKey;
                    aes.IV      = iv;
                    aes.Mode    = CipherMode.CBC;
                    aes.Padding = PaddingMode.PKCS7;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
                        {
                            using (CryptoStream encrypt = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                            {
                                encrypt.Write(plainText, 0, plainText.Length);
                                encrypt.FlushFinalBlock();

                                cipherText = ms.ToArray();
                            }
                        }
                    }
                }
            }
            catch (CryptographicException e)
            {
                throw new EncryptionException("Unable to encrypt content.", e);
            }

            byte[] authTag = ComputeAuthTag(aad, iv, cipherText, hmacKey);

            return(new[] { iv, cipherText, authTag });
        }
Example #17
0
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            var cek = Arrays.Random(cekSizeBits);

#if NET40
            var publicKey = Ensure.Type <RSACryptoServiceProvider>(key, "RsaKeyManagement alg expects key to be of RSACryptoServiceProvider type.");

            return(useSha256 ? new[] { cek, RsaOaep.Encrypt(cek, RsaKey.New(publicKey.ExportParameters(false)), CngAlgorithm.Sha256) }
                             : new[] { cek, publicKey.Encrypt(cek, useRsaOaepPadding) });
#elif NETSTANDARD1_4
            var publicKey = Ensure.Type <RSA>(key, "RsaKeyManagement alg expects key to be of RSA type.");

            var padding = useSha256         ? RSAEncryptionPadding.OaepSHA256 :
                          useRsaOaepPadding ? RSAEncryptionPadding.OaepSHA1 :
                          RSAEncryptionPadding.Pkcs1;

            return(new[] { cek, publicKey.Encrypt(cek, padding) });
#endif
        }
Example #18
0
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            byte[] numArray;
            string str = Ensure.Type <string>(key, "Pbse2HmacShaKeyManagementWithAesKeyWrap management algorithm expectes key to be string.", new object[0]);

            byte[] bytes  = Encoding.UTF8.GetBytes(str);
            byte[] bytes1 = Encoding.UTF8.GetBytes((string)header["alg"]);
            int    num    = 8192;

            byte[] numArray1 = Arrays.Random(96);
            header["p2c"] = num;
            header["p2s"] = Base64Url.Encode(numArray1);
            byte[] numArray2 = Arrays.Concat(new byte[][] { bytes1, Arrays.Zero, numArray1 });
            using (HMAC pRF = this.PRF)
            {
                numArray = PBKDF2.DeriveKey(bytes, numArray2, num, this.keyLengthBits, pRF);
            }
            return(this.aesKW.WrapNewKey(cekSizeBits, numArray, header));
        }
Example #19
0
        public byte[][] WrapNewKey(int cekSizeBits, object key, IDictionary <string, object> header)
        {
            var cek = Arrays.Random(cekSizeBits);

            return(new byte[][] { cek, this.WrapKey(cek, key, header) });
        }