开发者

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

开发者 https://www.devze.com 2023-01-26 10:20 出处:网络 作者: Danny_hi
目录C#实现文件加密与解密C#进行url加密解密与jquery前端加密解密C#进行url加密与解密jquery总结C#实现文件加密与解密
目录
  • C#实现文件加密与解密
  • C#进行url加密解密与jquery前端加密解密
    • C#进行url加密与解密
    • jquery
  • 总结

    C#实现文件加密与解密

    代码:

    static class HandleFiles
        {
            public static void EncphpryptFile(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();
    
                    Cry开发者_JS开发ptoStream 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)python
                        fsOut.WriteByte((byte)data);
    
                    fsOut.Close();
                    cs.Close();
                    fsCrypt.Close();
    
                    MessageBox.ShocPWuvw("Decrypt Source file succeed!", javascript"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"));//gbcPWuv2312编码
    System.Web.HttpUtility.UrlDecode(val, System.Text.Encoding.GetEncoding("GB2312"));//gb2312解码

    jquery

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

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消