====求一段加密的代码。谢谢!=====
如:我取出来机器硬盘的序列号后,通过加密,生成注册码。请一段加密的代码。不要用md5这种的。 --------------------编程问答-------------------- System.Security.Cryptography 命名空间 --------------------编程问答-------------------- ding --------------------编程问答-------------------- using System;using System.IO;
using System.Security.Cryptography;
namespace Ki1381Test
{
/// <summary>
/// DES加密/解密
/// </summary>
public class KiSecurity
{
const string KEY_64 = "xxxxxxxx";
const string IV_64 = "xxxxxxxx"; //注意了,是8个字符,64位
public KiSecurity()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string Encode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
if (data == "")
{
return "";
}
else
{
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
}
public static string Decode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
if (data == "")
{
return "";
}
else
{
return sr.ReadToEnd();
}
}
}
}
--------------------编程问答-------------------- using System;
/* The reason that i am using inte易做图ce is that, in several
* weeks i will use a bitwise operator for have encryption and decryption
* */
public inte易做图ce IBindesh
{
string encode(string str);
string decode(string str);
}
namespace EncryptionDecryption
{
/// <summary>
/// Summary description for EncryptionDecryption.
/// </summary>
public class EncryptionDecryption : IBindesh
{
public string encode(string str)
{
string htext = ""; // blank text
for ( int i = 0; i < str.Length; i++)
{
htext = htext + (char) (str[i] + 10 - 1 * 2);
}
return htext;
}
public string decode(string str)
{
string dtext = "";
for ( int i=0; i < str.Length; i++)
{
dtext = dtext + (char) (str[i] - 10 + 1*2);
}
return dtext;
}
}
}
----别人的 --------------------编程问答-------------------- 可以通过 "异或" 操作来进行加密解密
//content:要加密要解密的字符串
//key:加密或解密的密钥
private string CryptString(string content,string key)
{
int i;
byte[] str = Encoding.Default.GetBytes(content);
byte[] m_key = Encoding.Default.GetBytes(key);
for(i=0;i<str.Length;i++)
{
str[i] ^= m_key[i%m_key.Length];
}
return Encoding.Default.GetString(str);
} --------------------编程问答-------------------- msdn里面有一个rsa的例子 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- mark~~~~~~~~~~
补充:.NET技术 , ASP.NET