微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么命令行 dalvikvm 使用标准 Java 安全库keystore而不是使用 Android 版本

如何解决为什么命令行 dalvikvm 使用标准 Java 安全库keystore而不是使用 Android 版本

我正在执行一项实验:在命令行 Java 应用程序中使用 Android 密钥库。我有一个密钥库的 Activity hello world 示例:
https://github.com/phanirajabhandari/android-keystore-example

我刚刚转换了 EncryptionUtils 中的私有方法 getKeyStore() 并在 MainActivity 上添加了一行,以打印 getKeyStore()。 MainActivity如下:

public class MainActivity extends AppCompatActivity {

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Timber.plant(new Timber.DebugTree());

    String value = "Password/Token to be encrypted";

    String encryptedValue = EncryptionUtils.encrypt(this,value);
    Timber.d(" Encrypted Value :" + encryptedValue);

    String decryptedValue = EncryptionUtils.decrypt(this,encryptedValue);
    Timber.d(" Decrypted Value :" + decryptedValue);
    
    Timber.d("Keystore:" + EncryptionUtils.getKeyStore());
  }
}

getKeyStore() 方法只返回 Android 密钥库:

  public static KeyStore getKeyStore() {
    KeyStore keyStore = null;
    try {
      keyStore = KeyStore.getInstance(EncryptionKeyGenerator.ANDROID_KEY_STORE);
      keyStore.load(null);
      //FileOutputStream fos = new FileOutputStream("/data/data/com.example.keystore/mykeystore");
      //keyStore.store(fos,"".tochararray());
    } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) {
      Timber.e(e);
    }
    return keyStore;
  }

我使用 Android Studio 和项目提供的 Gradle 构建了 apk,在日志中我可以看到正确创建了 Keystore 对象:

2021-01-09 10:51:53.246 4103-4103/com.example.keystore D/MainActivity:  Encrypted Value :fH92kpVlBIRTVF3SnTiSZejZtDRa0H2uj1Ze2kXJ8Obt9OEO5_Qd0RNVlAkK8Q==
2021-01-09 10:51:53.272 4103-4103/com.example.keystore D/MainActivity:  Decrypted Value :Password/Token to be encrypted
2021-01-09 10:51:53.275 4103-4103/com.example.keystore D/MainActivity: Keystore:java.security.KeyStore@afbdfae

现在我正在尝试进行以下实验:

  1. 创建一个 MainExample.java 类:
package com.example.keystore;

public class MainExample {
    public static void main(String[] args) {
       System.out.println("Hello") ;
        System.out.println(EncryptionUtils.getKeyStore()) ;
    }
}
  1. 在 android 模拟器中构建并上传 apk(genymotion,root 访问权限):
adb push app-debug.apk /data/local/tmp/   
  1. 使用dalvikvm执行主hello world
dalvikvm -cp app-debug.apk com.example.keystore.MainExample                       <
Hello
java.security.KeyStoreException: AndroidKeyStore not found
        at java.security.KeyStore.getInstance(KeyStore.java:890)
        at com.example.keystore.EncryptionUtils.getKeyStore(EncryptionUtils.java:40)
        at com.example.keystore.MainExample.main(MainExample.java:6)
Caused by: java.security.NoSuchAlgorithmException: AndroidKeyStore KeyStore not available
        at sun.security.jca.GetInstance.getInstance(GetInstance.java:159)
        at java.security.Security.getImpl(Security.java:628)
        at java.security.KeyStore.getInstance(KeyStore.java:887)
        ... 2 more
null  

dalvikvm 好像没有看到 AndroidKeyStore,好像 dalvikvm 用的不是 Android 的“java.security”包,而是官方的 Java 包。为什么 dalvikvm 命令行执行与 Activity 执行不同? 提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。