c#中,如何将含有字母和数字的字符串加密成一个全是字母的字符串,并解密
2个回答
2014-04-01
展开全部
public class StringEncrypt
{
/// <summary>
/// 使用缺省密钥字符串加密
/// </summary>
/// <param name="original">明文</param>
/// <returns>密文</returns>
public static string Encrypt(string original)
{
return Encrypt(original, "xyzABc*$!");
}
/// <summary>
/// 使用缺省密钥解密
/// </summary>
/// <param name="original">密文</param>
/// <returns>明文</returns>
public static string Decrypt(string original)
{
return Decrypt(original, "xyzABc*$!", System.Text.Encoding.Default);
}
/// <summary>
/// 使用给定密钥解密
/// </summary>
/// <param name="original">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static string Decrypt(string original, string key)
{
return Decrypt(original, key, System.Text.Encoding.Default);
}
/// <summary>
/// 使用缺省密钥解密,返回指定编码方式明文
/// </summary>
/// <param name="original">密文</param>
/// <param name="encoding">编码方式</param>
/// <returns>明文</returns>
public static string Decrypt(string original, Encoding encoding)
{
return Decrypt(original, "xyzABc*$!", encoding);
}
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="original">原始文字</param>
/// <param name="key">密钥</param>
/// <param name="encoding">字符编码方案</param>
/// <returns>密文</returns>
public static string Encrypt(string original, string key)
{
byte[] buff = System.Text.Encoding.Default.GetBytes(original);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return Convert.ToBase64String(Encrypt(buff, kb));
}
/// <summary>
/// 使用给定密钥解密
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <param name="encoding">字符编码方案</param>
/// <returns>明文</returns>
public static string Decrypt(string encrypted, string key, Encoding encoding)
{
byte[] buff = Convert.FromBase64String(encrypted);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return encoding.GetString(Decrypt(buff, kb));
}
/// <summary>
/// 生成MD5摘要
/// </summary>
/// <param name="original">数据源</param>
/// <returns>摘要</returns>
public static byte[] MakeMD5(byte[] original)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] keyhash = hashmd5.ComputeHash(original);
hashmd5 = null;
return keyhash;
}
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="original">明文</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
public static byte[] Encrypt(byte[] original, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
}
/// <summary>
/// 使用给定密钥解密数据
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static byte[] Decrypt(byte[] encrypted, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
}
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="original">原始数据</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
public static byte[] Encrypt(byte[] original)
{
byte[] key = System.Text.Encoding.Default.GetBytes("xyzABc*$!");
return Encrypt(original, key);
}
/// <summary>
/// 使用缺省密钥解密数据
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static byte[] Decrypt(byte[] encrypted)
{
byte[] key = System.Text.Encoding.Default.GetBytes("xyzABc*$!");
return Decrypt(encrypted, key);
}
}
展开全部
一个思路 先将原字符串用ascii编码成byte[] 进行基于字节的加密 (下面的例子我用了0xa0++异或)
然后用base16编码 映射到一个字符串序列上 保证生成的结果全是字母(下面例子我把0-9映射到a-j,a-f映射到u-z)
解密时反过来执行一遍就OK
void Main()
{
CreateMapping();
string enc = Encrypt("abcxyz1234567");
Console.WriteLine(enc);
string dec = Decrypt(enc)
Console.WriteLine(dec);
}
// Define other methods and classes here
static List<KeyValuePair<char,char>> MappingList;
static void CreateMapping(){
MappingList = new List<KeyValuePair<char,char>>();
for(char c='0';c<='9';c++)
MappingList.Add(new KeyValuePair<char,char>(c,(char)(c-'0'+'a')));
for(char c='a';c<='f';c++)
MappingList.Add(new KeyValuePair<char,char>(c,(char)(c-'a'+'u')));
}
static string Encrypt(string str){
return Encoding.ASCII.GetBytes(str)
.Select((b,i)=>(b^((byte)(0xa0+i))).ToString("x2"))
.Aggregate("",(s,c)=>s+c)
.ToCharArray().Select(c=>MappingList.First(kv=>kv.Key==c).Value)
.Aggregate("",(s,c)=>s+c);
}
static string Decrypt(string str){
string base16 = str.ToCharArray()
.Select(c=>MappingList.First(kv=>kv.Value==c).Key)
.Aggregate("",(s,c)=>s+c);
return Encoding.ASCII.GetString((new byte[base16.Length/2])
.Select((b,i)=>(byte)(Convert.ToByte(base16.Substring(i*2,2),16)^((byte)(0xa0+i)))).ToArray());
}
追问
那个生成子字符长度可以和原来的字符长度一样长吗
追答
逻辑上行不通 你需要建立一个1对1的不重复映射 而字母和数字的域显然比字母域要大 映射是无法成立的
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询