예제 #1
0
        private ICryptoTransform NewEncryptor (byte[] rgbKey,
                                               CipherMode mode,
                                               byte[] rgbIV,
                                               int feedbackSize,
                                               RijndaelManagedTransformMode encryptMode) {
            // Build the key if one does not already exist
            if (rgbKey == null) {
                rgbKey = Utils.GenerateRandom(KeySizeValue / 8);
            }

            // If not ECB mode, make sure we have an IV. In CoreCLR we do not support ECB, so we must have
            // an IV in all cases.
#if !FEATURE_CRYPTO
            if (mode != CipherMode.ECB) {
#endif // !FEATURE_CRYPTO
                if (rgbIV == null) {
                    rgbIV = Utils.GenerateRandom(BlockSizeValue / 8);
                }
#if !FEATURE_CRYPTO
            }
#endif // !FEATURE_CRYPTO

            // Create the encryptor/decryptor object
            return new RijndaelManagedTransform (rgbKey,
                                                 mode,
                                                 rgbIV,
                                                 BlockSizeValue,
                                                 feedbackSize,
                                                 PaddingValue,
                                                 encryptMode);
        }
 private ICryptoTransform NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int feedbackSize, RijndaelManagedTransformMode encryptMode)
 {
     if (rgbKey == null)
     {
         rgbKey = Utils.GenerateRandom(base.KeySizeValue / 8);
     }
     if (rgbIV == null)
     {
         rgbIV = Utils.GenerateRandom(base.BlockSizeValue / 8);
     }
     return new RijndaelManagedTransform(rgbKey, mode, rgbIV, base.BlockSizeValue, feedbackSize, base.PaddingValue, encryptMode);
 }
        internal RijndaelManagedTransform (byte[] rgbKey,
                                           CipherMode mode,
                                           byte[] rgbIV,
                                           int blockSize,
                                           int feedbackSize,
                                           PaddingMode PaddingValue,
                                           RijndaelManagedTransformMode transformMode) {
            if (rgbKey == null)
                throw new ArgumentNullException("rgbKey");
            Contract.EndContractBlock();

#if !FEATURE_CRYPTO
            Contract.Assert(mode == CipherMode.CBC, "CipherMode is unsupported on CoreCLR");
            Contract.Assert(PaddingValue == PaddingMode.PKCS7, "PaddingMode is unsupported on CoreCLR");
#endif // !FEATURE_CRYPTO

            m_blockSizeBits = blockSize;
            m_blockSizeBytes = blockSize / 8;
            m_cipherMode = mode;
            m_paddingValue = PaddingValue;
            m_transformMode = transformMode;
            m_Nb = blockSize / 32;
            m_Nk = rgbKey.Length / 4;

            int S1 = m_Nb > 6 ? 3 : 2;
            int S2 = m_Nb > 6 ? 4 : 3;

            // Precompute the modulus operations: these are performance killers when called frequently
            int[] encryptindex1 = new int[m_Nb];
            int[] encryptindex2 = new int[m_Nb];
            int[] encryptindex3 = new int[m_Nb];

            int[] decryptindex1 = new int[m_Nb];
            int[] decryptindex2 = new int[m_Nb];
            int[] decryptindex3 = new int[m_Nb];

            for (int j=0; j < m_Nb; j++) {
                encryptindex1[j] = (j + 1) % m_Nb;
                encryptindex2[j] = (j + S1) % m_Nb;
                encryptindex3[j] = (j + S2) % m_Nb;
                decryptindex1[j] = (j -1  + m_Nb) % m_Nb;
                decryptindex2[j] = (j -S1 + m_Nb) % m_Nb;
                decryptindex3[j] = (j -S2 + m_Nb) % m_Nb;
            }

            m_encryptindex = new int[m_Nb * 3];
            Array.Copy(encryptindex1, 0, m_encryptindex, 0, m_Nb);
            Array.Copy(encryptindex2, 0, m_encryptindex, m_Nb, m_Nb);
            Array.Copy(encryptindex3, 0, m_encryptindex, m_Nb * 2, m_Nb);

            m_decryptindex = new int[m_Nb * 3];
            Array.Copy(decryptindex1, 0, m_decryptindex, 0, m_Nb);
            Array.Copy(decryptindex2, 0, m_decryptindex, m_Nb, m_Nb);
            Array.Copy(decryptindex3, 0, m_decryptindex, m_Nb * 2, m_Nb);

            switch (m_cipherMode) {
                case CipherMode.ECB:
                case CipherMode.CBC:
                    m_inputBlockSize = m_blockSizeBytes;
                    m_outputBlockSize = m_blockSizeBytes;
                    break;

                case CipherMode.CFB:
                    m_inputBlockSize = feedbackSize / 8;
                    m_outputBlockSize = feedbackSize / 8;
                    break;

                default:
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidCipherMode"));
            }

            // On CorECLR we only support CBC mode
#if FEATURE_CRYPTO
            if (mode == CipherMode.CBC || mode == CipherMode.CFB) {
#endif // FEATURE_CRYPTO
                if (rgbIV == null)
                    throw new ArgumentNullException("rgbIV");
                if (rgbIV.Length / 4 != m_Nb)
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize"));

                m_IV = new int[m_Nb];
                int index = 0;
                for (int i = 0; i < m_Nb; ++i) {
                    int i0 = rgbIV[index++];
                    int i1 = rgbIV[index++];
                    int i2 = rgbIV[index++];
                    int i3 = rgbIV[index++];
                    m_IV[i] = i3 << 24 | i2 << 16 | i1 << 8 | i0;
                }
#if FEATURE_CRYPTO
            }
#endif // FEATURE_CRYPTO

            GenerateKeyExpansion(rgbKey);

#if FEATURE_CRYPTO // see code:System.Security.Cryptography.RijndaelManaged#CoreCLRRijndaelModes
            if (m_cipherMode == CipherMode.CBC) {
#endif // FEATURE_CRYPTO
                m_lastBlockBuffer = new int[m_Nb];
                Buffer.InternalBlockCopy(m_IV, 0, m_lastBlockBuffer, 0, m_blockSizeBytes);
#if FEATURE_CRYPTO
            }
#endif // FEATURE_CRYPTO

#if FEATURE_CRYPTO
            if (m_cipherMode == CipherMode.CFB) {
                m_shiftRegister = new byte[4*m_Nb];
                Buffer.InternalBlockCopy(m_IV, 0, m_shiftRegister, 0, 4*m_Nb);
            }
#endif // FEATURE_CRYPTO
        }
예제 #4
0
 // Token: 0x0600231C RID: 8988 RVA: 0x0007DE70 File Offset: 0x0007C070
 private ICryptoTransform NewEncryptor(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int feedbackSize, RijndaelManagedTransformMode encryptMode)
 {
     if (rgbKey == null)
     {
         rgbKey = Utils.GenerateRandom(this.KeySizeValue / 8);
     }
     if (rgbIV == null)
     {
         rgbIV = Utils.GenerateRandom(this.BlockSizeValue / 8);
     }
     return(new RijndaelManagedTransform(rgbKey, mode, rgbIV, this.BlockSizeValue, feedbackSize, this.PaddingValue, encryptMode));
 }
        internal RijndaelManagedTransform(byte[] rgbKey, CipherMode mode, byte[] rgbIV, int blockSize, int feedbackSize, PaddingMode PaddingValue, RijndaelManagedTransformMode transformMode)
        {
            if (rgbKey == null)
            {
                throw new ArgumentNullException("rgbKey");
            }
            this.m_blockSizeBits = blockSize;
            this.m_blockSizeBytes = blockSize / 8;
            this.m_cipherMode = mode;
            this.m_paddingValue = PaddingValue;
            this.m_transformMode = transformMode;
            this.m_Nb = blockSize / 0x20;
            this.m_Nk = rgbKey.Length / 4;
            int num = (this.m_Nb > 6) ? 3 : 2;
            int num2 = (this.m_Nb > 6) ? 4 : 3;
            int[] sourceArray = new int[this.m_Nb];
            int[] numArray2 = new int[this.m_Nb];
            int[] numArray3 = new int[this.m_Nb];
            int[] numArray4 = new int[this.m_Nb];
            int[] numArray5 = new int[this.m_Nb];
            int[] numArray6 = new int[this.m_Nb];
            for (int i = 0; i < this.m_Nb; i++)
            {
                sourceArray[i] = (i + 1) % this.m_Nb;
                numArray2[i] = (i + num) % this.m_Nb;
                numArray3[i] = (i + num2) % this.m_Nb;
                numArray4[i] = ((i - 1) + this.m_Nb) % this.m_Nb;
                numArray5[i] = ((i - num) + this.m_Nb) % this.m_Nb;
                numArray6[i] = ((i - num2) + this.m_Nb) % this.m_Nb;
            }
            this.m_encryptindex = new int[this.m_Nb * 3];
            Array.Copy(sourceArray, 0, this.m_encryptindex, 0, this.m_Nb);
            Array.Copy(numArray2, 0, this.m_encryptindex, this.m_Nb, this.m_Nb);
            Array.Copy(numArray3, 0, this.m_encryptindex, this.m_Nb * 2, this.m_Nb);
            this.m_decryptindex = new int[this.m_Nb * 3];
            Array.Copy(numArray4, 0, this.m_decryptindex, 0, this.m_Nb);
            Array.Copy(numArray5, 0, this.m_decryptindex, this.m_Nb, this.m_Nb);
            Array.Copy(numArray6, 0, this.m_decryptindex, this.m_Nb * 2, this.m_Nb);
            switch (this.m_cipherMode)
            {
                case CipherMode.CBC:
                case CipherMode.ECB:
                    this.m_inputBlockSize = this.m_blockSizeBytes;
                    this.m_outputBlockSize = this.m_blockSizeBytes;
                    break;

                case CipherMode.CFB:
                    this.m_inputBlockSize = feedbackSize / 8;
                    this.m_outputBlockSize = feedbackSize / 8;
                    break;

                default:
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidCipherMode"));
            }
            if ((mode == CipherMode.CBC) || (mode == CipherMode.CFB))
            {
                if (rgbIV == null)
                {
                    throw new ArgumentNullException("rgbIV");
                }
                if ((rgbIV.Length / 4) != this.m_Nb)
                {
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize"));
                }
                this.m_IV = new int[this.m_Nb];
                int num4 = 0;
                for (int j = 0; j < this.m_Nb; j++)
                {
                    int num6 = rgbIV[num4++];
                    int num7 = rgbIV[num4++];
                    int num8 = rgbIV[num4++];
                    int num9 = rgbIV[num4++];
                    this.m_IV[j] = (((num9 << 0x18) | (num8 << 0x10)) | (num7 << 8)) | num6;
                }
            }
            this.GenerateKeyExpansion(rgbKey);
            if (this.m_cipherMode == CipherMode.CBC)
            {
                this.m_lastBlockBuffer = new int[this.m_Nb];
                Buffer.InternalBlockCopy(this.m_IV, 0, this.m_lastBlockBuffer, 0, this.m_blockSizeBytes);
            }
            if (this.m_cipherMode == CipherMode.CFB)
            {
                this.m_shiftRegister = new byte[4 * this.m_Nb];
                Buffer.InternalBlockCopy(this.m_IV, 0, this.m_shiftRegister, 0, 4 * this.m_Nb);
            }
        }