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

如何在c#中加密和解密pdf文件?,c#,visual-studio-2010,C#,Visual Studio 2010,是否可以用c#加密pdf文件,然后再次解密?我在sql数据库中加密了一条记录,但我需要在pdf文件中加密。我该怎么做呢?这段代码是我在项目中上传和下载文件时使用的,你可以使用它 像 使用系统; 使用System.Collections.Generic; 使用System.IO; 使用System.Linq; 使用System.Security.Cryptography; 使用系统文本; 使用System.Threading.Tasks; 名称空间网站.Server { 公共类文件加密解密 {

是否可以用c#加密pdf文件,然后再次解密?我在sql数据库中加密了一条记录,但我需要在pdf文件中加密。我该怎么做呢?

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


使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Security.Cryptography;
使用系统文本;
使用System.Threading.Tasks;
名称空间网站.Server
{
公共类文件加密解密
{
//private const string password=@“myKey123”//此处是您的密钥
/// 
///使用AES加密文件
/// 
///输入文件路径
///输出文件路径
公共无效加密文件(字符串输入文件、字符串输出文件)
{
尝试
{
Unicode编码UE=新的Unicode编码();
byte[]key=UE.GetBytes(Settings.ServerSettings.EncDecKey);
字符串cryptFile=outputFile;
FileStream fsCrypt=newfilestream(cryptFile,FileMode.Create);
//RijndaelManaged RMCrypto=新的RijndaelManaged();
var RMCrypto=Aes.Create();
CryptoStream cs=新加密流(fsCrypt,
RMCrypto.CreateEncryptor(密钥,密钥),
CryptoStreamMode.Write);
FileStream fsIn=newfilestream(inputFile,FileMode.Open);
int数据;
而((data=fsIn.ReadByte())!=-1)
cs.WriteByte((字节)数据);
fsIn.Dispose();
cs.Dispose();
fsCrypt.Dispose();
}
捕获(例外情况除外)
{
var msg=ex.Message;///MessageBox.Show(“加密失败!”,“错误”);
}
}
/// 
///使用Aes解密文件。
/// 
///输入文件路径
///输出文件路径
公共无效解密文件(字符串输入文件、字符串输出文件)
{
尝试
{
Unicode编码UE=新的Unicode编码();
byte[]key=UE.GetBytes(Settings.ServerSettings.EncDecKey);
FileStream fsCrypt=newfilestream(inputFile,FileMode.Open);
var RMCrypto=Aes.Create();
//RijndaelManaged RMCrypto=新的RijndaelManaged();
CryptoStream cs=新加密流(fsCrypt,
RMCrypto.CreateDecryptor(密钥,密钥),
CryptoStreamMode.Read);
FileStream fsOut=newfilestream(outputFile,FileMode.Create);
int数据;
而((data=cs.ReadByte())!=-1)
写字节((字节)数据);
fsOut.Dispose();
cs.Dispose();
fsCrypt.Dispose();
}
捕获(例外情况除外)
{
var msg=ex.Message;
}
}
私有静态void EncryptData(字符串inName,字符串outName,字节[]rijnKey,字节[]rijnIV)
{
//创建文件流以处理输入和输出文件。
FileStream fin=newfilestream(inName,FileMode.Open,FileAccess.Read);
FileStream fout=newfilestream(outName,FileMode.OpenOrCreate,FileAccess.Write);
fout.设置长度(0);
//创建变量以帮助读取和写入。
byte[]bin=新字节[100];//这是加密的中间存储。
long rdlen=0;//这是写入的总字节数。
long totlen=fin.Length;//这是输入文件的总长度。
int len;//这是一次要写入的字节数。
var rijn=System.Security.Cryptography.Aes.Create();
//SymmetricAlgorithm rijn=新建SymmetricAlgorithm.Create();//创建默认实现,即RijndaelManaged。
CryptoStream encStream=新的加密流(fout,rijn.CreateEncryptor(rijnKey,rijnIV),CryptoStreamMode.Write);
Console.WriteLine(“加密…”);
//读取输入文件,然后加密并写入输出文件。
而(rdlen
使用合适的库。软件推荐有自己的网站,在SOPossible上是离题的。看看
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();
        }
    }
}