예제 #1
0
        public byte[] Decrypt(byte[] data)
        {
            if (ModeSignature.Equals("CBC") || ModeSignature.Equals("ECB"))
            {
                using var aes = new AesManaged
                      {
                          Mode = AlgorithmUtility.GetCipherMode(ModeSignature)
                      };

                using var decryptor = aes.CreateDecryptor(Key, IV);
                using var ms        = new MemoryStream(data);
                using var cs        = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

                var decrypted = new byte[data.Length];
                var bytesRead = cs.Read(decrypted, 0, decrypted.Length);

                return(decrypted.Take(bytesRead).ToArray());
            }
            else // OFB or CFB
            {
                byte[] decrypted;
                var    aes = CreateAesCipher(false);

                try
                {
                    decrypted = new byte[aes.GetOutputSize(data.Length)];

                    var len = aes.ProcessBytes(data, 0, data.Length, decrypted, 0);
                    aes.DoFinal(decrypted, len);

                    return(decrypted);
                }
                catch (CryptoException)
                {
                }
            }

            return(null);
        }
예제 #2
0
        public byte[] Decrypt(byte[] data)
        {
            if (!ModeSignature.Equals("OFB"))
            {
                using var tdes = new TripleDESCryptoServiceProvider
                {
                    Mode = AlgorithmUtility.GetCipherMode(ModeSignature)
                };

                using var decryptor = tdes.CreateDecryptor(Key, IV);
                using var ms = new MemoryStream(data);
                using var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

                var decrypted = new byte[data.Length];
                var bytesRead = cs.Read(decrypted, 0, data.Length);

                return decrypted.Take(bytesRead).ToArray();
            }
            else
            {
                byte[] decrypted;
                var tdes = CreateTripleDesCipher(false);

                try
                {
                    decrypted = new byte[tdes.GetOutputSize(data.Length)];

                    var len = tdes.ProcessBytes(data, 0, data.Length, decrypted, 0);
                    tdes.DoFinal(decrypted, len);

                    return decrypted;
                }
                catch (CryptoException)
                {
                }
            }

            return null;
        }
예제 #3
0
        public byte[] Encrypt(byte[] data)
        {
            if (ModeSignature.Equals("CBC") || ModeSignature.Equals("ECB"))
            {
                using var aes = new AesManaged
                      {
                          Mode = AlgorithmUtility.GetCipherMode(ModeSignature)
                      };

                using var encryptor = aes.CreateEncryptor(Key, IV);
                using var ms        = new MemoryStream();
                using var writer    = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);

                writer.Write(data, 0, data.Length);
                writer.FlushFinalBlock();

                return(ms.ToArray());
            }
            else // OFB or CFB
            {
                byte[] encrypted;
                var    aes = CreateAesCipher(true);

                try
                {
                    encrypted = new byte[aes.GetOutputSize(data.Length)];

                    var len = aes.ProcessBytes(data, 0, data.Length, encrypted, 0);
                    aes.DoFinal(encrypted, len);

                    return(encrypted);
                }
                catch (CryptoException)
                {
                }

                return(null);
            }
        }
예제 #4
0
        public byte[] Encrypt(byte[] data)
        {
            if (!ModeSignature.Equals("OFB"))
            {
                using var tdes = new TripleDESCryptoServiceProvider
                {
                    Mode = AlgorithmUtility.GetCipherMode(ModeSignature)
                };
                //tdes.Padding = PaddingMode.PKCS7;
                using var encryptor = tdes.CreateEncryptor(Key, IV);
                using var ms = new MemoryStream();
                using var writer = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);

                writer.Write(data, 0, data.Length);
                writer.FlushFinalBlock();
                return ms.ToArray();
            }
            else
            {
                byte[] encrypted;
                var tdes = CreateTripleDesCipher(true);

                try
                {
                    encrypted = new byte[tdes.GetOutputSize(data.Length)];

                    var len = tdes.ProcessBytes(data, 0, data.Length, encrypted, 0);
                    tdes.DoFinal(encrypted, len);

                    return encrypted;
                }
                catch (CryptoException)
                {
                }

            }
            return null;
        }