Exemplo n.º 1
0
        // wraps the supplied input key data using the provided symmetric algorithm
        public static byte[] EncryptKey(byte[] keyData, SymmetricAlgorithm symmetricAlgorithm)
        {
            if (keyData == null)
            {
                throw new ArgumentNullException(nameof(keyData));
            }
            if (symmetricAlgorithm == null)
            {
                throw new ArgumentNullException(nameof(symmetricAlgorithm));
            }

            if (symmetricAlgorithm is TripleDES)
            {
                // CMS Triple DES Key Wrap
                return(SymmetricKeyWrap.TripleDESKeyWrapEncrypt(symmetricAlgorithm.Key, keyData));
            }
#pragma warning disable SYSLIB0022 // Rijndael types are obsolete
            else if (symmetricAlgorithm is Rijndael || symmetricAlgorithm is Aes)
#pragma warning restore SYSLIB0022
            {
                // FIPS AES Key Wrap
                return(SymmetricKeyWrap.AESKeyWrapEncrypt(symmetricAlgorithm.Key, keyData));
            }
            // throw an exception if the transform is not in the previous categories
            throw new CryptographicException(SR.Cryptography_Xml_NotSupportedCryptographicTransform);
        }
Exemplo n.º 2
0
 public static byte[] EncryptKey(byte[] keyData, SymmetricAlgorithm symmetricAlgorithm)
 {
     if (keyData == null)
     {
         throw new ArgumentNullException("keyData");
     }
     if (symmetricAlgorithm == null)
     {
         throw new ArgumentNullException("symmetricAlgorithm");
     }
     if (symmetricAlgorithm is TripleDES)
     {
         return(SymmetricKeyWrap.TripleDESKeyWrapEncrypt(symmetricAlgorithm.Key, keyData));
     }
     if (!(symmetricAlgorithm is Rijndael) && !(symmetricAlgorithm is Aes))
     {
         throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_NotSupportedCryptographicTransform"));
     }
     return(SymmetricKeyWrap.AESKeyWrapEncrypt(symmetricAlgorithm.Key, keyData));
 }
Exemplo n.º 3
0
        public static byte[] DecryptKey(byte[] keyData, SymmetricAlgorithm symAlg)
        {
            if (keyData == null)
            {
                throw new ArgumentNullException("keyData");
            }
            if (symAlg == null)
            {
                throw new ArgumentNullException("symAlg");
            }

            if (symAlg is TripleDES)
            {
                return(SymmetricKeyWrap.TripleDESKeyWrapDecrypt(symAlg.Key, keyData));
            }
            if (symAlg is Rijndael)
            {
                return(SymmetricKeyWrap.AESKeyWrapDecrypt(symAlg.Key, keyData));
            }
            throw new CryptographicException("The specified cryptographic transform is not supported.");
        }
Exemplo n.º 4
0
        // decrypts the supplied wrapped key using the provided symmetric algorithm
        public static byte[] DecryptKey(byte[] keyData, SymmetricAlgorithm symmetricAlgorithm)
        {
            if (keyData == null)
            {
                throw new ArgumentNullException("keyData");
            }
            if (symmetricAlgorithm == null)
            {
                throw new ArgumentNullException("symmetricAlgorithm");
            }

            if (symmetricAlgorithm is TripleDES)
            {
                // CMS Triple DES Key Wrap
                return(SymmetricKeyWrap.TripleDESKeyWrapDecrypt(symmetricAlgorithm.Key, keyData));
            }
            else if (symmetricAlgorithm is Rijndael || symmetricAlgorithm is Aes)
            {
                // FIPS AES Key Wrap
                return(SymmetricKeyWrap.AESKeyWrapDecrypt(symmetricAlgorithm.Key, keyData));
            }
            // throw an exception if the transform is not in the previous categories
            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_NotSupportedCryptographicTransform"));
        }