从安卓中的密钥库中检索证书/密钥相关信息



我是Android的新手。我已经创建了密钥库文件。在 PrivateActivity 类下,我还加载了密钥库。我的任务是在将别名(我创建的(名称放在 android 模拟器上后获取任何与密钥/证书相关的信息。我还更改了 Gradle 构建中的路径,但我想了解我是否必须在代码中的某处添加密钥库文件名或路径,或者我必须编写一个将从密钥库中检索所有密钥和证书相关信息的方法?因为编写方法后,我无法获取与证书/密钥相关的信息。 私人活动.java

``` package org.jssec.android.activity.privateactivity;```
```import java.io.IOException;
import java.security.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import java.io.InputStream;```

import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.CertificateException;
import java.util.Enumeration;
import javax.crypto.KeyGenerator;
//Appropriate libraries need to be also imported
public class PrivateActivity extends Activity {
private KeyStore my_ks;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.private_activity);
// Handle the received Intent carefully and securely,
// even though the Intent was sent from the same application.
Intent intent=this.getIntent();
PrivateUserActivity.keystore_info param = (PrivateUserActivity.keystore_info) intent.getSerializableExtra("CL_k");

//CODE MUST BE ADDED HERE IN ORDER TO HANDLE the COLLECTED OBJECT from the Intent
Bundle extras = intent.getExtras();
String data = getIntent().getExtras().getString("Cl_k");

}
public void onReturnResultClick(View view) throws KeyStoreException,
UnrecoverableEntryException, NoSuchAlgorithmException, CertificateException, IOException, NoSuchProviderException {
// Sensitive information can be sent since it is sending
// and receiving all within the same application.
Intent intent = new Intent();
Intent i = new Intent(this, PrivateUserActivity.class);

//Create instance
KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
Enumeration<String> aliases = ks.aliases();


//CODE MUST BE ADDED HERE IN ORDER TO send the appropriate values to the PrivateUserActivity
//i.e. the "Sensitive Info" value should be changed with something different

//// return new KeyPair (publicKey,privateKey);
intent.putExtra("RESULT", "Sensitive Info");
setResult(RESULT_OK, intent);
finish();
}
}```
Another class is this PrivateUserActivity.java
```/*
* Copyright (C) 2012-2019 Japan Smartphone Security Association
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jssec.android.activity.privateactivity;
import java.io.IOException;
import java.security.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import java.io.InputStream;

import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.CertificateException;
import java.util.Enumeration;
import javax.crypto.KeyGenerator;
//Appropriate libraries need to be also imported
public class PrivateActivity extends Activity {
private KeyStore my_ks;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.private_activity);
// Handle the received Intent carefully and securely,
// even though the Intent was sent from the same application.
Intent intent=this.getIntent();
PrivateUserActivity.keystore_info param = (PrivateUserActivity.keystore_info) intent.getSerializableExtra("CL_k");

//CODE MUST BE ADDED HERE IN ORDER TO HANDLE the COLLECTED OBJECT from the Intent
Bundle extras = intent.getExtras();
String data = getIntent().getExtras().getString("Cl_k");

}
public void onReturnResultClick(View view) throws KeyStoreException,
UnrecoverableEntryException, NoSuchAlgorithmException, CertificateException, IOException, NoSuchProviderException {
// Sensitive information can be sent since it is sending
// and receiving all within the same application.
Intent intent = new Intent();
Intent i = new Intent(this, PrivateUserActivity.class);

//Create instance
KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
Enumeration<String> aliases = ks.aliases();


//CODE MUST BE ADDED HERE IN ORDER TO send the appropriate values to the PrivateUserActivity
//i.e. the "Sensitive Info" value should be changed with something different

//// return new KeyPair (publicKey,privateKey);
intent.putExtra("RESULT", "Sensitive Info");
setResult(RESULT_OK, intent);
finish();
}
}```

不清楚你的问题是什么,但如果你使用的是KeyStore.getInstance("AndroidKeyStore"(,则不需要加载任何文件。 Android 会自动管理密钥和任何底层文件。

要实际生成保存在 Android 密钥库中的文件,您需要在设置生成参数时指定"Android密钥库"。 请参阅此处的文档:https://developer.android.com/training/articles/keystore

相关内容

最新更新