Exemplo n.º 1
0
 public static byte[] RemoveEncryptHeader(byte[] datas)
 {
     if (datas != null)
     {
         return(FileHeaderUtil.RemoveEncryptHeader(new MemoryStream(datas)));
     }
     return(null);
 }
Exemplo n.º 2
0
        public static byte[] EncryptBytes(byte[] data, bool header = true)
        {
            if (header && FileHeaderUtil.CheckHeaderIsEncrypt(data))
            {
                return(null);
            }
            ICryptoTransform cryptoTransform = AESSecurity.aesManaged.CreateEncryptor();
            MemoryStream     memoryStream    = new MemoryStream();
            CryptoStream     cryptoStream    = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write);

            cryptoStream.Write(data, 0, data.Length);
            cryptoStream.Close();
            byte[] array = (!header) ? memoryStream.ToArray() : FileHeaderUtil.AddEncryptHeader(memoryStream);
            memoryStream.Dispose();
            return(array);
        }
Exemplo n.º 3
0
        public static byte[] DecryptBytes(byte[] encryptBytes, bool header = true)
        {
            if (header && !FileHeaderUtil.CheckHeaderIsEncrypt(encryptBytes))
            {
                return(null);
            }
            ICryptoTransform cryptoTransform = AESSecurity.aesManaged.CreateDecryptor();

            byte[] result = null;
            using (MemoryStream memoryStream = new MemoryStream(encryptBytes))
            {
                if (header)
                {
                    BinaryReader binaryReader = FileHeaderUtil.RemoveEncryptHeaderStream <MemoryStream>(memoryStream);
                }
                result = cryptoTransform.TransformFinalBlock(encryptBytes, Convert.ToInt32(memoryStream.Position), encryptBytes.Length - Convert.ToInt32(memoryStream.Position));
            }
            return(result);
        }
Exemplo n.º 4
0
 public static byte[] DecryptFile(string path)
 {
     byte[] result = null;
     using (FileStream fileStream = new FileStream(path, FileMode.Open))
     {
         BinaryReader     binaryReader    = FileHeaderUtil.RemoveEncryptHeaderStream <FileStream>(fileStream);
         ICryptoTransform cryptoTransform = AESSecurity.aesManaged.CreateDecryptor();
         using (CryptoStream cryptoStream = new CryptoStream(fileStream, cryptoTransform, 0))
         {
             byte[] array = new byte[1048576];
             using (MemoryStream memoryStream = new MemoryStream(Convert.ToInt32(fileStream.Length) + AESSecurity.aesManaged.BlockSize))
             {
                 int num;
                 while ((num = cryptoStream.Read(array, 0, array.Length)) > 0)
                 {
                     memoryStream.Write(array, 0, num);
                 }
                 result = memoryStream.ToArray();
             }
         }
         binaryReader.Close();
     }
     return(result);
 }
Exemplo n.º 5
0
 public static bool CheckHeaderIsEncrypt(byte[] datas)
 {
     return(datas != null && FileHeaderUtil.CheckHeaderIsEncrypt(new MemoryStream(datas)));
 }