关于加密方法

Magichi 2009-02-16 03:23:09
现在有2个string,一个长度为5,保存着学校代码信息;另一个长度为3,保存着学生代码。
现在想将他们加密为一个长度为8的代码,并且可以解密。 最简单的方法是:分别加密,然后连接在一起。解密的时候取也很简单,密码的前5位解密成学校,后3位解密成学生即可。
但是这样有很明显的缺陷,比如一个学校的很多学生拿到代码后互相比较,就可以发现他们的密码前5位都一样,只有后3位不一样,那么很容易就可以猜测到这个密码的构成方法。
请问各位提供一种更为隐蔽安全的加密/解密方法,有具体代码更佳。非常感谢。
...全文
83 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Magichi 2009-02-16
  • 打赏
  • 举报
回复
多谢楼上诸位弟兄的答复。不过上面的几个加密方法和我想要的还不太一样。
首先,我想要的是5位和3位组合成一个8位的,而md5或者des在长度上不符合规则;其次我希望的是5位和3位这两个码的组合,而不是每个单独加密。
期待其他解决方法。谢谢
chenwei175528 2009-02-16
  • 打赏
  • 举报
回复
mark and up
If DES arithmetic is not enough,you can use AES arithmetic to encrypt them!
  • 打赏
  • 举报
回复
mark
饺子87 2009-02-16
  • 打赏
  • 举报
回复
MARK
-无-为- 2009-02-16
  • 打赏
  • 举报
回复
学习了 标记一下..
宝_爸 2009-02-16
  • 打赏
  • 举报
回复
加密解密可以使用.net framework提供的DES算法:

public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);

//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}

//解密方法
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();

//Put the input string into the byte array
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(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();

//Get the decrypted data back from the memory stream
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();

return System.Text.Encoding.Default.GetString(ms.ToArray());
}

string encripted = "";
private void button6_Click(object sender, EventArgs e)
{
encripted = Encrypt("1234567890", "hello123");
}

private void button7_Click(object sender, EventArgs e)
{
MessageBox.Show(Decrypt(encripted, "hello123"));
}
宝_爸 2009-02-16
  • 打赏
  • 举报
回复
加密解密可以使用.net framework提供的DES算法:

public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);

//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}

//解密方法
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();

//Put the input string into the byte array
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(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();

//Get the decrypted data back from the memory stream
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();

return System.Text.Encoding.Default.GetString(ms.ToArray());
}

string encripted = "";
private void button6_Click(object sender, EventArgs e)
{
encripted = Encrypt("1234567890", "hello123");
}

private void button7_Click(object sender, EventArgs e)
{
MessageBox.Show(Decrypt(encripted, "hello123"));
}
宝_爸 2009-02-16
  • 打赏
  • 举报
回复
先连接后解密呢?
读取的时候,先解密,然后前5为学校,后3为学生。
加密后的长度就不一定要求是8了。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧