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);/* 将加密后的字符串进行编码 */
时间: 2024-10-10 01:02:31