StringHelpers
public class StringHelpers
{
public const char QUERY_STRING_DELIMITER = '&'; private static RijndaelManaged _cryptoProvider;
//128 bit encyption: DO NOT CHANGE
private static readonly byte[] Key = { , , , , , , , , , , , , , , , };
private static readonly byte[] IV = { , , , , , , , , , , , , , , , };
static StringHelpers()
{
_cryptoProvider = new RijndaelManaged();
_cryptoProvider.Mode = CipherMode.CBC;
_cryptoProvider.Padding = PaddingMode.PKCS7;
} public static string Encrypt(string unencryptedString)
{
return Encrypt(unencryptedString, string.Empty);
} public static string Encrypt(string unencryptedString, string myKey)
{ //byte[] bytIn = ASCIIEncoding.ASCII.GetBytes(unencryptedString);
//如果內容有unicode的話,要用utf8編碼
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(unencryptedString);
byte[] bytKey;
if(string.IsNullOrEmpty(myKey)){
bytKey = Key;
}else{
bytKey = ASCIIEncoding.ASCII.GetBytes(myKey);
Array.Resize(ref bytKey, );
}
// Create a MemoryStream
MemoryStream ms = new MemoryStream();
// Create Crypto Stream that encrypts a stream
CryptoStream cs = new CryptoStream(ms, _cryptoProvider.CreateEncryptor(bytKey, IV), CryptoStreamMode.Write);
// Write content into MemoryStream
cs.Write(bytIn, , bytIn.Length);
cs.FlushFinalBlock();
byte[] bytOut = ms.ToArray();
//因為url不能吃+所以要轉成@@@
return Convert.ToBase64String(bytOut).Replace("+", "@@@");
} public static string Decrypt(string encryptedString)
{
return Decrypt(encryptedString, string.Empty);
} public static string Decrypt(string encryptedString, string myKey)
{
if (encryptedString.Trim().Length != )
{
//如果有人改加密字串的話,解就會發生錯誤,所以錯誤就回傳空字串
try
{
// Convert from Base64 to binary 在解開前要先將@@@轉成+
byte[] bytIn = Convert.FromBase64String(encryptedString.Replace("@@@", "+"));
byte[] bytKey;
if(string.IsNullOrEmpty(myKey)){
bytKey = Key;
}else{
bytKey = ASCIIEncoding.ASCII.GetBytes(myKey);
Array.Resize(ref bytKey, );
}
// Create a MemoryStream
MemoryStream ms = new MemoryStream(bytIn, , bytIn.Length);
// Create a CryptoStream that decrypts the data
CryptoStream cs = new CryptoStream(ms, _cryptoProvider.CreateDecryptor(bytKey, IV), CryptoStreamMode.Read); // Read the Crypto Stream
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
catch
{
return "";
}
}
else
{
return "";
}
} public static NameValueCollection DecryptQueryString(string queryString)
{
return DecryptQueryString(queryString, string.Empty);
} public static NameValueCollection DecryptQueryString(string queryString, string myKey)
{
if (queryString.Length != )
{
//Decode the string
string decodedQueryString = HttpUtility.UrlDecode(queryString);
//Decrypt the string
string decryptedQueryString = StringHelpers.Decrypt(decodedQueryString, myKey );
//Now split the string based on each parameter
string[] actionQueryString = decryptedQueryString.Split(new char[] { QUERY_STRING_DELIMITER });
NameValueCollection newQueryString = new NameValueCollection();
//loop around for each name value pair.
for (int index = ; index < actionQueryString.Length; index++)
{
string[] queryStringItem = actionQueryString[index].Split(new char[] { '=' });
if(queryStringItem.Length > )
newQueryString.Add(queryStringItem[], queryStringItem[]);
}
return newQueryString;
}
else
{
//No query string was passed in.
return null;
}
} public static string EncryptQueryString(NameValueCollection queryString)
{
return EncryptQueryString(queryString, string.Empty);
} public static string EncryptQueryString(NameValueCollection queryString, string myKey)
{
//create a string for each value in the query string passed in.
string tempQueryString = "";
for (int index = ; index < queryString.Count; index++)
{
tempQueryString += queryString.GetKey(index) + "=" + queryString[index];
if (index != queryString.Count - )
{
tempQueryString += QUERY_STRING_DELIMITER;
}
}
return EncryptQueryString(tempQueryString, myKey);
} public static string EncryptQueryString(string queryString)
{
return EncryptQueryString(queryString, string.Empty);
} public static string EncryptQueryString(string queryString, string myKey)
{
return "?" + HttpUtility.UrlEncode(StringHelpers.Encrypt(queryString, myKey));
}
}
StringHelpers的更多相关文章
随机推荐
- 【转帖】客户端通过 HTTP 请求和响应 的 Header 信息总结
请求Header原帖地址:http://technique-digest.iteye.com/blog/1174581 响应Header原帖地址:http://blog.pfan.cn/hurongl ...
- javascript 内置对象 第17节
<html> <head> <title>内置对象</title> </head> <body> <div>内置对象 ...
- java新手笔记9 类的封装示例
1.bank类 package com.yfs.javase; //类封装 public class BankCard { //属性 int balance;//默认0 实例变量 分配给每个对象一个 ...
- java新手笔记8 包
1.main函数 public class MainParam { //考察main 方法的参数 args //运行时可以传入参数 参数类型 String public static void mai ...
- 时间处理得到UTC时间
在工作过程遇到了时间处理的问题,因为需要统一将时间处理按照utc时间进行处理,因此,不能简单的通过系统运行直接得到时间的毫秒数,这样会在不同时区得到的值是不同的. import java.text.P ...
- 利用php获取图片完整Exif信息类 获取图片详细完整信息类
<?php /** * @Author: TonyLevid * @Copyright: TonyLevid.com * @Name: Image Exif Class * @Version: ...
- 【PHP】解决html网页乱码问题
在自己制作一个网页时,时常会遇到网页乱码的问题. 其实导致网页乱码主要有几个原因,以下给出解决方法. 1.HTML的字符编码问题 该问题较常见,也是最明显和最容易解决的. 在网页<head> ...
- localstorage 使用
localstorage作为HTML5的一个特殊属性,在发布时就备受关注:最近总结了其一些小的用法,希望可以抛砖引玉. 因HTML5本地存储只能存字符串,所以所有数据存储的话,都要转化成字符串:而js ...
- GitHub命令精简教程
Github其实也可以作为文件分享的地方,但是免费空间只有300M,所以不能存放大文件,否则可以成为一个分享资源的下载站,而且非常方便. 常用命令: git add . //添加所有的文件到索引 ...
- VMware Workstation 10.0 下载 – 正版序列号+简体中文官方原版
1.https://download3.vmware.com/software/wkst/file/VMware-workstation-full-10.0.0-1295980.exe 2.https ...