C# File, Stream, Byte[]转化

public class FileUtil
    {
        public static readonly FileUtil Instance = new FileUtil();

        private static byte[] key = Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOP"); // 128位密钥
        private static byte[] iv = Encoding.UTF8.GetBytes("1234567812345678");  // 128位初始化向量

        //对byte[] AES加密解密
        public byte[] Encrypt(byte[] pdfBytes)
        {
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = key;
                aesAlg.IV = iv;

                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(pdfBytes, 0, pdfBytes.Length);
                        csEncrypt.FlushFinalBlock();
                        return msEncrypt.ToArray();
                    }
                }
            }
        }

        public byte[] Decrypt(byte[] encryptedBytes)
        {
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = key;
                aesAlg.IV = iv;

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                using (MemoryStream msDecrypt = new MemoryStream(encryptedBytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        byte[] decryptedBytes = new byte[encryptedBytes.Length];
                        csDecrypt.Read(decryptedBytes, 0, decryptedBytes.Length);
                        return decryptedBytes;
                    }
                }
            }
        }

        public byte[] File2Byte(string filePath)
        {
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

            byte[] bInfile = new byte[(int)fs.Length];

            fs.Read(bInfile, 0, bInfile.Length);

            fs.Close();

            return bInfile;
        }

        public void Byte2File(byte[] bArr, string outFilePath)
        {
            FileStream fsout = new FileStream(outFilePath, FileMode.Create, FileAccess.Write);

            fsout.Write(bArr, 0, bArr.Length);

            fsout.Close();
        }

        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        public Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }

        public void StreamToFile(Stream stream, string fileName)
        {
            // 把 Stream 转换成 byte[]
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            // 把 byte[] 写入文件
            FileStream fs = new FileStream(fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }

        public Stream FileToStream(string fileName)
        {
            // 打开文件
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            // 读取文件的 byte[]
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();
            // 把 byte[] 转换成 Stream
            Stream stream = new MemoryStream(bytes);
            return stream;
        }




        //将指定路径pdf加密生成至指定路径
        public void PdfEnc(string inputPath, string outputPath)
        {
            byte[] b = File2Byte(inputPath);
            byte[] bAfter = Encrypt(b);
            Byte2File(bAfter, outputPath);
        }

        //将指定路径加密的pdf解密,输出Strem/byte[]/生成解密文件至指定路径
        public void PdfDec(string filePath2Dec)
        {
            byte[] b = File2Byte(filePath2Dec);
            byte[] bAfter = Decrypt(b);

            将byte转为stream,再生成文件
            Stream stream = BytesToStream(bAfter);
            StreamToFile(stream, @"F:\test\解密Stream.pdf");

            直接通过byte[]生成文件
            //Byte2File(bAfter, @"F:\test\解密B.pdf");
        }
    }

参考文档:https://www.cnblogs.com/Mr_JinRui/archive/2010/07/05/1771184.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值