Using Japanese password with KeyStore.load() cause

2019-07-02 02:09发布

I have created a Keystore using:

keytool -genkey -keystore myKeyStore -alias myself

and the password i have given is in Japanese i.e. "myPasswordは" I am using the following code to load the keystore file

String KEYSTORE_FILE="C:/myKeyStore";//Path to the keystore file on disk.
PASSWORD="myPasswordは";//Password

KeyStore.getInstance("JKS").load(new FileInputStream(KEYSTORE_FILE),PASSWORD.toCharArray());

The above throws the following exception: java.io.IOException: Keystore was tampered with, or password was incorrect.

If i create the keystore using an english password and use the same in code then it works fine.

Initially I created the keystore using the command: keytool -genkey -keystore myKeyStore -alias mks this will prompt me for the password which i enter as myPasswordは and then i enter the subsequent details. In this case, the code generates the above exception.

The interesting thing is that if i create the keystore using : keytool -genkey -keystore myKeyStore -alias mks -storepass myPasswordは then the code that i have given above works fine. I am using linux.

2条回答
走好不送
2楼-- · 2019-07-02 02:25

Well, from the description I guess that console doesn't process unicode characters as expected. You can try to use PowerShell or try to change code page of cmd.exe to UTF-8 by executing chcp 65001 command (note that this will distort console display behaviour, but it will accept input properly).

查看更多
一纸荒年 Trace。
3楼-- · 2019-07-02 02:30

The root cause of this problem is that the keytool utility does not decode a non-ascii password entered at prompt correctly.

This is a known issue which can be found at http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=53546691146578386c644cb554976?bug_id=6433238

So, the solution is to create a keystore with a password not entered at prompt. This can be done in the following way:

keytool -genkey -keystore myKeyStore -alias myAlias -storepass すてきなパスワード

查看更多
登录 后发表回答