当前位置 : 主页 > 网络编程 > c#编程 >

C#实现简单的文件加密与解密方式

来源:互联网 收集:自由互联 发布时间:2023-03-22
目录 C#实现文件加密与解密 C#进行url加密解密与jquery前端加密解密 C#进行url加密与解密 jquery 总结 C#实现文件加密与解密 代码: static class HandleFiles { public static void EncryptFile(string inputF
目录
  • C#实现文件加密与解密
  • C#进行url加密解密与jquery前端加密解密
    • C#进行url加密与解密
    • jquery
  • 总结

    C#实现文件加密与解密

    代码:

    static class HandleFiles
        {
            public static void EncryptFile(string inputFile, string outputFile)   //加密
            {
                try
                {
                    string password = @"12345678";
                    UnicodeEncoding UE = new UnicodeEncoding();
                    byte[] key = UE.GetBytes(password);
    
                    string cryptFile = outputFile;
                    FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
    
                    RijndaelManaged RMCrypto = new RijndaelManaged();
    
                    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.Close();
                    cs.Close();
                    fsCrypt.Close();
    
    
                    MessageBox.Show("Encrypt Source file succeed!", "Msg :");
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Source file error!", "Error :");
                }
            }
    
            public static void DecryptFile(string inputFile, string outputFile)   //解密
            {
                try
                {
                    string password = @"12345678";
                    UnicodeEncoding UE = new UnicodeEncoding();
                    byte[] key = UE.GetBytes(password);
    
                    FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
    
                    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.Close();
                    cs.Close();
                    fsCrypt.Close();
    
                    MessageBox.Show("Decrypt Source file succeed!", "Msg :");
    
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Source file error", "Error :");
                }
            }
        }
    

    C#进行url加密解密与jquery前端加密解密

    当我们程序发布于服务器上会遇到前端报错。因为有特殊原因导致。

    此时需要对传输的数据,进行加密,后台进行解密处理

    C#进行url加密与解密

    HttpUtility.UrlEncode(val);  //utf-8 编码
    HttpUtility.UrlDecode(val);  //utf-8 解码
    HttpUtility.UrlEncode(val, System.Text.Encoding.GetEncoding(936));  //gb2312编码
    HttpUtility.UrlDecode(val, System.Text.Encoding.GetEncoding(936));  //gb2312解码
    System.Web.HttpUtility.UrlEncode(val, System.Text.Encoding.GetEncoding("GB2312"));//gb2312编码
    System.Web.HttpUtility.UrlDecode(val, System.Text.Encoding.GetEncoding("GB2312"));//gb2312解码

    jquery

    decodeURIComponent(val);//Jquery解码
    encodeURIComponent(val);//Jquery编码

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

    网友评论