Using Japanese password with KeyStore.load() causes java.io.IOException: Keystore was tampered with, or password was incorrect

你。 提交于 2019-12-23 16:22:41

问题


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.


回答1:


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).




回答2:


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 すてきなパスワード



来源:https://stackoverflow.com/questions/12811950/using-japanese-password-with-keystore-load-causes-java-io-ioexception-keystor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!