简体   繁体   中英

Keytool error : Keystore was tampered with… Special characters

I know there are already a few posts about this error, but I couldn't find an answer fitting my problem :

I created an AES key with the following command :

keytool -genseckey -alias TEST -keyalg AES -keysize 128 -storepass "a#b$c<d>" 
-storetype JCEKS -keystore /usr/my/path/test.jck

I then try to access the keystore from java code :

String password = "a#b$c<d>";

char[] passwordChars= password.toCharArray(); 

// loading the file containing the key
InputStream inputStreamFichierCle;
try {
    inputStreamFichierCle = new FileInputStream(filePath);
    keyStore.load(inputStreamFichierCle, passwordChars);
}

And there I get an IOException : keystore was tampered with or password was incorrect .

Note that I tried with normal password (ex : pass) and this works perfectly, so I guess the problem here has to do with the special characters I use in my password.

What is happening, and how can I fix this?

The cause of this problem is the dollar sign in combination with bash command line.

Basically "$c" is substituted with the content of a variable with the name "c". Unfortunately there is no variable with this name, so it is replaced with an empty string.

You can avoid the variable substitution by using single quotes . See the difference:

$ echo "a#b$c<d>"
a#b<d>
$ echo 'a#b$c<d>'
a#b$c<d>

If you use the password " a#b<d> " in your java code, it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM