本人写RC4加密算法的时候
具体代码如下:
public string EncryptEx(string Data, string Key)
{
if (Data == null || Key == null) return null; byte[] Data_byte = System.Text.Encoding.Default.GetBytes(Data);
byte[] Output = new byte[Data_byte.Length];
Int64 i = 0;
Int64 j = 0;
Int16 mBoxL = 256; Byte[] mBox = GetKey(System.Text.Encoding.Default.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 M=mBox[(byte)(mBox[i] + mBox[j]) % mBoxL]; Output[L] =(byte)(M ^ Data_byte[L]);
} return System.Text.Encoding.Default.GetString(Output);
} public string 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;
}
编译能通过,没问题
但是用 EncryptEx加密后的密文和相同的key,
进入 DecryptEx解密后得到的明文
出现的问题是为什么有些明文和加密前相同,但是有些就不同了
请问是哪里出问题了?
具体代码如下:
public string EncryptEx(string Data, string Key)
{
if (Data == null || Key == null) return null; byte[] Data_byte = System.Text.Encoding.Default.GetBytes(Data);
byte[] Output = new byte[Data_byte.Length];
Int64 i = 0;
Int64 j = 0;
Int16 mBoxL = 256; Byte[] mBox = GetKey(System.Text.Encoding.Default.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 M=mBox[(byte)(mBox[i] + mBox[j]) % mBoxL]; Output[L] =(byte)(M ^ Data_byte[L]);
} return System.Text.Encoding.Default.GetString(Output);
} public string 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;
}
编译能通过,没问题
但是用 EncryptEx加密后的密文和相同的key,
进入 DecryptEx解密后得到的明文
出现的问题是为什么有些明文和加密前相同,但是有些就不同了
请问是哪里出问题了?
楼主可以根据你加密前后 相同的明文字符进行跟踪 分析