asp.net c#中的加密和解压缩 [英] encrption and decrption in asp.net c#
本文介绍了asp.net c#中的加密和解压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以像任何字符一样加密像657这样的整数字符串,但这不包括'+'和'/'和'\'
解决方案
由于您使用的是ASP.NET,MachineKey
类 [ ^ ]可能是最简单的选项。
私人 静态 readonly string [] QueryStringPurposes = { ProtectQueryString};
public static string EncryptQueryString( string plainText)
{
byte [] userData = Encoding.UTF8.GetBytes(明文);
byte [] encryptedData = MachineKey.Protect(userData,QueryStringPurposes);
string encodedData = Convert.ToBase64String(encryptedData);
return HttpUtility.UrlEncode(encodedData);
}
public static string DecryptQueryString( string cipherText)
{
string decodingData = HttpUtility.UrlDecode(cipherText);
byte [] userData = Convert.FromBase64String(decodingData);
byte [] decryptedData = MachineKey.Unprotect(userData,QueryStringPurposes);
return Encoding.UTF8.GetString(decryptedData);
}
使用MachineKey API保护ASP.NET中的值brockallen [ ^ ]
您可以使用标准加密例程(例如,参见 System.Security.Cryptography命名空间 [ ^ ])然后使用获得的字节的十六进制表示
Hi ,
is there any way i can encrypt the integer string like "657" to any character but this will not include '+' and '/' and '\'
解决方案
Since you're using ASP.NET, theMachineKey
Class[^] is probably the simplest option.
private static readonly string[] QueryStringPurposes = { "ProtectQueryString" }; public static string EncryptQueryString(string plainText) { byte[] userData = Encoding.UTF8.GetBytes(plainText); byte[] encryptedData = MachineKey.Protect(userData, QueryStringPurposes); string encodedData = Convert.ToBase64String(encryptedData); return HttpUtility.UrlEncode(encodedData); } public static string DecryptQueryString(string cipherText) { string decodedData = HttpUtility.UrlDecode(cipherText); byte[] userData = Convert.FromBase64String(decodedData); byte[] decryptedData = MachineKey.Unprotect(userData, QueryStringPurposes); return Encoding.UTF8.GetString(decryptedData); }
Use the MachineKey API to protect values in ASP.NET | brockallen[^]
You could use a standard encryption routine (see, for instance System.Security.Cryptography Namespace[^]) and then use the hexadecimal representation of the obtained bytes.
这篇关于asp.net c#中的加密和解压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文