110,533
社区成员
发帖
与我相关
我的任务
分享
public Byte[] EncryptEx(string Data, string Key)
{
if (Data == null || Key == null) return null;
byte[] Data_byte = Encode.GetBytes(Data);
Byte[] Output = new Byte[Data_byte.Length];
Int64 i = 0;
Int64 j = 0;
byte k;
Int16 mBoxL = 256;
Byte[] mBox = GetKey(Encode.GetBytes(Key), mBoxL);
// 加密
for (Int64 L = 0; L < Data_byte.Length; L++)
{
i = (i + 1) % mBoxL;
j = (j + mBox[i]) % mBoxL;
Byte temp = mBox[i];
mBox[i] = mBox[j];
mBox[j] = temp;
//Byte a = Data[L];
//Byte b = mBox[(mBox[i] + mBox[j] % mBox.Length) % mBox.Length];
// mBox[j] 一定比 mBox.Length 小,不需要在取模
mBox[k] =mBox[(byte)(mBox[i] + mBox[j]) % mBoxL];
Output[L] = (Byte)(mBox[k] ^ Data_byte[L]);
}
return Output;
}
public Byte[] DecryptEx(string Data, String Key)
{
return EncryptEx(Data, Key);
}
/// <returns>打乱后的S盒</returns>
static private Byte[] GetKey(Byte[] Pass,Int32 KLen)
{
Byte[] mBox = new Byte[KLen];
for (Int64 i = 0; i < KLen; i++)
{
mBox[i] = (Byte)i;
}
Int64 j = 0;
for (Int64 i = 0; i < KLen; i++)
{
j = (j + mBox[i] + Pass[i % Pass.Length]) % KLen;
Byte temp = mBox[i];
mBox[i] = mBox[j];
mBox[j] = temp;
}
return mBox;