public static string encrypt(string source)
{
md5cryptoserviceprovider md5 = new md5cryptoserviceprovider();
byte[] bytes = encoding.utf8.getbytes(source);
byte[] output = md5.computehash(bytes);
return bitconverter.tostring(output);
}
最常见的md5加密,但不带解密。
des加解密。
public class des
{
private const string key = av&6^3*e;
public static string encrypt(string source)
{
descryptoserviceprovider des = new descryptoserviceprovider();
byte[] bytes = encoding.utf8.getbytes(source);
des.key = asciiencoding.ascii.getbytes(key);
des.iv = asciiencoding.ascii.getbytes(key);
memorystream ms = new memorystream();
cryptostream cs = new cryptostream(ms, des.createencryptor(), cryptostreammode.write);
cs.write(bytes, 0, bytes.length);
cs.flushfinalblock();
stringbuilder sb = new stringbuilder();
foreach (byte b in ms.toarray())
{
sb.appendformat({0:x2}, b);
}
return sb.tostring();
}
public static string decrypt(string source)
{
if (source == null || source.length == 0)
{
return source;
}
descryptoserviceprovider des = new descryptoserviceprovider();
byte[] bytes = new byte[source.length / 2];
for (int x = 0; x < source.length / 2; x++)
{
int i = (convert.toint32(source.substring(x * 2, 2), 16));
bytes[x] = (byte)i;
}
des.key = asciiencoding.ascii.getbytes(key);
des.iv = asciiencoding.ascii.getbytes(key);
memorystream ms = new memorystream();
cryptostream cs = new cryptostream(ms, des.createdecryptor(), cryptostreammode.write);
cs.write(bytes, 0, bytes.length);
cs.flushfinalblock();
return encoding.utf8.getstring(ms.toarray());
}
}