Cipher Utility class contains methods that can not be specifically grouped into other classes.
Exemple #1
0
        public static object CreateEngine(
            string algorithm)
        {
            string mechanism = (string)algorithms[algorithm.ToUpper(CultureInfo.InvariantCulture)];

            if (mechanism.StartsWith("PBEwithSHA-1"))
            {
                if (mechanism.Equals("PBEwithSHA-1and128bitRC4"))
                {
//                    return new RC4Engine();
                    return(CipherUtilities.GetCipher("RC4"));
                }
                else if (mechanism.Equals("PBEwithSHA-1and40bitRC4"))
                {
//                    return new RC4Engine();
                    return(CipherUtilities.GetCipher("RC4"));
                }
                else if (mechanism.Equals("PBEwithSHA-1and3-keyDESEDE-CBC"))
                {
//                    return new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEdeEngine()), new Pkcs7Padding());
                    return(CipherUtilities.GetCipher("DESEDE/CBC"));
                }
                else if (mechanism.Equals("PBEwithSHA-1and2-keyDESEDE-CBC"))
                {
                    return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEdeEngine()), new Pkcs7Padding()));
                }
                else if (mechanism.Equals("PBEwithSHA-1and128bitRC2-CBC"))
                {
                    return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new RC2Engine()), new Pkcs7Padding()));
                }
                else if (mechanism.Equals("PBEwithSHA-1and40bitRC2-CBC"))
                {
                    return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new RC2Engine()), new Pkcs7Padding()));
                }
                else if (mechanism.Equals("PBEwithSHA-1andDES-CBC"))
                {
                    return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEngine()), new Pkcs7Padding()));
                }
                else if (mechanism.Equals("PBEwithSHA-1andRC2-CBC"))
                {
                    return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new RC2Engine()), new Pkcs7Padding()));
                }
            }
            else if (mechanism.StartsWith("PBEwithMD5"))
            {
                if (mechanism.Equals("PBEwithMD5andDES-CBC"))
                {
                    return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEngine()), new Pkcs7Padding()));
                }
                else if (mechanism.Equals("PBEwithMD5andRC2-CBC"))
                {
                    return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new RC2Engine()), new Pkcs7Padding()));
                }
            }
            else if (mechanism.StartsWith("PBEwithMD2"))
            {
                if (mechanism.Equals("PBEwithMD2andDES-CBC"))
                {
                    return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEngine()), new Pkcs7Padding()));
                }
                else if (mechanism.Equals("PBEwithMD2andRC2-CBC"))
                {
                    return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new RC2Engine()), new Pkcs7Padding()));
                }
            }
            else if (mechanism.StartsWith("PBEwithHmac"))
            {
                if (mechanism.Equals("PBEwithHmacSHA-1"))
                {
                    return(new HMac(new Sha1Digest()));
                }
                else if (mechanism.Equals("PBEwithHmacSHA-224"))
                {
                    return(new HMac(new Sha224Digest()));
                }
                else if (mechanism.Equals("PBEwithHmacSHA-256"))
                {
                    return(new HMac(new Sha256Digest()));
                }
                else if (mechanism.Equals("PBEwithHmacRipeMD128"))
                {
                    return(new HMac(new RipeMD128Digest()));
                }
                else if (mechanism.Equals("PBEwithHmacRipeMD160"))
                {
                    return(new HMac(new RipeMD160Digest()));
                }
                else if (mechanism.Equals("PBEwithHmacRipeMD256"))
                {
                    return(new HMac(new RipeMD256Digest()));
                }
            }
            return(null);
        }
 public static IBufferedCipher GetCipher(DerObjectIdentifier oid)
 {
     return(CipherUtilities.GetCipher(oid.Id));
 }
        public static IBufferedCipher GetCipher(string algorithm)
        {
            if (algorithm == null)
            {
                throw new ArgumentNullException("algorithm");
            }
            algorithm = Platform.ToUpperInvariant(algorithm);
            string text = (string)CipherUtilities.algorithms[algorithm];

            if (text != null)
            {
                algorithm = text;
            }
            IBasicAgreement basicAgreement = null;

            if (algorithm == "IES")
            {
                basicAgreement = new DHBasicAgreement();
            }
            else if (algorithm == "ECIES")
            {
                basicAgreement = new ECDHBasicAgreement();
            }
            if (basicAgreement != null)
            {
                return(new BufferedIesCipher(new IesEngine(basicAgreement, new Kdf2BytesGenerator(new Sha1Digest()), new HMac(new Sha1Digest()))));
            }
            if (algorithm.StartsWith("PBE"))
            {
                if (algorithm.EndsWith("-CBC"))
                {
                    if (algorithm == "PBEWITHSHA1ANDDES-CBC")
                    {
                        return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEngine())));
                    }
                    if (algorithm == "PBEWITHSHA1ANDRC2-CBC")
                    {
                        return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new RC2Engine())));
                    }
                    if (Strings.IsOneOf(algorithm, new string[]
                    {
                        "PBEWITHSHAAND2-KEYTRIPLEDES-CBC",
                        "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"
                    }))
                    {
                        return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new DesEdeEngine())));
                    }
                    if (Strings.IsOneOf(algorithm, new string[]
                    {
                        "PBEWITHSHAAND128BITRC2-CBC",
                        "PBEWITHSHAAND40BITRC2-CBC"
                    }))
                    {
                        return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new RC2Engine())));
                    }
                }
                else if ((algorithm.EndsWith("-BC") || algorithm.EndsWith("-OPENSSL")) && Strings.IsOneOf(algorithm, new string[]
                {
                    "PBEWITHSHAAND128BITAES-CBC-BC",
                    "PBEWITHSHAAND192BITAES-CBC-BC",
                    "PBEWITHSHAAND256BITAES-CBC-BC",
                    "PBEWITHSHA256AND128BITAES-CBC-BC",
                    "PBEWITHSHA256AND192BITAES-CBC-BC",
                    "PBEWITHSHA256AND256BITAES-CBC-BC",
                    "PBEWITHMD5AND128BITAES-CBC-OPENSSL",
                    "PBEWITHMD5AND192BITAES-CBC-OPENSSL",
                    "PBEWITHMD5AND256BITAES-CBC-OPENSSL"
                }))
                {
                    return(new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesFastEngine())));
                }
            }
            string[] array = algorithm.Split(new char[]
            {
                '/'
            });
            IBlockCipher           blockCipher           = null;
            IAsymmetricBlockCipher asymmetricBlockCipher = null;
            IStreamCipher          streamCipher          = null;
            string text2 = array[0];
            string text3 = (string)CipherUtilities.algorithms[text2];

            if (text3 != null)
            {
                text2 = text3;
            }
            CipherUtilities.CipherAlgorithm cipherAlgorithm;
            try
            {
                cipherAlgorithm = (CipherUtilities.CipherAlgorithm)Enums.GetEnumValue(typeof(CipherUtilities.CipherAlgorithm), text2);
            }
            catch (ArgumentException)
            {
                throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
            }
            switch (cipherAlgorithm)
            {
            case CipherUtilities.CipherAlgorithm.AES:
                blockCipher = new AesFastEngine();
                break;

            case CipherUtilities.CipherAlgorithm.ARC4:
                streamCipher = new RC4Engine();
                break;

            case CipherUtilities.CipherAlgorithm.BLOWFISH:
                blockCipher = new BlowfishEngine();
                break;

            case CipherUtilities.CipherAlgorithm.CAMELLIA:
                blockCipher = new CamelliaEngine();
                break;

            case CipherUtilities.CipherAlgorithm.CAST5:
                blockCipher = new Cast5Engine();
                break;

            case CipherUtilities.CipherAlgorithm.CAST6:
                blockCipher = new Cast6Engine();
                break;

            case CipherUtilities.CipherAlgorithm.DES:
                blockCipher = new DesEngine();
                break;

            case CipherUtilities.CipherAlgorithm.DESEDE:
                blockCipher = new DesEdeEngine();
                break;

            case CipherUtilities.CipherAlgorithm.ELGAMAL:
                asymmetricBlockCipher = new ElGamalEngine();
                break;

            case CipherUtilities.CipherAlgorithm.GOST28147:
                blockCipher = new Gost28147Engine();
                break;

            case CipherUtilities.CipherAlgorithm.HC128:
                streamCipher = new HC128Engine();
                break;

            case CipherUtilities.CipherAlgorithm.HC256:
                streamCipher = new HC256Engine();
                break;

            case CipherUtilities.CipherAlgorithm.IDEA:
                blockCipher = new IdeaEngine();
                break;

            case CipherUtilities.CipherAlgorithm.NOEKEON:
                blockCipher = new NoekeonEngine();
                break;

            case CipherUtilities.CipherAlgorithm.PBEWITHSHAAND128BITRC4:
            case CipherUtilities.CipherAlgorithm.PBEWITHSHAAND40BITRC4:
                streamCipher = new RC4Engine();
                break;

            case CipherUtilities.CipherAlgorithm.RC2:
                blockCipher = new RC2Engine();
                break;

            case CipherUtilities.CipherAlgorithm.RC5:
                blockCipher = new RC532Engine();
                break;

            case CipherUtilities.CipherAlgorithm.RC5_64:
                blockCipher = new RC564Engine();
                break;

            case CipherUtilities.CipherAlgorithm.RC6:
                blockCipher = new RC6Engine();
                break;

            case CipherUtilities.CipherAlgorithm.RIJNDAEL:
                blockCipher = new RijndaelEngine();
                break;

            case CipherUtilities.CipherAlgorithm.RSA:
                asymmetricBlockCipher = new RsaBlindedEngine();
                break;

            case CipherUtilities.CipherAlgorithm.SALSA20:
                streamCipher = new Salsa20Engine();
                break;

            case CipherUtilities.CipherAlgorithm.SEED:
                blockCipher = new SeedEngine();
                break;

            case CipherUtilities.CipherAlgorithm.SERPENT:
                blockCipher = new SerpentEngine();
                break;

            case CipherUtilities.CipherAlgorithm.SKIPJACK:
                blockCipher = new SkipjackEngine();
                break;

            case CipherUtilities.CipherAlgorithm.TEA:
                blockCipher = new TeaEngine();
                break;

            case CipherUtilities.CipherAlgorithm.TWOFISH:
                blockCipher = new TwofishEngine();
                break;

            case CipherUtilities.CipherAlgorithm.VMPC:
                streamCipher = new VmpcEngine();
                break;

            case CipherUtilities.CipherAlgorithm.VMPC_KSA3:
                streamCipher = new VmpcKsa3Engine();
                break;

            case CipherUtilities.CipherAlgorithm.XTEA:
                blockCipher = new XteaEngine();
                break;

            default:
                throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
            }
            if (streamCipher != null)
            {
                if (array.Length > 1)
                {
                    throw new ArgumentException("Modes and paddings not used for stream ciphers");
                }
                return(new BufferedStreamCipher(streamCipher));
            }
            else
            {
                bool flag  = false;
                bool flag2 = true;
                IBlockCipherPadding blockCipherPadding = null;
                IAeadBlockCipher    aeadBlockCipher    = null;
                if (array.Length > 2)
                {
                    if (streamCipher != null)
                    {
                        throw new ArgumentException("Paddings not used for stream ciphers");
                    }
                    string text4 = array[2];
                    CipherUtilities.CipherPadding cipherPadding;
                    if (text4 == "")
                    {
                        cipherPadding = CipherUtilities.CipherPadding.RAW;
                    }
                    else if (text4 == "X9.23PADDING")
                    {
                        cipherPadding = CipherUtilities.CipherPadding.X923PADDING;
                    }
                    else
                    {
                        try
                        {
                            cipherPadding = (CipherUtilities.CipherPadding)Enums.GetEnumValue(typeof(CipherUtilities.CipherPadding), text4);
                        }
                        catch (ArgumentException)
                        {
                            throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                        }
                    }
                    switch (cipherPadding)
                    {
                    case CipherUtilities.CipherPadding.NOPADDING:
                        flag2 = false;
                        break;

                    case CipherUtilities.CipherPadding.RAW:
                        break;

                    case CipherUtilities.CipherPadding.ISO10126PADDING:
                    case CipherUtilities.CipherPadding.ISO10126D2PADDING:
                    case CipherUtilities.CipherPadding.ISO10126_2PADDING:
                        blockCipherPadding = new ISO10126d2Padding();
                        break;

                    case CipherUtilities.CipherPadding.ISO7816_4PADDING:
                    case CipherUtilities.CipherPadding.ISO9797_1PADDING:
                        blockCipherPadding = new ISO7816d4Padding();
                        break;

                    case CipherUtilities.CipherPadding.ISO9796_1:
                    case CipherUtilities.CipherPadding.ISO9796_1PADDING:
                        asymmetricBlockCipher = new ISO9796d1Encoding(asymmetricBlockCipher);
                        break;

                    case CipherUtilities.CipherPadding.OAEP:
                    case CipherUtilities.CipherPadding.OAEPPADDING:
                        asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher);
                        break;

                    case CipherUtilities.CipherPadding.OAEPWITHMD5ANDMGF1PADDING:
                        asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new MD5Digest());
                        break;

                    case CipherUtilities.CipherPadding.OAEPWITHSHA1ANDMGF1PADDING:
                    case CipherUtilities.CipherPadding.OAEPWITHSHA_1ANDMGF1PADDING:
                        asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new Sha1Digest());
                        break;

                    case CipherUtilities.CipherPadding.OAEPWITHSHA224ANDMGF1PADDING:
                    case CipherUtilities.CipherPadding.OAEPWITHSHA_224ANDMGF1PADDING:
                        asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new Sha224Digest());
                        break;

                    case CipherUtilities.CipherPadding.OAEPWITHSHA256ANDMGF1PADDING:
                    case CipherUtilities.CipherPadding.OAEPWITHSHA_256ANDMGF1PADDING:
                        asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new Sha256Digest());
                        break;

                    case CipherUtilities.CipherPadding.OAEPWITHSHA384ANDMGF1PADDING:
                    case CipherUtilities.CipherPadding.OAEPWITHSHA_384ANDMGF1PADDING:
                        asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new Sha384Digest());
                        break;

                    case CipherUtilities.CipherPadding.OAEPWITHSHA512ANDMGF1PADDING:
                    case CipherUtilities.CipherPadding.OAEPWITHSHA_512ANDMGF1PADDING:
                        asymmetricBlockCipher = new OaepEncoding(asymmetricBlockCipher, new Sha512Digest());
                        break;

                    case CipherUtilities.CipherPadding.PKCS1:
                    case CipherUtilities.CipherPadding.PKCS1PADDING:
                        asymmetricBlockCipher = new Pkcs1Encoding(asymmetricBlockCipher);
                        break;

                    case CipherUtilities.CipherPadding.PKCS5:
                    case CipherUtilities.CipherPadding.PKCS5PADDING:
                    case CipherUtilities.CipherPadding.PKCS7:
                    case CipherUtilities.CipherPadding.PKCS7PADDING:
                        blockCipherPadding = new Pkcs7Padding();
                        break;

                    case CipherUtilities.CipherPadding.TBCPADDING:
                        blockCipherPadding = new TbcPadding();
                        break;

                    case CipherUtilities.CipherPadding.WITHCTS:
                        flag = true;
                        break;

                    case CipherUtilities.CipherPadding.X923PADDING:
                        blockCipherPadding = new X923Padding();
                        break;

                    case CipherUtilities.CipherPadding.ZEROBYTEPADDING:
                        blockCipherPadding = new ZeroBytePadding();
                        break;

                    default:
                        throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                    }
                }
                if (array.Length > 1)
                {
                    string text5      = array[1];
                    int    digitIndex = CipherUtilities.GetDigitIndex(text5);
                    string text6      = (digitIndex >= 0) ? text5.Substring(0, digitIndex) : text5;
                    try
                    {
                        switch ((text6 == "") ? CipherUtilities.CipherMode.NONE : ((CipherUtilities.CipherMode)Enums.GetEnumValue(typeof(CipherUtilities.CipherMode), text6)))
                        {
                        case CipherUtilities.CipherMode.ECB:
                        case CipherUtilities.CipherMode.NONE:
                            break;

                        case CipherUtilities.CipherMode.CBC:
                            blockCipher = new CbcBlockCipher(blockCipher);
                            break;

                        case CipherUtilities.CipherMode.CCM:
                            aeadBlockCipher = new CcmBlockCipher(blockCipher);
                            break;

                        case CipherUtilities.CipherMode.CFB:
                        {
                            int bitBlockSize = (digitIndex < 0) ? (8 * blockCipher.GetBlockSize()) : int.Parse(text5.Substring(digitIndex));
                            blockCipher = new CfbBlockCipher(blockCipher, bitBlockSize);
                            break;
                        }

                        case CipherUtilities.CipherMode.CTR:
                            blockCipher = new SicBlockCipher(blockCipher);
                            break;

                        case CipherUtilities.CipherMode.CTS:
                            flag        = true;
                            blockCipher = new CbcBlockCipher(blockCipher);
                            break;

                        case CipherUtilities.CipherMode.EAX:
                            aeadBlockCipher = new EaxBlockCipher(blockCipher);
                            break;

                        case CipherUtilities.CipherMode.GCM:
                            aeadBlockCipher = new GcmBlockCipher(blockCipher);
                            break;

                        case CipherUtilities.CipherMode.GOFB:
                            blockCipher = new GOfbBlockCipher(blockCipher);
                            break;

                        case CipherUtilities.CipherMode.OCB:
                            aeadBlockCipher = new OcbBlockCipher(blockCipher, CipherUtilities.CreateBlockCipher(cipherAlgorithm));
                            break;

                        case CipherUtilities.CipherMode.OFB:
                        {
                            int blockSize = (digitIndex < 0) ? (8 * blockCipher.GetBlockSize()) : int.Parse(text5.Substring(digitIndex));
                            blockCipher = new OfbBlockCipher(blockCipher, blockSize);
                            break;
                        }

                        case CipherUtilities.CipherMode.OPENPGPCFB:
                            blockCipher = new OpenPgpCfbBlockCipher(blockCipher);
                            break;

                        case CipherUtilities.CipherMode.SIC:
                            if (blockCipher.GetBlockSize() < 16)
                            {
                                throw new ArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)");
                            }
                            blockCipher = new SicBlockCipher(blockCipher);
                            break;

                        default:
                            throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                        }
                    }
                    catch (ArgumentException)
                    {
                        throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                    }
                }
                if (aeadBlockCipher != null)
                {
                    if (flag)
                    {
                        throw new SecurityUtilityException("CTS mode not valid for AEAD ciphers.");
                    }
                    if (flag2 && array.Length > 2 && array[2] != "")
                    {
                        throw new SecurityUtilityException("Bad padding specified for AEAD cipher.");
                    }
                    return(new BufferedAeadBlockCipher(aeadBlockCipher));
                }
                else if (blockCipher != null)
                {
                    if (flag)
                    {
                        return(new CtsBlockCipher(blockCipher));
                    }
                    if (blockCipherPadding != null)
                    {
                        return(new PaddedBufferedBlockCipher(blockCipher, blockCipherPadding));
                    }
                    if (!flag2 || blockCipher.IsPartialBlockOkay)
                    {
                        return(new BufferedBlockCipher(blockCipher));
                    }
                    return(new PaddedBufferedBlockCipher(blockCipher));
                }
                else
                {
                    if (asymmetricBlockCipher != null)
                    {
                        return(new BufferedAsymmetricBlockCipher(asymmetricBlockCipher));
                    }
                    throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                }
            }
        }
        public static IWrapper GetWrapper(string algorithm)
        {
            string text  = Platform.ToUpperInvariant(algorithm);
            string text2 = (string)WrapperUtilities.algorithms[text];

            if (text2 == null)
            {
                text2 = text;
            }
            try
            {
                switch ((WrapperUtilities.WrapAlgorithm)Enums.GetEnumValue(typeof(WrapperUtilities.WrapAlgorithm), text2))
                {
                case WrapperUtilities.WrapAlgorithm.AESWRAP:
                {
                    IWrapper result = new AesWrapEngine();
                    return(result);
                }

                case WrapperUtilities.WrapAlgorithm.CAMELLIAWRAP:
                {
                    IWrapper result = new CamelliaWrapEngine();
                    return(result);
                }

                case WrapperUtilities.WrapAlgorithm.DESEDEWRAP:
                {
                    IWrapper result = new DesEdeWrapEngine();
                    return(result);
                }

                case WrapperUtilities.WrapAlgorithm.RC2WRAP:
                {
                    IWrapper result = new RC2WrapEngine();
                    return(result);
                }

                case WrapperUtilities.WrapAlgorithm.SEEDWRAP:
                {
                    IWrapper result = new SeedWrapEngine();
                    return(result);
                }

                case WrapperUtilities.WrapAlgorithm.DESEDERFC3211WRAP:
                {
                    IWrapper result = new Rfc3211WrapEngine(new DesEdeEngine());
                    return(result);
                }

                case WrapperUtilities.WrapAlgorithm.AESRFC3211WRAP:
                {
                    IWrapper result = new Rfc3211WrapEngine(new AesFastEngine());
                    return(result);
                }

                case WrapperUtilities.WrapAlgorithm.CAMELLIARFC3211WRAP:
                {
                    IWrapper result = new Rfc3211WrapEngine(new CamelliaEngine());
                    return(result);
                }
                }
            }
            catch (ArgumentException)
            {
            }
            IBufferedCipher cipher = CipherUtilities.GetCipher(algorithm);

            if (cipher != null)
            {
                return(new WrapperUtilities.BufferedCipherWrapper(cipher));
            }
            throw new SecurityUtilityException("Wrapper " + algorithm + " not recognised.");
        }