如何在Java中更改KeyStore文件的密码?

时间:2016-02-22 16:13:14

标签: java android keystore android-keystore java-security

我正在使用java KeyStore存储用户安全信息并使用密码锁定(加密)密钥存储区,并在需要时使用密码检索信息,这很正常。

但我不知道如何更改同一个keyStore文件的密码

我使用KeyStore.load(InputStream,Password)方法访问具有用户密码的密钥库,使用keyStore.store(Keystore,password)将值写入keyStore。

有没有办法更改keyStore文件的密码。 我检查了api文档在这里给出: http://docs.oracle.com/javase/7/docs/api/java/security/KeyStore.html

1 个答案:

答案 0 :(得分:3)

感谢@Danail Alexiev的工作,使用旧密码加载KeyStore并使用新密码覆盖相同的keyStoreStream将更改keyStore密码(使用新密码加密密钥库内容)

这是reffrence的代码:

InputStream keyStoreStream = ontext.openFileInput ( filename );
KeyStore keyStore = KeyStore.getInstance ( KeyStore.getDefaultType ( ) );
keyStore.load ( keyStoreStream, oldPass );
FileOutputStream fileOutputStream = context.openFileOutput ( filename, Context.MODE_PRIVATE );
keystore.store ( fileOutputStream, newPassword );
fileOutputStream.close ( );