我想要的是使用android keystore,以便我想隐藏加密密钥。
这是代码。
String alias = "testalias";
KeyStoreManager.init(getApplicationContext());
String plain = "helloWorld";
String enc = KeyStoreManager.encryptData(plain, alias);
Log.d(TAG, "enc = " + enc);
我将enc
字符串保存到名为encText
的变量中,然后再次运行。
String alias = "testalias";
KeyStoreManager.init(getApplicationContext());
String plain = "helloWorld";
String enc = KeyStoreManager.encryptData(plain, alias);
Log.d(TAG, "enc = " + enc);
String encText = "AcasR3G2Oai+Misqcsacw0Fmnj96vrEqV0Cdmc3ycT2FoEvmVZFAk6ZyG+Srww26R+slDl/32a/D\n" +
"uaQQQxBqbW0uO5pwUL5HIFb3WBiPm1dh5JsHfQ==";
String dec = KeyStoreManager.decryptData(encText, alias);
// dec : helloWorld
已正确解码。
但是,此问题在其他设备上发生。在另一台设备上解密encText
将导致PaddingException
。
这不可能吗?
===关于Keystoremanager ===
基于java.security.KeyStore]的Keystoremanager类使用* .jks文件
public String encryptString(String alias, String plainText) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, IOException, UnrecoverableEntryException, KeyStoreException, InvalidAlgorithmParameterException {
String encryptedText = "";
createKeys(context, alias);
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);
RSAPublicKey publicKey = (RSAPublicKey) privateKeyEntry.getCertificate().getPublicKey();
Cipher inCipher = getCipher();
inCipher.init(Cipher.ENCRYPT_MODE, publicKey);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, inCipher);
cipherOutputStream.write(plainText.getBytes(CHARSET));
cipherOutputStream.close();
byte[] vals = outputStream.toByteArray();
encryptedText = (Base64.encodeToString(vals, Base64.DEFAULT));
return encryptedText;
}
我想要的是使用android keystore,以便我想隐藏加密密钥。这是代码。字符串别名=“ testalias”; KeyStoreManager.init(getApplicationContext());普通字符串=“ ...
好吧,四处走走。一种密码方案,用于加密密码的AES密钥存储在密钥库中。