• 数据加密解密类(C#)


    View Code
    1 using System;
    2 using System.Data;
    3 using System.Configuration;
    4 using System.Web;
    5 using System.Web.Security;
    6 using System.Web.UI;
    7 using System.Web.UI.WebControls;
    8 using System.Web.UI.WebControls.WebParts;
    9 using System.Web.UI.HtmlControls;
    10 using System.Security.Cryptography;
    11 using System.IO;
    12
    13 /// <summary>
    14 /// 密码控制
    15 /// </summary>
    16 public class Class_Password
    17 {
    18 const string KEY_64 = "VavicApp";//注意了,是8个字符,64位
    19
    20 const string IV_64 = "VavicApp";
    21
    22 public Class_Password()
    23 {
    24 //
    25 // TODO: 在此处添加构造函数逻辑
    26 //
    27 }
    28
    29 #region EnCode 加密
    30 /// <summary>
    31 /// EnCode 加密
    32 /// </summary>
    33 /// <param name="str">要加密的字符串</param>
    34 /// <returns></returns>
    35
    36 public string EnCode(string data)
    37 {
    38 byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
    39 byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
    40
    41 DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    42 int i = cryptoProvider.KeySize;
    43 MemoryStream ms = new MemoryStream();
    44 CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey,
    45
    46 byIV), CryptoStreamMode.Write);
    47
    48 StreamWriter sw = new StreamWriter(cst);
    49 sw.Write(data);
    50 sw.Flush();
    51 cst.FlushFinalBlock();
    52 sw.Flush();
    53 return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
    54 }
    55 #endregion
    56
    57 #region DeCode 解密
    58 /// <summary>
    59 /// DeCode 解密
    60 /// </summary>
    61 /// <param name="str">要解密的字符串</param>
    62 /// <returns></returns>
    63 public string DeCode(string data)
    64 {
    65 byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
    66 byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
    67
    68 byte[] byEnc;
    69 try
    70 {
    71 byEnc = Convert.FromBase64String(data);
    72 }
    73 catch
    74 {
    75 return null;
    76 }
    77
    78 DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    79 MemoryStream ms = new MemoryStream(byEnc);
    80 CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey,
    81
    82 byIV), CryptoStreamMode.Read);
    83 StreamReader sr = new StreamReader(cst);
    84 return sr.ReadToEnd();
    85 }
    86 #endregion
    87
    88 }
  • 相关阅读:
    hdu 2222 Keywords Search 模板题
    AC自动机 (模板)
    7. 通过鼠标右键改变视角
    NGUI所见即所得之UIAtlasMaker , UIAtlas (2)
    6. 通过鼠标滑轮控制“镜头远近”
    5. Unity脚本的执行顺序
    4. 在Inspector面板中显示类中变量+ 拓展编辑器
    NGUI 的使用教程与实例(入门)(1 )
    1. 通过移动鼠标旋转摄像机观察模型
    C#面试题
  • 原文地址:https://www.cnblogs.com/jhabb/p/2015908.html
Copyright © 2020-2023  润新知