Android KeyStore问题,另一个设备与同一个应用程序

问题描述 投票:1回答:1

我想要的是使用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

这不可能吗?

  1. device'A'->使用KeyStore加密文本。
  2. device'B'->使用KeyStore解密该文本。 ->例外(使用相同的.jks,相同的应用程序,相同的代码)

===关于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());普通字符串=“ ...

java android keystore
1个回答
0
投票

好吧,四处走走。一种密码方案,用于加密密码的AES密钥存储在密钥库中。

© www.soinside.com 2019 - 2024. All rights reserved.