Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 286
realUser404 Avatar asked Dec 29 '14 10:12

realUser404


1 Answers

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.

like image 117
Omikron Avatar answered Oct 21 '22 17:10

Omikron