检测安装的证书在我的Android设备 [英] Detect installed certificate in my android device
问题描述
我在应用程序启动时在我的应用程序中安装证书。我已通过以下几个链接,并成功安装证书。
我知道我们无法在没有用户交互的情况下静默安装证书。目前我不知道如何停止提示时间用户打开我的应用程序。
每当我的应用程序启动时,每次它要求用户安装证书。是否有某种方式可以通过编程方式检测证书(在这种情况下是我的证书)是否已经安装。
我在我的应用程序中安装证书的代码段
private void installCertificate()
{
try
{
BufferedInputStream bis = new BufferedInputStream(getAssets()。open(MY_CERT));
byte [] keychain = new byte [bis.available()];
bis.read(keychain);
Intent installIntent = KeyChain.createInstallIntent();
X509Certificate x509 = X509Certificate.getInstance(keychain);
installIntent.putExtra(KeyChain.EXTRA_CERTIFICATE,x509.getEncoded());
installIntent.putExtra(KeyChain.EXTRA_NAME,MY_CERT);
startActivityForResult(installIntent,INSTALL_KEYCHAIN_CODE);
}
catch(IOException e){
e.printStackTrace();
}
catch(CertificateException e)
{
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
if(requestCode = = INSTALL_KEYCHAIN_CODE)
{
switch(resultCode)
{
case Activity.RESULT_OK:
doTheTask();
break;
case Activity.RESULT_CANCELED:
finish();
break;
默认值:
super.onActivityResult(requestCode,resultCode,data);
}
}
}
<是从onCreate()调用。
请帮助我。
选择和改变方向切换/复制选项。任何机构知道如何在提示时停止文本选择?证书是否已安装
try
{
KeyStore ks = KeyStore.getInstance(AndroidCAStore) ;
if(ks!= null)
{
ks.load(null,null);
枚举aliases = ks.aliases();
while(aliases.hasMoreElements())
{
String alias =(String)aliases.nextElement();
java.security.cert.X509Certificate cert =(java.security.cert.X509Certificate)ks.getCertificate(alias);
if(cert.getIssuerDN()。getName()。contains(MyCert))
{
isCertExist = true;
break;
}
}
}
} catch(IOException e){
e.printStackTrace();
} catch(KeyStoreException e){
e.printStackTrace();
} catch(NoSuchAlgorithmException e){
e.printStackTrace();
} catch(java.security.cert.CertificateException e){
e.printStackTrace();
}
I am installing a certificate in my application when application starts. I have gone through few of the links as below and successfully install the certificate.
http://nelenkov.blogspot.in/2011/12/ics-trust-store-implementation.html
How to programmatically install a CA Certificate (for EAP WiFi configuration) in Android?
how to install CA certificate programmatically on Android without user interaction
I came to know we cannot install the certificate silently without user interaction.Currently I don't know how to stop prompt each time user open my app.
Whenever my application starts currently every time it ask user to install certificate. Is there some way I can detect whether a certificate(in this case my certificate) is already installed or not, programmatically.
Code snippet where I have installing certificate in my app
private void installCertificate()
{
try
{
BufferedInputStream bis = new BufferedInputStream(getAssets().open(MY_CERT));
byte[] keychain = new byte[bis.available()];
bis.read(keychain);
Intent installIntent = KeyChain.createInstallIntent();
X509Certificate x509 = X509Certificate.getInstance(keychain);
installIntent.putExtra(KeyChain.EXTRA_CERTIFICATE, x509.getEncoded());
installIntent.putExtra(KeyChain.EXTRA_NAME, MY_CERT);
startActivityForResult(installIntent, INSTALL_KEYCHAIN_CODE);
}
catch (IOException e) {
e.printStackTrace();
}
catch (CertificateException e)
{
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == INSTALL_KEYCHAIN_CODE)
{
switch (resultCode)
{
case Activity.RESULT_OK:
doTheTask();
break;
case Activity.RESULT_CANCELED:
finish();
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Also FYI, installCertificate() is called from onCreate().
Please help me out for the same. Any help will appreciated.
Query: When prompt comes for certificate name, entered text comes as selected and on orientation change cut/copy option comes. Any body knows how to stop text selection when prompt comes?!!!
I used below piece of code to check whether my certificate is installed or not
try
{
KeyStore ks = KeyStore.getInstance("AndroidCAStore");
if (ks != null)
{
ks.load(null, null);
Enumeration aliases = ks.aliases();
while (aliases.hasMoreElements())
{
String alias = (String) aliases.nextElement();
java.security.cert.X509Certificate cert = (java.security.cert.X509Certificate) ks.getCertificate(alias);
if (cert.getIssuerDN().getName().contains("MyCert"))
{
isCertExist = true;
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (java.security.cert.CertificateException e) {
e.printStackTrace();
}
这篇关于检测安装的证书在我的Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!