背景: Mike Shaffer の VB RC4 暗号化を C# に変換しようとしています ( http://www.4guysfromrolla.com/articles/091802-1.3.aspx )。Converting Mike Shaffer's RC4Encryption to C#で私の以前の質問を参照してください。
暗号化が機能していないようです。
次のデモ ページを使用: http://www.4guysfromrolla.com/demos/rc4test.asp、パスワードは「abc」:
平文: og;|Q{Feは
A2 FA E2 55 09 A4 AB 16
ただし、私のコードは 5 番目の文字を 09 ではなく 9 として生成しています。
A2 FA E2 55 9 A4 AB 16
別の例 - プレーン テキスト: cl**z!Ssは次のようになります。
AE F1 F3 03 22 FE BE 00
ただし、私のコードは生成しています:
AE F1 F3 3 22 FE BE 0
英数字以外の特定の文字にのみ問題があるようです。
これが私のコードです:
private static string EnDeCrypt(string text)
{
int i = 0;
int j = 0;
string cipher = "";
// Call our method to initialize the arrays used here.
RC4Initialize(password);
// Set up a for loop. Again, we use the Length property
// of our String instead of the Len() function
for (int a = 1; a <= text.Length; a++)
{
// Initialize an integer variable we will use in this loop
int itmp = 0;
// Like the RC4Initialize method, we need to use the %
// in place of Mod
i = (i + 1) % 256;
j = (j + sbox[i]) % 256;
itmp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = itmp;
int k = sbox[(sbox[i] + sbox[j]) % 256];
// Again, since the return type of String.Substring is a
// string, we need to convert it to a char using
// String.ToCharArray() and specifying that we want the
// first value, [0].
char ctmp = text.Substring(a - 1, 1).ToCharArray()
[0];
itmp = ctmp; //there's an implicit conversion for char to int
int cipherby = itmp ^ k;
cipher += (char)cipherby; //just cast cipherby to a char
}
// Return the value of cipher as the return value of our
// method
return cipher;
}
public static string ConvertAsciiToHex(string input)
{
return string.Join(string.Empty, input.Select(c => Convert.ToInt32(c).ToString("X")).ToArray());
}
public static string Encrypt(string text)
{
return ConvertAsciiToHex(EnDeCrypt(text));
}
暗号化された結果を取得する方法は次のとおりです。
var encryptedResult = RC4Encrypt.Encrypt(valuetoencrypt);