数目字与字符串相互转换
数字与字符串相互转换
有这样一个需求,一张表有一个自增的Int类型的主键列,在页面显示的时候想转成4位或5位,由数字,字母,或数字加字母组成的字符串,并且要求可以逆向转换,各位高手有什么好的方案,可以自由定义规则,谢谢!
------解决方案--------------------
有这样一个需求,一张表有一个自增的Int类型的主键列,在页面显示的时候想转成4位或5位,由数字,字母,或数字加字母组成的字符串,并且要求可以逆向转换,各位高手有什么好的方案,可以自由定义规则,谢谢!
------解决方案--------------------
#region 加密解密
/// <summary>
/// DES对称加密方法
/// </summary>
/// <param name="InitData">原始待加密数据</param>
/// <param name="SecretKey">加密密钥,密钥长度必须为八位有效英文字符</param>
public string EncryptData(object InitData, string SecretKey)
{
try
{
string _newsecretkey = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(SecretKey + "123456(your key)", "MD5").ToLower();
string newSecretKey = _newsecretkey.Substring(12, 4) + _newsecretkey.Substring(25, 4);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
Byte[] inputByteArray = Encoding.Default.GetBytes(InitData.ToString());
//建立加密对象的密钥和偏移量
des.Key = ASCIIEncoding.ASCII.GetBytes(newSecretKey);
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
des.IV = ASCIIEncoding.ASCII.GetBytes(newSecretKey);
//使得输入密码必须输入英文文本
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (Byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);