Vscode-java: Code result different between "vscode" and "intellij idea"

Created on 12 Nov 2019  路  5Comments  路  Source: redhat-developer/vscode-java

here is the code, about 3DES encryption

import java.security.Key;
import java.util.Base64;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;

public class Main {

    static String key = "693207DB8148FBC9D8410179";
    static String iv = "20190225";
    static String encoding = "gb2312";
    static String data = "{\"parkCode\":\"1\",\"plateNo\":\"浜珹11113\"}";


    public static String encrypt(String data) throws Exception {
        DESedeKeySpec keySpec = new DESedeKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede");
        Key deskey = keyFactory.generateSecret(keySpec);

        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] encryptData = cipher.doFinal(data.getBytes(encoding));
        return Base64.getEncoder().encodeToString(encryptData);
    }

    public static void main(String[] args) throws Exception {
        System.out.println(encrypt(data));
    }

}

in vscode, the result is

image

0edFaLw+X+9J2LHbzKQa0zo3RGmHII0JhagfRQLJhEVodBeA8XO6rw==

in intellij idea, the result is :
image

0edFaLw+X+9J2LHbzKQa0zo3RGmHII0JXoSLu19d+/pvYydg1qZV5A==

I checked the class file, the Main.class in vscode size is 2KB.

The Main.class size in 'intellij idea` is 3KB.

Here is the file:
two classes.zip

Please check the problem, thank you for your time.

not-a-bug

Most helpful comment

@15050050972
From the two compiled classes you attached you can see this;
Intellij compiled class:

{\"parkCode\":\"1\",\"plateNo\":\"?A11113\"}

Vscode compiled class:

{\"parkCode\":\"1\",\"plateNo\":\"??11113\"}

Note the "A" seems to be read incorrectly so I assume some kind of character encoding issue.

So I opened the files with a hex editor and converted them to UTF-8, the issue becomes clear. Using UTF-8, the vscode compiled file the plate number is 娴滅徆11113 whereas in intellij it is 浜珹11113.
I do not believe this is an issue with vscode, on my end it has no trouble reading those characters.

All 5 comments

I can't reproduce the issue. I have tested VS Code, Eclipse, javac/java cmd line.
The result is the same:

0edFaLw+X+9J2LHbzKQa0zo3RGmHII0JXoSLu19d+/pvYydg1qZV5A==

You could check your JDK.

@15050050972
From the two compiled classes you attached you can see this;
Intellij compiled class:

{\"parkCode\":\"1\",\"plateNo\":\"?A11113\"}

Vscode compiled class:

{\"parkCode\":\"1\",\"plateNo\":\"??11113\"}

Note the "A" seems to be read incorrectly so I assume some kind of character encoding issue.

So I opened the files with a hex editor and converted them to UTF-8, the issue becomes clear. Using UTF-8, the vscode compiled file the plate number is 娴滅徆11113 whereas in intellij it is 浜珹11113.
I do not believe this is an issue with vscode, on my end it has no trouble reading those characters.

What JDK do you use?

Sorry for your long waiting

C:\Users\tianyu>java -version
java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)

I execute same code in vscode of linux, the result is correctly. But in windows, it's incorrectly.
I write the code in other editor, and run the code by windows terminal cmd, it's still incorrectly.
So it's clearly it is not a issue in your project, it's some problem like file character encoding( even I already set it to UTF-8).

You can close the issue, thanks for your help.

I found the reason

PS E:\workspace\kotlin\leetcode> cd "e:\workspace\kotlin\leetcode\" ; if ($?) { javac Main.java } ; if ($?) { java Main }
0edFaLw+X+9J2LHbzKQa0zo3RGmHII0JhagfRQLJhEVodBeA8XO6rw==

PS E:\workspace\kotlin\leetcode> cd "e:\workspace\kotlin\leetcode\" ; if ($?) { javac Main.java -encoding utf-8 } ; if ($?) { java Main }
0edFaLw+X+9J2LHbzKQa0zo3RGmHII0JXoSLu19d+/pvYydg1qZV5A==

PS E:\workspace\kotlin\leetcode> cd "e:\workspace\kotlin\leetcode\" ; if ($?) { javac Main.java -encoding gbk } ; if ($?) { java Main }  
0edFaLw+X+9J2LHbzKQa0zo3RGmHII0JhagfRQLJhEVodBeA8XO6rw==

I need use -encoding option to specify the encoding of java file, otherwise javac will get default encoding from OS, my OS is windows, and I'm in China. So my default encoding is GBK

Was this page helpful?
0 / 5 - 0 ratings