Beispiel #1
0
        // Constructor.
        internal CryptoAPITransform(int algorithm, byte[] iv, byte[] key,
                                    int blockSize, int feedbackBlockSize,
                                    CipherMode mode, PaddingMode padding,
                                    bool encrypt)
        {
            // Initialize the common state.
            if (iv != null)
            {
                this.iv = (byte[])(iv.Clone());
            }
            else
            {
                this.iv = null;
            }
            this.blockSize         = blockSize / 8;
            this.feedbackBlockSize = feedbackBlockSize / 8;
            this.padding           = padding;
            this.mode = mode;

            // Determine which processing methods to use based on the
            // mode and the encrypt/decrypt flag.
            switch (mode)
            {
            case CipherMode.CBC:
            {
                // Cipher Block Chaining Mode.
                if (encrypt)
                {
                    state        = CryptoMethods.EncryptCreate(algorithm, key);
                    processBlock = new ProcessBlock
                                       (CBCEncrypt.TransformBlock);
                    processFinal = new ProcessFinal
                                       (CBCEncrypt.TransformFinalBlock);
                }
                else
                {
                    CBCDecrypt.Initialize(this);
                    state        = CryptoMethods.DecryptCreate(algorithm, key);
                    processBlock = new ProcessBlock
                                       (CBCDecrypt.TransformBlock);
                    processFinal = new ProcessFinal
                                       (CBCDecrypt.TransformFinalBlock);
                }
            }
            break;

            case CipherMode.ECB:
            {
                // Electronic Code Book Mode.
                if (encrypt)
                {
                    state        = CryptoMethods.EncryptCreate(algorithm, key);
                    processBlock = new ProcessBlock
                                       (ECBEncrypt.TransformBlock);
                    processFinal = new ProcessFinal
                                       (ECBEncrypt.TransformFinalBlock);
                }
                else
                {
                    ECBDecrypt.Initialize(this);
                    state        = CryptoMethods.DecryptCreate(algorithm, key);
                    processBlock = new ProcessBlock
                                       (ECBDecrypt.TransformBlock);
                    processFinal = new ProcessFinal
                                       (ECBDecrypt.TransformFinalBlock);
                }
            }
            break;

            case CipherMode.OFB:
            {
                // Output Feed Back Mode.
                OFBEncrypt.Initialize(this);
                state        = CryptoMethods.EncryptCreate(algorithm, key);
                processBlock = new ProcessBlock
                                   (OFBEncrypt.TransformBlock);
                processFinal = new ProcessFinal
                                   (OFBEncrypt.TransformFinalBlock);
            }
            break;

            case CipherMode.CFB:
            {
                // Cipher Feed Back Mode.
                if (encrypt)
                {
                    CFBEncrypt.Initialize(this);
                    state        = CryptoMethods.EncryptCreate(algorithm, key);
                    processBlock = new ProcessBlock
                                       (CFBEncrypt.TransformBlock);
                    processFinal = new ProcessFinal
                                       (CFBEncrypt.TransformFinalBlock);
                }
                else
                {
                    CFBDecrypt.Initialize(this);
                    state        = CryptoMethods.EncryptCreate(algorithm, key);
                    processBlock = new ProcessBlock
                                       (CFBDecrypt.TransformBlock);
                    processFinal = new ProcessFinal
                                       (CFBDecrypt.TransformFinalBlock);
                }
            }
            break;

            case CipherMode.CTS:
            {
                // Cipher Text Stealing Mode.
                if (encrypt)
                {
                    CTSEncrypt.Initialize(this);
                    state        = CryptoMethods.EncryptCreate(algorithm, key);
                    processBlock = new ProcessBlock
                                       (CTSEncrypt.TransformBlock);
                    processFinal = new ProcessFinal
                                       (CTSEncrypt.TransformFinalBlock);
                }
                else
                {
                    // We need an encryptor as well to handle
                    // streams with only a single block in them.
                    CTSDecrypt.Initialize(this);
                    state  = CryptoMethods.DecryptCreate(algorithm, key);
                    state2 = CryptoMethods.EncryptCreate
                                 (algorithm, key);
                    processBlock = new ProcessBlock
                                       (CTSDecrypt.TransformBlock);
                    processFinal = new ProcessFinal
                                       (CTSDecrypt.TransformFinalBlock);
                }
            }
            break;
            }
        }