aes加解密

public class AesCryptto
{
private string key = "hjyf57468jhmuist";
private string iv = "ud547io0d98eddl2d"; /// <summary>
/// aes加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string Encrpty(string str)
{
try
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.Key = Encoding.UTF8.GetBytes(key);
aes.IV = Encoding.UTF8.GetBytes(iv);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.Zeros; MemoryStream memory = new MemoryStream(); using (CryptoStream stream = new CryptoStream(memory, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
stream.Write(buffer, , buffer.Length);
stream.FlushFinalBlock();
} string result = Convert.ToBase64String(memory.ToArray()); //对url参数进行编码,会把"/"等转码
result = Uri.EscapeDataString(result); //对url地址进行编码,不会对"/"编码,对空格和中文编码
//result = Uri.EscapeDataString(result); return result;
}
catch
{
return str;
}
} /// <summary>
/// aes解密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string DeEncrpty(string str)
{
try
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.Key = Encoding.UTF8.GetBytes(key);
aes.IV = Encoding.UTF8.GetBytes(iv);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.Zeros; str = Uri.UnescapeDataString(str);
byte[] buffer = Convert.FromBase64String(str);
MemoryStream memory = new MemoryStream(buffer); using (CryptoStream stream = new CryptoStream(memory, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
stream.Write(buffer, , buffer.Length);
stream.FlushFinalBlock(); } return Encoding.UTF8.GetString(memory.ToArray());
}
catch
{
return str;
}
}
}

对象序列化

public class SerializeHelper
{
/// <summary>
/// 把对象序列化为字符串
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public string Serialize<T>(T entity)
{
try
{
BinaryFormatter formatter = new BinaryFormatter(); MemoryStream memory = new MemoryStream(); formatter.Serialize(memory, entity); return Encoding.Default.GetString(memory.ToArray());
}
catch
{
return "";
}
} /// <summary>
/// 将序列化后的字符串反序列化为对象
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public T DeSerialize<T>(string str)
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream memory = new MemoryStream(Encoding.Default.GetBytes(str)); return (T)formatter.Deserialize(memory);
}
catch
{
return default(T);
}
}
}

对象序列化时,必须在类前面加上[Serializable()],想让某个字段不可序列化时,需要在该字段前加上[NonSerialized()],对属性无效。

[Serializable()]
public class User
{
public string LoginName { get; set; } public string Password { get; set; } [NonSerialized()]
private DateTime regTime; public DateTime RegTime
{
get { return regTime; }
set { regTime = value; }
}
}

这两个功能一起使用时可以用来在cookie中存储登陆信息等。

随机推荐

  1. 【译】Core Java Questions and Answers【1-33】

    前言 译文链接:http://www.journaldev.com/2366/core-java-interview-questions-and-answers Java 8有哪些重要的特性 Java ...

  2. 总结-Intellij Idea (快捷键 配置修改)

    忽略大小写 输入sensitive,选择Code Completion,右边第一个下拉框,选择noneEditor 鼠标悬浮show quick docEditor Editor Tabs : Mar ...

  3. linux----------ab--性能测试工具

    ab.exe –n 10000 –c 100 localhost/index.php //其中-n代表请求数,-c代表并发数

  4. 笔记本中的archlinux调节亮度

    参考了archlinux的wiki:https://wiki.archlinux.org/index.php/Backlight 说一下情况:/sys/class/backlight/acpi_vid ...

  5. 关于Maven项目

    用maven 工程搭建项目,在搭建好之后,运行时却抛出了这样的错误:  Error configuring application listener of class org.springframew ...

  6. Eclipse中@author的修改

    什么东西都可能会被忘掉,我这种记性不好的就更容易忘了.换了电脑后eclipse中的@author变了,于是找了下修改@author的方法:1. 在eclipse.ini中添加-vmargs-Duser ...

  7. CreateThread、_beginthreadex和AfxBeginThread 的区别

    CreateThread._beginthreadex和AfxBeginThread 创建线程好几个函数可以使用,可是它们有什么区别,适用于什么情况呢?参考了一些资料,写得都挺好的,这里做一些摘抄和整 ...

  8. Windows &amp; RabbitMQ:Shovel

    RabbitMQ的集群和高可用不适合跨域的情况,如果跨域可以使用Shovel或Federation. 描述:我们需要配置三台服务器:ServerA, ServerB, ServerC Step 1:安 ...

  9. CentOS下rpm命令详解

    CentOS下rpm命令详解 rpm,Redhat Package Manager,即为红帽公司为RHEL开发的专用包管理器,后来更改为RPM Package Manager,类似于GNU项目,使用递 ...

  10. vue中组件通信之子父通信

    <div id="app"> <parent-comp1></parent-comp1> <parent-comp1></pa ...