C# 代码如下:
public string EncryptRC4wq(string ckey, string str)
{
int[] s = new int[256];
for (int i = 0; i < 256; i++)
{
s[i] = i;
}
//密钥转数组
char[] keys = ckey.ToCharArray();//密钥转字符数组
int[] key = new int[keys.Length];
for (int i = 0; i < keys.Length; i++)
{
key[i] = keys[i];
}
//明文转数组
char[] datas = str.ToCharArray();
int[] mingwen = new int[datas.Length];
for (int i = 0; i < datas.Length; i++)
{
mingwen[i] = datas[i];
}
//通过循环得到 256 位的数组(密钥)
int j = 0;
int key_len = key.Length;
for (int i = 0; i < 256; i++)
{
j = (j + s[i] + key[i% key_len])%256;
//swap
int temp = s[i];
s[i] = s[j];
s[j] = temp;
}
//根据上面的 256 的密钥数组 和 明文得到密文数组
int x = 0, y = 0, c;
int data_len = mingwen.Length;
int[] miwen = new int[data_len];
string ret = "";
for (int i = 0; i < data_len; i++)
{
x = (x + 1) % 256;
y = (y + s[x]) % 256;
//swap
int temp = s[x];
s[x] = s[y];
s[y] = temp;
c = s[x]+s[y];
c = c % 256;
miwen[i] = mingwen[i] ^ s[c];
ret +=(char)miwen[i];
}
return ret;
}
PHP 代码如下
// RC4 加密方法
// $key 为 加密密匙
// $pt 为 加密字符串
// 加密内容再次加密则为解密
public function RC4($key, $pt){
$s = array();
for ($i=0; $i<256; $i++) {
$s[$i] = $i;
}
$j = 0;
$key_len = strlen($key);
for ($i=0; $i<256; $i++) {
$j = ($j + $s[$i] + ord($key[$i % $key_len])) % 256;
//swap
$x = $s[$i];
$s[$i] = $s[$j];
$s[$j] = $x;
}
$i = 0;
$j = 0;
$ct = '';
$data_len = strlen($pt);
for ($y=0; $y< $data_len; $y++) {
$i = ($i + 1) % 256;
$j = ($j + $s[$i]) % 256;
//swap
$x = $s[$i];
$s[$i] = $s[$j];
$s[$j] = $x;
$ct .= $pt[$y] ^ chr($s[($s[$i] + $s[$j]) % 256]);
}
return $ct;
}
1
zaiyuzhong OP 大佬们别忙着摸鱼也帮我看看
|
2
zaiyuzhong OP v 大佬来看看
|
3
di1012 2019-10-18 16:45:42 +08:00
不会 php 咋帮你看
|
4
loqixh 2019-10-18 16:52:20 +08:00
public static void Encrypt(byte[] key, byte[] data, int startIndex, int count)
{ int i, j, n, t; byte c; byte[] s = new byte[256]; //S-box byte[] k = new byte[256]; for (i = 0; i < 256; i++) { s[i] = (byte)i; } //把 key 循环放进 K j = 0; for (i = 0; i < 256; i++) { k[i] = key[j % key.Length]; j++; } j = 0; for (i = 0; i < 256; i++) { j = (j + s[i] + k[i]) % 256; c = s[i]; s[i] = s[j]; s[j] = c; } i = 0; j = 0; for (n = startIndex; n < startIndex + count; n++) { i = (i + 1) % 256; j = (j + s[i]) % 256; //交换 S[i]和 S[j] c = s[i]; s[i] = s[j]; s[j] = c; t = (int)((s[i] + s[j]) % 256); c = s[t]; data[n] = (byte)(data[n] ^ c); //异或加密 } |
5
hlayk 2019-10-18 19:20:50 +08:00
相同的关键点加 log 看哪里不一样
|
6
Mohanson 2019-10-18 19:27:18 +08:00 via Android
|
7
ShareDuck 2019-10-18 21:10:24 +08:00 via Android
在网上找个已知正确的结果验证一下不更好?搞不好两个都错了呢?
|
8
areless 2019-10-18 21:22:10 +08:00
装个 openssl
openssl_encrypt($param_string,'rc4',$key,OPENSSL_R AW_DATA,$key); |
9
dosmlp 2019-10-19 09:49:41 +08:00 via Android
盲猜是模式不同
|