数字与字符串相互转换
有这样一个需求,一张表有一个自增的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);
}
ret.ToString();
return ret.ToString();
}
catch
{
return "";
}
}
/// <summary>
/// DES对称解密方法
/// </summary>
/// <param name="EncryptedData">待解密数据</param>
/// <param name="SecretKey">解密密钥,必须是加密时的密钥,密钥长度必须为八位有效英文字符,例如"12secret"</param>
public string DecryptData(object EncryptedData, 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);
string pToDecrypt = EncryptedData.ToString();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(newSecretKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(newSecretKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch
{
return "";
}
}
public class Scale36
{
public static readonly String MapString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String To36String(Int32 number)
{
if (number <= 0)
{
return String.Empty;
}
Int32 remainder = number % 36;
Int32 result = number / 36;
String result36 = To36Char(remainder).ToString();
while (result >= 36)
{
remainder = result % 36;
result = result / 36;
result36 = To36Char(remainder).ToString() + result36;
}
if (result != 0)
{
result36 = To36Char(result).ToString() + result36;
}
return result36;
}
public static Char To36Char(Int32 number)
{
if (number < 0
[解决办法]
number >= MapString.Length)
{
throw new IndexOutOfRangeException("[0-36]");
}
return MapString[number];
}
public static Int32 ToInt32(String value)
{
if (String.IsNullOrEmpty(value)
[解决办法]
String.IsNullOrWhiteSpace(value))
{
throw new ArgumentNullException("value");
}
Int32 result = 0;
for (int l = value.Length - 1, p = 0; l >= 0; l--, p++)
{
Int32 c = ToInt32(value[l]);
result += c * GetSquared(p);
}
return result;
}
public static Int32 ToInt32(Char value)
{
Int32 number = MapString.IndexOf(value);
if (number == -1)
{
throw new ArgumentOutOfRangeException("[0-9A-Z]");
}
return number;
}
private static Int32 GetSquared(Int32 squared)
{
Int32 result = 1;
while (squared >= 1)
{
result = result * 36;
squared--;
}
return result;
}
}