c# - 如何在c#中加密和解密pdf文件?

标签 c# visual-studio-2010

是否可以在 c# 中加密 pdf 文件并再次解密?我已经在 sql 数据库中加密了一条记录,但我需要在 pdf 文件上进行加密。我该怎么做?

最佳答案

我在项目中上传和下载文件时使用的代码,你可以使用它

喜欢

var myFile = your file;
   if (System.IO.File.Exists (myFile.Path)) {
          var decreypt = new FileEncryptDecrypt ();

          decreypt.DecryptFile (myFile.Path/*input file*/, tempFilePath/*output*/);
        } 

    using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Website.Server
{


    public class FileEncryptDecrypt
    {
      //  private const string password = @"myKey123"; // Your Key Here

        /// <summary>
        /// Encrypts a file using AES 
        /// </summary>
        /// <param name="inputFile">inputFile Path</param>
        /// <param name="outputFile">outputFile path</param>
        public  void EncryptFile(string inputFile, string outputFile)
        {

            try
            {
                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(Settings.ServerSettings.EncDecKey);

                string cryptFile = outputFile;
                FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

                //  RijndaelManaged RMCrypto = new RijndaelManaged();
                var RMCrypto = Aes.Create();
                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateEncryptor(key, key),
                    CryptoStreamMode.Write);

                FileStream fsIn = new FileStream(inputFile, FileMode.Open);

                int data;
                while ((data = fsIn.ReadByte()) != -1)
                    cs.WriteByte((byte)data);


                fsIn.Dispose();
                cs.Dispose();
                fsCrypt.Dispose();
            }
            catch(Exception ex)
            {
              var msg=ex.Message   ;  /// MessageBox.Show("Encryption failed!", "Error");
            }
        }

        /// <summary>
        /// Decrypts a file using Aes .
        /// </summary>
        /// <param name="inputFile">inputFile Path</param>
        /// <param name="outputFile">outputFile path</param>
        public  void DecryptFile(string inputFile, string outputFile)
        {
            try
            {

                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(Settings.ServerSettings.EncDecKey);

                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

                var RMCrypto = Aes.Create();
                // RijndaelManaged RMCrypto = new RijndaelManaged();

                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateDecryptor(key, key),
                    CryptoStreamMode.Read);

                FileStream fsOut = new FileStream(outputFile, FileMode.Create);

                int data;
                while ((data = cs.ReadByte()) != -1)
                    fsOut.WriteByte((byte)data);

                fsOut.Dispose();
                cs.Dispose();
                fsCrypt.Dispose();

            }
            catch(Exception ex)
            {
               var msg=ex .Message  ;
            }
        }

        private static void EncryptData(String inName, String outName, byte[] rijnKey, byte[] rijnIV)
        {
            //Create the file streams to handle the input and output files.
            FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
            fout.SetLength(0);

            //Create variables to help with read and write.
            byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
            long rdlen = 0;              //This is the total number of bytes written.
            long totlen = fin.Length;    //This is the total length of the input file.
            int len;                     //This is the number of bytes to be written at a time.

            var rijn = System.Security.Cryptography.Aes.Create();

            //   SymmetricAlgorithm rijn = new SymmetricAlgorithm .Create(); //Creates the default implementation, which is RijndaelManaged.         
            CryptoStream encStream = new CryptoStream(fout, rijn.CreateEncryptor(rijnKey, rijnIV), CryptoStreamMode.Write);

            Console.WriteLine("Encrypting...");

            //Read from the input file, then encrypt and write to the output file.
            while (rdlen < totlen)
            {
                len = fin.Read(bin, 0, 100);
                encStream.Write(bin, 0, len);
                rdlen = rdlen + len;
                Console.WriteLine("{0} bytes processed", rdlen);
            }

            encStream.Dispose();
            fout.Dispose();
            fin.Dispose();
        }
    }
}

关于c# - 如何在c#中加密和解密pdf文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30474317/

相关文章:

c# - 哪个更适合获取程序集位置,GetAssembly().Location 或 GetExecutingAssembly().Location

Visual Studio 2010 中的 Rest 初学者工具包

visual-studio-2010 - 将文件添加到CMake,但不进行编译

c++ - 在加载 MSVisual C++ 时重命名窗体

c# - 如何让光盘在 DVD ROM 驱动器中保持旋转?

c# - Web 引用服务错误

c# - 尽管提供了 "Everyone"访问权限,IIS 应用程序仍无法访问路径

c# - 是否有 LINQ 等效方法?

vb.net - 算术运算导致溢出

wpf - 如何将 WPF 控件绑定(bind)到 VB.net 属性?