这个算法是用在普通的便笺加密上的,加密强度不用很高。但是要求要支持中文字,并且保存成文本后不会变成乱码(比如变成了???结果不能还原了)。
因为还有搜索功能,所以这个算法最好是对称算法,就是每次加密的结果是一样的。总结来说就是要有三点:
1.1000字以下的文本加密速度快,至少让用户感觉不到明显的延迟。
2.加密后的文本可以被还原而且方便保存(如存入数据库或存入文本)。
3.加密后的文本能够尽量照顾到搜索。不知道大家有这方面的好算法么?谢谢
因为还有搜索功能,所以这个算法最好是对称算法,就是每次加密的结果是一样的。总结来说就是要有三点:
1.1000字以下的文本加密速度快,至少让用户感觉不到明显的延迟。
2.加密后的文本可以被还原而且方便保存(如存入数据库或存入文本)。
3.加密后的文本能够尽量照顾到搜索。不知道大家有这方面的好算法么?谢谢
其实1k字左右的文本量进行加密 各种算法差别不大的 反正不能一次加密整个文本字符串 :)
这篇blog或许也有点用
http://blog.csdn.net/veryhappy/archive/2006/01/09/574152.aspx
在看看大家有什么好算法介绍~现在有什么专门研究加密算法的网站么?
便笺要求留档的,如果是根据密码求HASH,然后异或再BASE64,那么一旦密码发生改变要做的工作就太大了...这个方法当初也想到过,但是因为后续问题还是放弃了
另外,只是简单测试了一下,不保证没有bug
static void Main(string[] args)
{
byte[] key = new byte[] { 10, 200 };// key随便设置,只要保证每次都是这一个
string src = @" 这个算法是用在普通的便笺加密上的,加密强度不用很高。但是要求要支持中文字,并且保存成文本后不会变成乱码(比如变成了???结果不能还原了)。
因为还有搜索功能,所以这个算法最好是对称算法,就是每次加密的结果是一样的。总结来说就是要有三点:
1.1000字以下的文本加密速度快,至少让用户感觉不到明显的延迟。
2.加密后的文本可以被还原而且方便保存(如存入数据库或存入文本)。
3.加密后的文本能够尽量照顾到搜索。不知道大家有这方面的好算法么?谢谢";
string encryto = EncrytoString(src, key);
Console.WriteLine(encryto);
string decryto = DecrytoString(encryto, key);
Console.WriteLine(encryto);
string search = "便笺";
string encrytoSearch = EncrytoString(search, key);
Console.WriteLine(encryto.IndexOf(encrytoSearch));
Console.ReadLine();
} private static string EncrytoString(string src, byte[] key)
{
SimpleSymmetricAlgorithm ssa = new SimpleSymmetricAlgorithm();
ssa.Key = key;
byte[] srcBytes = Encoding.Unicode.GetBytes(src);
ICryptoTransform transform = ssa.CreateEncryptor();
MemoryStream output = new MemoryStream();
using (CryptoStream cs = new CryptoStream(output, ssa.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(srcBytes, 0, srcBytes.Length);
cs.FlushFinalBlock();
output.Seek(0L, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(output, Encoding.Unicode))
return sr.ReadToEnd();
}
} private static string DecrytoString(string src, byte[] key)
{
SimpleSymmetricAlgorithm ssa = new SimpleSymmetricAlgorithm();
ssa.Key = key;
byte[] srcBytes = Encoding.Unicode.GetBytes(src);
ICryptoTransform transform = ssa.CreateEncryptor();
MemoryStream output = new MemoryStream();
using (CryptoStream cs = new CryptoStream(output, ssa.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(srcBytes, 0, srcBytes.Length);
cs.FlushFinalBlock();
output.Seek(0L, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(output, Encoding.Unicode))
return sr.ReadToEnd();
}
} public class SimpleSymmetricAlgorithm
: SymmetricAlgorithm
{
private sealed class CryptoTransform
: ICryptoTransform
{ private SimpleSymmetricAlgorithm _parent;
private bool _isEncrypto; internal CryptoTransform(SimpleSymmetricAlgorithm parent, bool isEncrypto)
{
_parent = parent;
_isEncrypto = isEncrypto;
} #region ICryptoTransform Members public bool CanReuseTransform
{
get { return true; }
} public bool CanTransformMultipleBlocks
{
get { return true; }
} public int InputBlockSize
{
get { return 2; }
} public int OutputBlockSize
{
get { return 2; }
} public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
if (inputCount % 2 == 1)
throw new InvalidOperationException();
byte[] buffer = TransformCore(inputBuffer, inputOffset, inputCount);
Buffer.BlockCopy(buffer, 0, outputBuffer, outputOffset, buffer.Length);
return buffer.Length;
} public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
if (inputCount % 2 == 1)
throw new InvalidOperationException();
return TransformCore(inputBuffer, inputOffset, inputCount);
} #endregion private byte[] TransformCore(byte[] inputBuffer, int inputOffset, int inputCount)
{
const int MinChineseUnicode = 0x4e00;
const int MaxChineseUnicode = 0x9FA5;
short key = BitConverter.ToInt16(_parent.Key, 0);
int offset = 0;
byte[] result = new byte[inputCount];
while (offset < inputCount)
{
int temp = BitConverter.ToUInt16(inputBuffer, inputOffset + offset);
if (temp > MinChineseUnicode && temp < MaxChineseUnicode)
{
// encryto or decryto chinese char only
if (_isEncrypto)
temp += key;
else
temp -= key;
while (temp < MinChineseUnicode)
temp += MaxChineseUnicode - MinChineseUnicode;
while (temp > MaxChineseUnicode)
temp -= MaxChineseUnicode - MinChineseUnicode;
}
byte[] bytes = BitConverter.GetBytes((ushort)temp);
result[offset++] = bytes[0];
result[offset++] = bytes[1];
}
return result;
} #region IDisposable Members public void Dispose()
{
GC.SuppressFinalize(this);
} #endregion } public SimpleSymmetricAlgorithm()
{
base.BlockSizeValue = 16;
base.KeySizeValue = 16;
base.FeedbackSizeValue = base.BlockSizeValue;
base.LegalBlockSizesValue = new KeySizes[] { new KeySizes(16, 16, 8) };
base.LegalKeySizesValue = new KeySizes[] { new KeySizes(16, 16, 8) };
} public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
{
return new CryptoTransform(this, false);
} public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
{
return new CryptoTransform(this, true);
} public override void GenerateIV()
{
IV = new byte[] { 0, 0 };
} public override void GenerateKey()
{
RandomNumberGenerator.Create().GetNonZeroBytes(Key);
}
}
(目前QQ就是使用这个算法加密,标准TEA算法推荐32次迭代,qq使用16次迭代)
不论什么算法,加密结果肯定都是二进制数据直接保存成文本文件肯定有问题,建议用base64将加密结果再处理一次。或者干脆将每个字节转换成十六进制的文本表示方式。
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
谢谢各位的回答,我结贴了
好用!直接拿走备用了 THX~