底层设备C语言RC4 加密后,为什么java使用rc4解密不了,但是别人php程序可以解密成功
底层设备是一个wifi探针,定时推送数据给某个服务,此服务地址 接收数据,使用rc4解密。
底层设备rc4加密是用c语言写的。
java接收程序
@RequestMapping("/acc")public void acc1(HttpServletRequest request,HttpServletResponse response) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
System.out.println(sdf.format(new Date())+"-->post begin.......");
BufferedReader reader = new BufferedReader(
new InputStreamReader(request.getInputStream(),"UTF-8"));
String line=null;
StringBuilder buffer = new StringBuilder();
while((line = reader.readLine())!=null){
buffer.append(line);
}
System.out.println("recive: "+buffer.toString());
String key = buffer.toString().substring(0, 16);
String content = buffer.toString().substring(16);
System.out.println("before 16 string: "+key);
System.out.println("content : "+content);
System.out.println("decrypt : "+RC4.HloveyRC4(content, key));
System.out.println(sdf.format(new Date())+"-->post end.......");
}
java rc4 解密程序:
public class RC4 {
public static String HloveyRC4(String aInput,String aKey) { int[] iS = new int[256];
byte[] iK = new byte[256];
for (int i=0;i<256;i++)
iS[i]=i;
int j = 1;
for (short i= 0;i<256;i++) {
iK[i]=(byte)aKey.charAt((i % aKey.length()));
}
j=0;
for (int i=0;i<255;i++){
j=(j+iS[i]+iK[i]) % 256;
int temp = iS[i];
iS[i]=iS[j];
iS[j]=temp;
}
int i=0; j=0;
char[] iInputChar = aInput.toCharArray();
char[] iOutputChar = new char[iInputChar.length];
for(short x = 0;x<iInputChar.length;x++) {
i = (i+1) % 256;
j = (j+iS[i]) % 256;
int temp = iS[i];
iS[i]=iS[j];
iS[j]=temp;
int t = (iS[i]+(iS[j] % 256)) % 256;
int iY = iS[t];
char iCY = (char)iY;
iOutputChar[x] =(char)( iInputChar[x] ^ iCY) ;
}
return new String(iOutputChar);
} }
php解密程序 可以解密成功
$json=file_get_contents('php://input');
if ($json) {
$time_str=date('YmdHms',time());//开始RC4解密
$key = substr($json,0,16);
$json = rc4($key, substr($json,16));
$json=mb_convert_encoding($json,"UTF-8","auto");
}
function rc4($pwd, $data)//$pwd密钥 $data需加密字符串
{
$key[] =""; $box[] ="";
$cipher="";
$pwd_length = strlen($pwd);
$data_length = strlen($data);
for ($i = 0; $i < 256; $i++)
{
$key[$i] = ord($pwd[$i % $pwd_length]);
$box[$i] = $i;
}
for ($j = $i = 0; $i < 256; $i++)
{
$j = ($j + $box[$i] + $key[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for ($a = $j = $i = 0; $i < $data_length; $i++)
{
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$k = $box[(($box[$a] + $box[$j]) % 256)];
$cipher .= chr(ord($data[$i]) ^ $k);
}
// var_dump($cipher);
//exit();
return $cipher;
}
java控制台解密后的数据乱码
为何就java解密失败呢。获取数据试过不同编码,都是解密后乱码。
回答:
主要问题是java接收数据的时候,需要获取byte数组的方式接收
Java应当先把字符串转换为byte[], 然后再去参与解密算法; PHP算法正确,是因为它使用的的strlen函数, 会把一个中文字符当成3个byte来看(utf-8),而JAVA错误是因为你使用的是 toCharArray, 一个中文等于一个char;一个是对byte数组进行解密,一个是对char数组进行解密,结果自然不同。
更改后java接收端代码:
in = request.getInputStream(); byte[] getArr = readBytes(in, request.getContentLength());
byte[] key = new byte[16];
System.arraycopy(getArr, 0, key, 0, key.length);
System.out.println("key:"+new String(key));
int contentLength = request.getContentLength()-16;
byte[] content = new byte[contentLength];
System.arraycopy(getArr, 16, content, 0, content.length);
System.out.println("content"+new String(content));
String result = RC4T2.decry_RC4(content, new String(key));
System.out.println("RC4 decrypt:"+result);
更改后解密算法代码:
public class RC4T2 {
public static String decry_RC4(byte[] data, String key) {
if (data == null || key == null) {
return null;
}
return asString(RC4Base(data, key));
}
private static String asString(byte[] buf) {
StringBuffer strbuf = new StringBuffer(buf.length);
for (int i = 0; i < buf.length; i++) {
strbuf.append((char) buf[i]);
}
return strbuf.toString();
}
private static byte[] RC4Base (byte [] input, String mKkey) {
int x = 0;
int y = 0;
byte key[] = initKey(mKkey);
int xorIndex;
byte[] result = new byte[input.length];
for (int i = 0; i < input.length; i++) {
x = (x + 1) & 0xff;
y = ((key[x] & 0xff) + y) & 0xff;
byte tmp = key[x];
key[x] = key[y];
key[y] = tmp;
xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;
result[i] = (byte) (input[i] ^ key[xorIndex]);
}
return result;
}
}
解密结果:
RC4decrypt:13 MSFCS11612000203 B4:8B:19:E3:16:C4 -63 1.00,22.00 1500430087 12 MSFCS11612000203 A6:34:D9:B0:7B:FB DIRECT-CCZHTANGHAORAN2msQU 11 WPA2 -74 1.00,22.00 1500430087 12 MSFCS11612000203 00:11:B5:11:22:9A freewifi-yhnw 1 WPA2 -71 1.00,22.00 1500430087 12 MSFCS11612000203 80:F6:2E:99:0F:D0 SZB_1F 1 WP
20170719102921-->acc post end.......
回答:
就不能用JCE的RC4来解密吗?另外,两边密钥对不对?
回答:
RC4
加密之后不是二进制内容吗,你把他放在一个StringBuilder
里,也不知道是否会影响正常解密。另外,解密的时候,不应该是按字节 (byte) 解的吗?你不知道java
中的char
和其他大多语言不同的是 Unicode (UTF-16) 字符吗?如果你把来源内容当做字符串,一个一个char
的来解密,那结果一定是不正确的。
回答:
Java应当先把字符串转换为byte[], 然后再去参与解密算法; PHP算法正确,是因为它使用的的strlen函数, 会把一个中文字符当成3个byte来看(utf-8),而JAVA错误是因为你使用的是 toCharArray, 一个中文等于一个char;
一个是对byte数组进行解密,一个是对char数组进行解密,结果自然不同。
char[] iInputChar = aInput.toCharArray(); 应当修改为
byte[] iInputChar = aInput.getBytes("utf-8");
其他相关代码请自行修改。
以上是 底层设备C语言RC4 加密后,为什么java使用rc4解密不了,但是别人php程序可以解密成功 的全部内容, 来源链接: utcz.com/p/194227.html