首页 > 代码库 > 通过证书拿到公钥,进行加密、解密

通过证书拿到公钥,进行加密、解密

InputStream is = this.getAssets().open(fileName);//证书读取流CertificateFactory cf = CertificateFactory.getInstance("X.509");//获取X.509的证书工厂X509Certificate certificate = (X509Certificate) cf.generateCertificate(is);//获取证书实例PublicKey publicKey = certificate.getPublicKey();//获取证书公钥Cipher tcsCipher = Cipher.getInstance(publicKey.getAlgorithm());//通过公钥的加密算法获取加解密实例    tcsCipher.init(Cipher.ENCRYPT_MODE, publicKey);//通过公钥初始化实例byte[] encode = Base64.encode("123".getBytes(),Base64.DEFAULT);/* 要加密的字符串进行编码 */
byte[] doFinal = tcsCipher.doFinal(encode);/*加密*/
String tcsEncryptPassword = Base64.encodeToString(doFinal, Base64.DEFAULT);/* 将加密后的字符串进行编码 */

  

  

通过证书拿到公钥,进行加密、解密