Nodejs and java get different results from rc4 encryption.

at the same time to a

buffer < 03 01 00 00 02 00 00 >

encrypt. The result of
java and c is

CA 66 C4 5D 90 E7 3001
The result of

nodejs is

6f 61 f8 95 ab ba aa 90

what is the reason why it is different?

java code address ideone.com/V5Nzos" rel=" nofollow noreferrer "> https://ideone.com/V5Nzos

import java.nio.ByteBuffer;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

class Test {  
    public static void main(String[] args) {  

        try {

            byte[] bs = {
            (byte)0x03, (byte)0x01, (byte)0x00, (byte)0x00, 
            (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00
            };
            ByteBuffer buff = ByteBuffer.wrap(bs);
            Cipher instance = Cipher.getInstance("RC4");
            instance.init(Cipher.DECRYPT_MODE, new SecretKeySpec(("c2eWxyNe5c4G9GUHMQECzcUEWUK8MWkk" + "48LoIDEv8EQOrmwPirikDa3iUaickMSq").getBytes(), "RC4"));
            instance.update(buff.array(), 0, 8, buff.array(), 0);
            byte[] array = buff.array();
            for (int j=0; j<array.length; jPP) {
                System.out.format("%02X ", array[j]);
            }
            System.out.println();
            // :CA 66 C4 5D 90 E7 30 01
        
            
        }catch (Throwable th) {
            System.out.println(th);
            System.exit(0);
        }
    }


}

nodejs code address
ideone.com/XQVhRM" rel=" nofollow noreferrer "> https://ideone.com/XQVhRM

var crypto = require("crypto");
//en
function cipher (buf) {
    var rc4Key = "c2eWxyNe5c4G9GUHMQECzcUEWUK8MWkk"
    var cipher = crypto.createCipher("rc4", (rc4Key+"48LoIDEv8EQOrmwPirikDa3iUaickMSq"),1);
    var cryptedBuffers = [cipher.update(buf)];
    cryptedBuffers.push(cipher.final());
    var crypted = Buffer.concat(cryptedBuffers);
    return crypted;
};
var buf1 = Buffer.from([
    0x03, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00
]);
 
console.log(cipher((buf1)));
//<Buffer 6f 61 f8 95 ab ba aa 90>
The byte type of

java is signed.
this is basically the reason for the problem with general encryption algorithms. You can find it and try it.


how to solve this problem?

Menu