Пример #1
0
        private static Stream CreateStream(Stream s, bool bEncrypt, byte[] pbKey, byte[] pbIV)
        {
            StandardAesEngine.ValidateArguments(s, bEncrypt, pbKey, pbIV);

#if 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);

            RijndaelManaged r = new RijndaelManaged();

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

            byte[] pbLocalKey = new byte[32];
            Array.Copy(pbKey, pbLocalKey, 32);
            r.KeySize = 256;
            r.Key     = pbLocalKey;

            r.Mode    = m_rCipherMode;
            r.Padding = m_rCipherPadding;

            ICryptoTransform iTransform = (bEncrypt ? r.CreateEncryptor() : r.CreateDecryptor());

            Debug.Assert(iTransform != null);
            if (iTransform == null)
            {
                throw new SecurityException("Unable to create Rijndael transform!");
            }

            return(new CryptoStream(s, iTransform, bEncrypt ? CryptoStreamMode.Write :
                                    CryptoStreamMode.Read));
        }
Пример #3
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 KeePassUWP
            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
        }
Пример #4
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 !KeePassRT
            RijndaelManaged r = new RijndaelManaged();
            if (r.BlockSize != 128)            // AES block size
            {
                Debug.Assert(false);
                r.BlockSize = 128;
            }

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

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

            return(new CryptoStream(s, iTransform, bEncrypt ? CryptoStreamMode.Write :
                                    CryptoStreamMode.Read));
#else
            AesEngine                 aes = new AesEngine();
            CbcBlockCipher            cbc = new CbcBlockCipher(aes);
            PaddedBufferedBlockCipher bc  = new PaddedBufferedBlockCipher(cbc,
                                                                          new Pkcs7Padding());
            KeyParameter     kp    = new KeyParameter(pbLocalKey);
            ParametersWithIV prmIV = new ParametersWithIV(kp, pbLocalIV);
            bc.Init(bEncrypt, prmIV);

            IBufferedCipher cpRead  = (bEncrypt ? null : bc);
            IBufferedCipher cpWrite = (bEncrypt ? bc : null);
            return(new CipherStream(s, cpRead, cpWrite));
#endif
        }
Пример #5
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 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
        }