コード例 #1
0
        private static Stream CreateStream(Stream s, bool bEncrypt, byte[] pbKey, byte[] pbIV)
        {
            StandardAesEngine.ValidateArguments(s, bEncrypt, pbKey, pbIV);

#if ModernKeePassLib || KeePassUAP
            return(StandardAesEngineExt.CreateStream(s, bEncrypt, pbKey, pbIV));
#else
            SymmetricAlgorithm a = CryptoUtil.CreateAes();
            if (a.BlockSize != 128)            // AES block size
            {
                Debug.Assert(false);
                a.BlockSize = 128;
            }
            a.KeySize = 256;
            a.Mode    = SaeCipherMode;
            a.Padding = SaePaddingMode;

            ICryptoTransform t;
            if (bEncrypt)
            {
                t = a.CreateEncryptor(pbKey, pbIV);
            }
            else
            {
                t = a.CreateDecryptor(pbKey, pbIV);
            }
            if (t == null)
            {
                Debug.Assert(false); throw new SecurityException("Unable to create AES transform!");
            }

            return(new CryptoStreamEx(s, t, bEncrypt ? CryptoStreamMode.Write :
                                      CryptoStreamMode.Read, a));
#endif
        }
コード例 #2
0
        private static Stream CreateStream(Stream s, bool bEncrypt, byte[] pbKey, byte[] pbIV)
        {
            StandardAesEngine.ValidateArguments(s, bEncrypt, pbKey, pbIV);

            byte[] pbLocalIV = new byte[16];
            Array.Copy(pbIV, pbLocalIV, 16);

            byte[] pbLocalKey = new byte[32];
            Array.Copy(pbKey, pbLocalKey, 32);

#if ModernKeePassLib
            var cbc = new CbcBlockCipher(new AesEngine());
            var bc  = new PaddedBufferedBlockCipher(cbc,
                                                    new Pkcs7Padding());
            var kp    = new KeyParameter(pbLocalKey);
            var prmIV = new ParametersWithIV(kp, pbLocalIV);
            bc.Init(bEncrypt, prmIV);

            var cpRead  = (bEncrypt ? null : bc);
            var cpWrite = (bEncrypt ? bc : null);
            return(new CipherStream(s, cpRead, cpWrite));
#elif KeePassUAP
            return(StandardAesEngineExt.CreateStream(s, bEncrypt, pbLocalKey, pbLocalIV));
#else
            SymmetricAlgorithm a = CryptoUtil.CreateAes();
            if (a.BlockSize != 128)            // AES block size
            {
                Debug.Assert(false);
                a.BlockSize = 128;
            }

            a.IV      = pbLocalIV;
            a.KeySize = 256;
            a.Key     = pbLocalKey;
            a.Mode    = m_rCipherMode;
            a.Padding = m_rCipherPadding;

            ICryptoTransform iTransform = (bEncrypt ? a.CreateEncryptor() : a.CreateDecryptor());
            Debug.Assert(iTransform != null);
            if (iTransform == null)
            {
                throw new SecurityException("Unable to create AES transform!");
            }

            return(new CryptoStream(s, iTransform, bEncrypt ? CryptoStreamMode.Write :
                                    CryptoStreamMode.Read));
#endif
        }