Skip to content

Instantly share code, notes, and snippets.

@zach-klippenstein
Last active March 8, 2024 02:26
Star You must be signed in to star a gist
Save zach-klippenstein/4631307 to your computer and use it in GitHub Desktop.
The keystore password on Java keystore files is utterly pointless. You can reset it without knowing it, as shown by this code. Note that private keys are still secure, as far as I know. The JKS implementation is copyright Casey Marshall (rsdio@metastatic.org), and the original source is available at http://metastatic.org/source/JKS.java. I've in…
import java.util.*;
import java.io.*;
import java.security.*;
public class ChangePassword
{
private final static JKS j = new JKS();
public static void main(String[] args) throws Exception
{
if (args.length < 2)
{
System.out.println("Usage: java ChangePassword keystoreFile newKeystoreFile");
return;
}
String keystoreFilename = args[0];
String newFilename = args[1];
InputStream in = new FileInputStream(keystoreFilename);
String passwd = promptForPassword("keystore");
System.out.printf("Changing password on '%s', writing to '%s'...\n", keystoreFilename, newFilename);
j.engineLoad(in, passwd.toCharArray());
in.close();
passwd = promptForPassword("new keystore");
OutputStream out = new FileOutputStream(newFilename);
j.engineStore(out, passwd.toCharArray());
out.close();
}
private static String promptForPassword(String which)
{
System.out.printf("Enter %s password: ", which);
Scanner kbd = new Scanner(System.in);
return kbd.nextLine();
}
}
/* JKS.java -- implementation of the "JKS" key store.
Copyright (C) 2003 Casey Marshall <rsdio@metastatic.org>
Permission to use, copy, modify, distribute, and sell this software and
its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation. No representations are made about the
suitability of this software for any purpose. It is provided "as is"
without express or implied warranty.
This program was derived by reverse-engineering Sun's own
implementation, using only the public API that is available in the 1.4.1
JDK. Hence nothing in this program is, or is derived from, anything
copyrighted by Sun Microsystems. While the "Binary Evaluation License
Agreement" that the JDK is licensed under contains blanket statements
that forbid reverse-engineering (among other things), it is my position
that US copyright law does not and cannot forbid reverse-engineering of
software to produce a compatible implementation. There are, in fact,
numerous clauses in copyright law that specifically allow
reverse-engineering, and therefore I believe it is outside of Sun's
power to enforce restrictions on reverse-engineering of their software,
and it is irresponsible for them to claim they can. */
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.DigestInputStream;
import java.security.DigestOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyStoreException;
import java.security.KeyStoreSpi;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import javax.crypto.EncryptedPrivateKeyInfo;
import javax.crypto.spec.SecretKeySpec;
/**
* This is an implementation of Sun's proprietary key store
* algorithm, called "JKS" for "Java Key Store". This implementation was
* created entirely through reverse-engineering.
*
* <p>The format of JKS files is, from the start of the file:
*
* <ol>
* <li>Magic bytes. This is a four-byte integer, in big-endian byte
* order, equal to <code>0xFEEDFEED</code>.</li>
* <li>The version number (probably), as a four-byte integer (all
* multibyte integral types are in big-endian byte order). The current
* version number (in modern distributions of the JDK) is 2.</li>
* <li>The number of entrires in this keystore, as a four-byte
* integer. Call this value <i>n</i></li>
* <li>Then, <i>n</i> times:
* <ol>
* <li>The entry type, a four-byte int. The value 1 denotes a private
* key entry, and 2 denotes a trusted certificate.</li>
* <li>The entry's alias, formatted as strings such as those written
* by <a
* href="http://java.sun.com/j2se/1.4.1/docs/api/java/io/DataOutput.html#writeUTF(java.lang.String)">DataOutput.writeUTF(String)</a>.</li>
* <li>An eight-byte integer, representing the entry's creation date,
* in milliseconds since the epoch.
*
* <p>Then, if the entry is a private key entry:
* <ol>
* <li>The size of the encoded key as a four-byte int, then that
* number of bytes. The encoded key is the DER encoded bytes of the
* <a
* href="http://java.sun.com/j2se/1.4.1/docs/api/javax/crypto/EncryptedPrivateKeyInfo.html">EncryptedPrivateKeyInfo</a> structure (the
* encryption algorithm is discussed later).</li>
* <li>A four-byte integer, followed by that many encoded
* certificates, encoded as described in the trusted certificates
* section.</li>
* </ol>
*
* <p>Otherwise, the entry is a trusted certificate, which is encoded
* as the name of the encoding algorithm (e.g. X.509), encoded the same
* way as alias names. Then, a four-byte integer representing the size
* of the encoded certificate, then that many bytes representing the
* encoded certificate (e.g. the DER bytes in the case of X.509).
* </li>
* </ol>
* </li>
* <li>Then, the signature.</li>
* </ol>
* </ol>
* </li>
* </ol>
*
* <p>(See <a href="genkey.java">this file</a> for some idea of how I
* was able to figure out these algorithms)</p>
*
* <p>Decrypting the key works as follows:
*
* <ol>
* <li>The key length is the length of the ciphertext minus 40. The
* encrypted key, <code>ekey</code>, is the middle bytes of the
* ciphertext.</li>
* <li>Take the first 20 bytes of the encrypted key as a seed value,
* <code>K[0]</code>.</li>
* <li>Compute <code>K[1] ... K[n]</code>, where
* <code>|K[i]| = 20</code>, <code>n = ceil(|ekey| / 20)</code>, and
* <code>K[i] = SHA-1(UTF-16BE(password) + K[i-1])</code>.</li>
* <li><code>key = ekey ^ (K[1] + ... + K[n])</code>.</li>
* <li>The last 20 bytes are the checksum, computed as <code>H =
* SHA-1(UTF-16BE(password) + key)</code>. If this value does not match
* the last 20 bytes of the ciphertext, output <code>FAIL</code>. Otherwise,
* output <code>key</code>.</li>
* </ol>
*
* <p>The signature is defined as <code>SHA-1(UTF-16BE(password) +
* US_ASCII("Mighty Aphrodite") + encoded_keystore)</code> (yup, Sun
* engineers are just that clever).
*
* <p>(Above, SHA-1 denotes the secure hash algorithm, UTF-16BE the
* big-endian byte representation of a UTF-16 string, and US_ASCII the
* ASCII byte representation of the string.)
*
* <p>The source code of this class should be available in the file <a
* href="http://metastatic.org/source/JKS.java">JKS.java</a>.
*
* @author Casey Marshall (rsdio@metastatic.org)
*/
public class JKS extends KeyStoreSpi
{
// Constants and fields.
// ------------------------------------------------------------------------
/** Ah, Sun. So goddamned clever with those magic bytes. */
private static final int MAGIC = 0xFEEDFEED;
private static final int PRIVATE_KEY = 1;
private static final int TRUSTED_CERT = 2;
private final Vector aliases;
private final HashMap trustedCerts;
private final HashMap privateKeys;
private final HashMap certChains;
private final HashMap dates;
// Constructor.
// ------------------------------------------------------------------------
public JKS()
{
super();
aliases = new Vector();
trustedCerts = new HashMap();
privateKeys = new HashMap();
certChains = new HashMap();
dates = new HashMap();
}
// Instance methods.
// ------------------------------------------------------------------------
public Key engineGetKey(String alias, char[] password)
throws NoSuchAlgorithmException, UnrecoverableKeyException
{
if (!privateKeys.containsKey(alias))
return null;
byte[] key = decryptKey((byte[]) privateKeys.get(alias),
charsToBytes(password));
Certificate[] chain = engineGetCertificateChain(alias);
if (chain.length > 0)
{
try
{
// Private and public keys MUST have the same algorithm.
KeyFactory fact = KeyFactory.getInstance(
chain[0].getPublicKey().getAlgorithm());
return fact.generatePrivate(new PKCS8EncodedKeySpec(key));
}
catch (InvalidKeySpecException x)
{
throw new UnrecoverableKeyException(x.getMessage());
}
}
else
return new SecretKeySpec(key, alias);
}
public Certificate[] engineGetCertificateChain(String alias)
{
return (Certificate[]) certChains.get(alias);
}
public Certificate engineGetCertificate(String alias)
{
return (Certificate) trustedCerts.get(alias);
}
public Date engineGetCreationDate(String alias)
{
return (Date) dates.get(alias);
}
// XXX implement writing methods.
public void engineSetKeyEntry(String alias, Key key, char[] passwd, Certificate[] certChain)
throws KeyStoreException
{
if (trustedCerts.containsKey(alias))
throw new KeyStoreException("\"" + alias + " is a trusted certificate entry");
privateKeys.put(alias, encryptKey(key, charsToBytes(passwd)));
if (certChain != null)
certChains.put(alias, certChain);
else
certChains.put(alias, new Certificate[0]);
if (!aliases.contains(alias))
{
dates.put(alias, new Date());
aliases.add(alias);
}
}
public void engineSetKeyEntry(String alias, byte[] encodedKey, Certificate[] certChain)
throws KeyStoreException
{
if (trustedCerts.containsKey(alias))
throw new KeyStoreException("\"" + alias + "\" is a trusted certificate entry");
try
{
new EncryptedPrivateKeyInfo(encodedKey);
}
catch (IOException ioe)
{
throw new KeyStoreException("encoded key is not an EncryptedPrivateKeyInfo");
}
privateKeys.put(alias, encodedKey);
if (certChain != null)
certChains.put(alias, certChain);
else
certChains.put(alias, new Certificate[0]);
if (!aliases.contains(alias))
{
dates.put(alias, new Date());
aliases.add(alias);
}
}
public void engineSetCertificateEntry(String alias, Certificate cert)
throws KeyStoreException
{
if (privateKeys.containsKey(alias))
throw new KeyStoreException("\"" + alias + "\" is a private key entry");
if (cert == null)
throw new NullPointerException();
trustedCerts.put(alias, cert);
if (!aliases.contains(alias))
{
dates.put(alias, new Date());
aliases.add(alias);
}
}
public void engineDeleteEntry(String alias) throws KeyStoreException
{
aliases.remove(alias);
}
public Enumeration engineAliases()
{
return aliases.elements();
}
public boolean engineContainsAlias(String alias)
{
return aliases.contains(alias);
}
public int engineSize()
{
return aliases.size();
}
public boolean engineIsKeyEntry(String alias)
{
return privateKeys.containsKey(alias);
}
public boolean engineIsCertificateEntry(String alias)
{
return trustedCerts.containsKey(alias);
}
public String engineGetCertificateAlias(Certificate cert)
{
for (Iterator keys = trustedCerts.keySet().iterator(); keys.hasNext(); )
{
String alias = (String) keys.next();
if (cert.equals(trustedCerts.get(alias)))
return alias;
}
return null;
}
public void engineStore(OutputStream out, char[] passwd)
throws IOException, NoSuchAlgorithmException, CertificateException
{
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(charsToBytes(passwd));
md.update("Mighty Aphrodite".getBytes("UTF-8"));
DataOutputStream dout = new DataOutputStream(new DigestOutputStream(out, md));
dout.writeInt(MAGIC);
dout.writeInt(2);
dout.writeInt(aliases.size());
for (Enumeration e = aliases.elements(); e.hasMoreElements(); )
{
String alias = (String) e.nextElement();
if (trustedCerts.containsKey(alias))
{
dout.writeInt(TRUSTED_CERT);
dout.writeUTF(alias);
dout.writeLong(((Date) dates.get(alias)).getTime());
writeCert(dout, (Certificate) trustedCerts.get(alias));
}
else
{
dout.writeInt(PRIVATE_KEY);
dout.writeUTF(alias);
dout.writeLong(((Date) dates.get(alias)).getTime());
byte[] key = (byte[]) privateKeys.get(alias);
dout.writeInt(key.length);
dout.write(key);
Certificate[] chain = (Certificate[]) certChains.get(alias);
dout.writeInt(chain.length);
for (int i = 0; i < chain.length; i++)
writeCert(dout, chain[i]);
}
}
byte[] digest = md.digest();
dout.write(digest);
}
public void engineLoad(InputStream in, char[] passwd)
throws IOException, NoSuchAlgorithmException, CertificateException
{
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(charsToBytes(passwd));
md.update("Mighty Aphrodite".getBytes("UTF-8")); // HAR HAR
aliases.clear();
trustedCerts.clear();
privateKeys.clear();
certChains.clear();
dates.clear();
DataInputStream din = new DataInputStream(new DigestInputStream(in, md));
if (din.readInt() != MAGIC)
throw new IOException("not a JavaKeyStore");
din.readInt(); // version no.
final int n = din.readInt();
aliases.ensureCapacity(n);
if (n < 0)
throw new IOException("negative entry count");
for (int i = 0; i < n; i++)
{
int type = din.readInt();
String alias = din.readUTF();
aliases.add(alias);
dates.put(alias, new Date(din.readLong()));
switch (type)
{
case PRIVATE_KEY:
int len = din.readInt();
byte[] encoded = new byte[len];
din.read(encoded);
privateKeys.put(alias, encoded);
int count = din.readInt();
Certificate[] chain = new Certificate[count];
for (int j = 0; j < count; j++)
chain[j] = readCert(din);
certChains.put(alias, chain);
break;
case TRUSTED_CERT:
trustedCerts.put(alias, readCert(din));
break;
default:
throw new IOException("malformed key store");
}
}
byte[] hash = new byte[20];
din.read(hash);
if (MessageDigest.isEqual(hash, md.digest()))
throw new IOException("signature not verified");
}
// Own methods.
// ------------------------------------------------------------------------
private static Certificate readCert(DataInputStream in)
throws IOException, CertificateException, NoSuchAlgorithmException
{
String type = in.readUTF();
int len = in.readInt();
byte[] encoded = new byte[len];
in.read(encoded);
CertificateFactory factory = CertificateFactory.getInstance(type);
return factory.generateCertificate(new ByteArrayInputStream(encoded));
}
private static void writeCert(DataOutputStream dout, Certificate cert)
throws IOException, CertificateException
{
dout.writeUTF(cert.getType());
byte[] b = cert.getEncoded();
dout.writeInt(b.length);
dout.write(b);
}
private static byte[] decryptKey(byte[] encryptedPKI, byte[] passwd)
throws UnrecoverableKeyException
{
try
{
EncryptedPrivateKeyInfo epki =
new EncryptedPrivateKeyInfo(encryptedPKI);
byte[] encr = epki.getEncryptedData();
byte[] keystream = new byte[20];
System.arraycopy(encr, 0, keystream, 0, 20);
byte[] check = new byte[20];
System.arraycopy(encr, encr.length-20, check, 0, 20);
byte[] key = new byte[encr.length - 40];
MessageDigest sha = MessageDigest.getInstance("SHA1");
int count = 0;
while (count < key.length)
{
sha.reset();
sha.update(passwd);
sha.update(keystream);
sha.digest(keystream, 0, keystream.length);
for (int i = 0; i < keystream.length && count < key.length; i++)
{
key[count] = (byte) (keystream[i] ^ encr[count+20]);
count++;
}
}
sha.reset();
sha.update(passwd);
sha.update(key);
if (!MessageDigest.isEqual(check, sha.digest()))
throw new UnrecoverableKeyException("checksum mismatch");
return key;
}
catch (Exception x)
{
throw new UnrecoverableKeyException(x.getMessage());
}
}
private static byte[] encryptKey(Key key, byte[] passwd)
throws KeyStoreException
{
try
{
MessageDigest sha = MessageDigest.getInstance("SHA1");
SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
byte[] k = key.getEncoded();
byte[] encrypted = new byte[k.length + 40];
byte[] keystream = rand.getSeed(20);
System.arraycopy(keystream, 0, encrypted, 0, 20);
int count = 0;
while (count < k.length)
{
sha.reset();
sha.update(passwd);
sha.update(keystream);
sha.digest(keystream, 0, keystream.length);
for (int i = 0; i < keystream.length && count < k.length; i++)
{
encrypted[count+20] = (byte) (keystream[i] ^ k[count]);
count++;
}
}
sha.reset();
sha.update(passwd);
sha.update(k);
sha.digest(encrypted, encrypted.length - 20, 20);
// 1.3.6.1.4.1.42.2.17.1.1 is Sun's private OID for this
// encryption algorithm.
return new EncryptedPrivateKeyInfo("1.3.6.1.4.1.42.2.17.1.1",
encrypted).getEncoded();
}
catch (Exception x)
{
throw new KeyStoreException(x.getMessage());
}
}
private static byte[] charsToBytes(char[] passwd)
{
byte[] buf = new byte[passwd.length * 2];
for (int i = 0, j = 0; i < passwd.length; i++)
{
buf[j++] = (byte) (passwd[i] >>> 8);
buf[j++] = (byte) passwd[i];
}
return buf;
}
}
@vamshi4001
Copy link

I was able to execute the changePassword.java and give a new password. Next I tried to change alias using code snippet mentioned
keytool -changealias -keystore new-release-keystore.jks -alias my_old_alias -destalias myalias
then it asks me password - where i give my newly given password
then asks me for old alias password - which is what I forgot right???? How can I enter that?

@equick
Copy link

equick commented Jan 3, 2016

@zach-klippenstein, Many thanks for providing this code. It's really useful to me as it gives me a way to monitor keystores for expiring certificates without needing to know the keystore password. I've uploaded that to https://github.com/equick/jkscerts if anyone's interested.

@Meinl
Copy link

Meinl commented Feb 24, 2016

Anyone could solve "java.security.UnrecoverableKeyException: Cannot recover key" trouble?

@deepak21p
Copy link

i tried your method but when i am signing my file then it shows error :
untitled
Publishing failed
Signing failed
jarsigner:key associated with sign not a private key

@faizworks
Copy link

First of all thank you so much. I was able to update password from your tool but I am getting following error when I try to sign and publish my app

Failed to sign package.\r\njarsigner: key associated with not a private key

Can you please help me on this :)

@MincDev
Copy link

MincDev commented Apr 22, 2016

If you go to your log files of Android Studio you should be able to retrieve your password from a previous successful signing. It is written there with no problem. My problem however, for some reason my alias password does not want to accept my password, even though I can clearly see that it is the password that was used ever time I signed.

@Arihant1234
Copy link

I getting this error Please help

C:\Program Files\Java\jdk1.7.0_04\bin>javac ChangePassword.java
ChangePassword.java:13: error: cannot find symbol
private final static JKS j = new JKS();
^
symbol: class JKS
location: class ChangePassword
ChangePassword.java:13: error: cannot find symbol
private final static JKS j = new JKS();
^
symbol: class JKS
location: class ChangePassword
2 errors

@calevano
Copy link

calevano commented Sep 1, 2016

@tristaaan thanks...

@masdedy
Copy link

masdedy commented Nov 5, 2016

hi zack, thank you for your guidance.
i lost my key password and password of my key.jks file.
i download your file to folder where my project located.
i run it by command:
D:\myproject>javac ChangePassword.java
D:\myproject>java ChangePassword key.jks key2.jks
Enter keystore password: 1234567
Changing password on 'key.jks', writing to 'key2.jks'...
Enter new keystore password: 1234567
D:\myproject>

and i back to my android studio, trying to build generated signed APK using new keystore (key2.jks) and new password: 1234567

but still can not release new APK.

result is:
Error:Execution failed for task ':app:packageRelease'.

Failed to read key hamil from store "D:\myproject\key2.jks": Keystore was tampered with, or password was incorrect

please kindly advice.
thanks

@marquessbr
Copy link

thank you my friend!
its work for me

@ibnuaulia
Copy link

do you want to help me ,please..

@ka05
Copy link

ka05 commented Nov 29, 2016

Thank you very much!

@MuhammadShaker
Copy link

If some one had "java.security.UnrecoverableKeyException: Cannot recover key", after changing the password using above code try to change it again using keytool "keytool -storepasswd -keystore my.keystore" after that change alias name if you want "keytool -keypasswd -alias <key_name> -keystore my.keystore"

it works for me,
Thanks.

@Nazmul56
Copy link

Nazmul56 commented Apr 29, 2017

I also recovered my password thanks a lot.

@erpalma
Copy link

erpalma commented May 9, 2017

Keystore password is used to verify keystore integrity not confidentiality, so the point is that if you have an application which loads a keystore using password "lol" and you change the keystore password to "nope", then the application knows that the keystore has been tampered.

If you want to list (and ONLY list) the content of a keystore ignoring the integrity check you can just press enter when keytool ask you "Enter keystore password:".

If you want to do this programmatically or you want to change the password you can do it in just 3 lines:

KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(new FileInputStream("keystore.jks"), null);
keystore.store(new FileOutputStream("keystore.jks"), "myNewPass".toCharArray());

Loading a keystore with keystore password argument null ignores the integrity check. To be clear this is not a way to recover the integrity password, but a way to change it.

@CLEAOSBORNE
Copy link

I'm getting a compile error:

C:\Program Files\Java\jdk1.7.0_04\bin>javac ChangePassword.java
ChangePassword.java:7: error: cannot find symbol
private final static JKS j = new JKS();
^
symbol: class JKS
location: class ChangePassword
ChangePassword.java:7: error: cannot find symbol
private final static JKS j = new JKS();
^
symbol: class JKS
location: class ChangePassword
2 errors

Any ideas or suggestions?

@Tymski
Copy link

Tymski commented Jul 15, 2017

It worked!

@NikhilRaut
Copy link

Able to change the pass but what about the "Alias Password" ...?, I forgot that also, How to change or retrieve Alias Password..?

@marccrobbins
Copy link

Can this change an Alias entry password as well?

@sainumtown
Copy link

It worked! Thank you.

@RameshJadhavmagic
Copy link

Hi Everyone,

I have my keystore password but forgot key alias password.

Can we reset or retrieve key alias password?

Please let me know . It's a little bit urgent

Thanks in advance!!!!

@hetykai
Copy link

hetykai commented Jul 9, 2018

How can we modify the alias password?

@badarshahzad
Copy link

@sainumtown how did you do this? did you also forgot keystore password ? and did you reset the new password using above example?

@gnanasuriyan
Copy link

destalias

I am trying to change the alias name but it's still asking password for my old alias which I forgot. Can you help me in this case.

@udb1997
Copy link

udb1997 commented Mar 26, 2019

Is there a support for jceks file? I am trying to use this for jceks file that I forgot the password

@danedang
Copy link

Caused by: java.security.UnrecoverableKeyException: Cannot recover key
at sun.security.provider.KeyProtector.recover(KeyProtector.java:315)
at sun.security.provider.JavaKeyStore.engineGetKey(JavaKeyStore.java:141)
at sun.security.provider.JavaKeyStore$JKS.engineGetKey(JavaKeyStore.java:56)
at java.security.KeyStoreSpi.engineGetEntry(KeyStoreSpi.java:473)
at sun.security.provider.KeyStoreDelegator.engineGetEntry(KeyStoreDelegator.java:172)
at sun.security.provider.JavaKeyStore$DualFormatJKS.engineGetEntry(JavaKeyStore.java:70)
at java.security.KeyStore.getEntry(KeyStore.java:1521)
at com.android.ide.common.signing.KeystoreHelper.getCertificateInfo(KeystoreHelper.java:191)
... 32 more

@KOSSOKO
Copy link

KOSSOKO commented Nov 27, 2019

hi zack, thank you for your guidance.
i lost my key password and password of my key.jks file.
i download your file to folder where my project located.
i run it by command:
D:\myproject>javac ChangePassword.java
D:\myproject>java ChangePassword key.jks key2.jks
Enter keystore password: 1234567
Changing password on 'key.jks', writing to 'key2.jks'...
Enter new keystore password: 1234567
D:\myproject>

and i back to my android studio, trying to build generated signed APK using new keystore (key2.jks) and new password: 1234567

but still can not release new APK.

result is:
Error:Execution failed for task ':app:packageRelease'.

Failed to read key hamil from store "D:\myproject\key2.jks": Keystore was tampered with, or password was incorrect

please kindly advice.
thanks

Did you find a solution ? I'm facing the same issue

@KOSSOKO
Copy link

KOSSOKO commented Nov 27, 2019

i tried your method but when i am signing my file then it shows error :
untitled
Publishing failed
Signing failed
jarsigner:key associated with sign not a private key

I have exactly the same error. How did you do ?

@jlizasoDevrank
Copy link

jlizasoDevrank commented May 20, 2020

So, I guess this is a dead end.
Noone answered about the issue with the alias password.
If I don't remember the Key password, for sure I don't remember the other one that usually is the same one. So there's no solution.
After using the java in here to change the password, and with the new resulting keystore file, I get the following results throwing the following commands in the command line:

Trying to do anything with the keytool

Enter keystore password: ALRIGHT
Enter key password for <alias_name>
keytool error: java.security.UnrecoverableKeyException: Cannot recover key

Trying to jarsigner:

Enter Passphrase for keystore: ALRIGHT
Enter key password for alias_name:
jarsigner: unable to recover key from keystore

Trying to apksigner:

Keystore password for signer #1: ALRIGHT
Key "alias_name" password for signer #1:
Failed to load signer "signer #1"
java.io.IOException: Failed to obtain key with alias "alias_name from keystore_file.keystore. Wrong password?
        at com.android.apksigner.SignerParams.loadPrivateKeyAndCertsFromKeyStore(SignerParams.java:320)
        at com.android.apksigner.SignerParams.loadPrivateKeyAndCerts(SignerParams.java:181)
        at com.android.apksigner.ApkSignerTool.sign(ApkSignerTool.java:277)
        at com.android.apksigner.ApkSignerTool.main(ApkSignerTool.java:83)
Caused by: java.security.UnrecoverableKeyException: Cannot recover key
        at sun.security.provider.KeyProtector.recover(KeyProtector.java:315)
        at sun.security.provider.JavaKeyStore.engineGetKey(JavaKeyStore.java:141)
        at sun.security.provider.JavaKeyStore$JKS.engineGetKey(JavaKeyStore.java:56)
        at sun.security.provider.KeyStoreDelegator.engineGetKey(KeyStoreDelegator.java:96)
        at sun.security.provider.JavaKeyStore$DualFormatJKS.engineGetKey(JavaKeyStore.java:70)
        at java.security.KeyStore.getKey(KeyStore.java:1023)
        at com.android.apksigner.SignerParams.getKeyStoreKey(SignerParams.java:375)
        at com.android.apksigner.SignerParams.loadPrivateKeyAndCertsFromKeyStore(SignerParams.java:302)

Is there anyone that has an answer to this?
All those that said that "it worked", did you remember the alias password but not the key one?
Where you able to sign the apk and post an update to an app already in the Play Store with it?

@Icevybe
Copy link

Icevybe commented Dec 16, 2021

Please who can assist me in importing my keystore? It's showing "failed to import"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment