コード例 #1
0
        //public static BinaryKeyVectorPair GetEncryptionKeyAndInitializationVector()
        //{

        //    System.Configuration.Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);

        //    EncryptionConfiguration conf = null;
        //    if (c != null)
        //    {

        //        conf = (EncryptionConfiguration)
        //            c.GetSection(
        //                EncryptionConfigurationConstants.SECTION_NAME);
        //    }
        //    conf = CheckConfiguration(conf);

        //    ICryptoTransform encryptor = null;
        //    BinaryKeyVectorPair kvpair = GetValidKeyVectorPair(conf);
        //}

        public static BinaryKeyVectorPair GetValidKeyVectorPair(byte[] key, byte[] vector, EncryptionAlgorithm algorithm)
        {
            BinaryKeyVectorPair pair = new BinaryKeyVectorPair();

            switch (algorithm)
            {
            case EncryptionAlgorithm.DES:
                //key is 8 bytes
                DESCryptoServiceProvider prov = new DESCryptoServiceProvider();
                pair.Key    = LimitBytes(key, prov.LegalKeySizes);
                pair.Vector = LimitBytes(vector, prov.LegalBlockSizes);
                break;

            case EncryptionAlgorithm.TripleDES:
                TripleDESCryptoServiceProvider prov1 = new TripleDESCryptoServiceProvider();
                pair.Key    = LimitBytes(key, prov1.LegalKeySizes);
                pair.Vector = LimitBytes(vector, prov1.LegalBlockSizes);
                break;

            case EncryptionAlgorithm.RC2:
                RC2CryptoServiceProvider prov2 = new RC2CryptoServiceProvider();
                pair.Key    = LimitBytes(key, prov2.LegalKeySizes);
                pair.Vector = LimitBytes(vector, prov2.LegalBlockSizes);
                break;

            case EncryptionAlgorithm.Rijndael:
                RijndaelManaged prov3 = new RijndaelManaged();
                pair.Key    = LimitBytes(key, prov3.LegalKeySizes);
                pair.Vector = LimitBytes(vector, prov3.LegalBlockSizes);
                break;
            }
            //shouldn't get here
            return(pair);
        }
コード例 #2
0
        public static ICryptoTransform GetEncryptorOrDecryptor(EncryptionAlgorithm algorithm, byte[] encryptionKey, byte[] initializationVector, bool encr)
        {
            BinaryKeyVectorPair kvp = GetValidKeyVectorPair(encryptionKey, initializationVector, algorithm);

            switch (algorithm)
            {
            case EncryptionAlgorithm.DES:
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //DES Keys are 8 bytes long only
                if (encr)
                {
                    return(des.CreateEncryptor(kvp.Key, kvp.Vector));
                }
                else
                {
                    return(des.CreateDecryptor(kvp.Key, kvp.Vector));
                }

            case EncryptionAlgorithm.RC2:
                RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
                if (encr)
                {
                    return(rc2.CreateEncryptor(kvp.Key, kvp.Vector));
                }
                else
                {
                    return(rc2.CreateDecryptor(kvp.Key, kvp.Vector));
                }

            case EncryptionAlgorithm.TripleDES:
                TripleDESCryptoServiceProvider trid = new TripleDESCryptoServiceProvider();
                //3DES keys are 24 bytes long only
                if (encr)
                {
                    return(trid.CreateEncryptor(kvp.Key, kvp.Vector));
                }
                else
                {
                    return(trid.CreateDecryptor(kvp.Key, kvp.Vector));
                }

            case EncryptionAlgorithm.Rijndael:
                RijndaelManaged rm = new RijndaelManaged();
                if (encr)
                {
                    return(rm.CreateEncryptor(kvp.Key, kvp.Vector));
                }
                else
                {
                    return(rm.CreateDecryptor(kvp.Key, kvp.Vector));
                }
            }
            return(null);
        }