コード例 #1
0
        private ICryptoTransform CreateTransform(byte[] rgbKey, byte[]?rgbIV, bool encrypting)
        {
            // note: rgbIV is guaranteed to be cloned before this method, so no need to clone it again

            long keySize = rgbKey.Length * (long)BitsPerByte;

            if (keySize > int.MaxValue || !((int)keySize).IsLegalSize(this.LegalKeySizes))
            {
                throw new ArgumentException(SR.Cryptography_InvalidKeySize, nameof(rgbKey));
            }

            if (rgbIV == null)
            {
                if (Mode.UsesIv())
                {
                    rgbIV = RandomNumberGenerator.GetBytes(8);
                }
            }
            else
            {
                // We truncate IV's that are longer than the block size to 8 bytes : this is
                // done to maintain backward .NET Framework compatibility with the behavior shipped in V1.x.
                // The call to set the IV in CryptoAPI will ignore any bytes after the first 8
                // bytes. We'll still reject IV's that are shorter than the block size though.
                if (rgbIV.Length < 8)
                {
                    throw new CryptographicException(SR.Cryptography_InvalidIVSize);
                }
            }

            Debug.Assert(EffectiveKeySize == KeySize);
            BasicSymmetricCipher cipher = new BasicSymmetricCipherCsp(CapiHelper.CALG_RC2, Mode, BlockSize / BitsPerByte, rgbKey, !UseSalt, rgbIV, encrypting, 0, 0);

            return(UniversalCryptoTransform.Create(Padding, cipher, encrypting));
        }
コード例 #2
0
        private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting)
        {
            // note: rgbIV is guaranteed to be cloned before this method, so no need to clone it again

            if (rgbKey == null)
            {
                throw new ArgumentNullException(nameof(rgbKey));
            }

            long keySize = rgbKey.Length * (long)BitsPerByte;

            if (keySize > int.MaxValue || !((int)keySize).IsLegalSize(LegalKeySizes))
            {
                throw new ArgumentException(SR.Cryptography_InvalidKeySize, nameof(rgbKey));
            }

            if (IsWeakKey(rgbKey))
            {
                throw new CryptographicException(SR.Cryptography_InvalidKey_Weak, "DES");
            }
            if (IsSemiWeakKey(rgbKey))
            {
                throw new CryptographicException(SR.Cryptography_InvalidKey_SemiWeak, "DES");
            }

            if (rgbIV == null)
            {
                if (Mode.UsesIv())
                {
                    rgbIV = new byte[8];
                    RandomNumberGenerator.Fill(rgbIV);
                }
            }
            else
            {
                // We truncate IV's that are longer than the block size to 8 bytes : this is
                // done to maintain backward desktop compatibility with the behavior shipped in V1.x.
                // The call to set the IV in CryptoAPI will ignore any bytes after the first 8
                // bytes. We'll still reject IV's that are shorter than the block size though.
                if (rgbIV.Length < 8)
                {
                    throw new CryptographicException(SR.Cryptography_InvalidIVSize);
                }
            }

            BasicSymmetricCipher cipher = new BasicSymmetricCipherCsp(CapiHelper.CALG_DES, Mode, BlockSize / BitsPerByte, rgbKey, 0, false, rgbIV, encrypting);

            return(UniversalCryptoTransform.Create(Padding, cipher, encrypting));
        }