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

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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);/* 要加密的字符串进行编码 */<br> byte[] doFinal = tcsCipher.doFinal(encode);/*加密*/<br> String tcsEncryptPassword = Base64.encodeToString(doFinal, Base64.DEFAULT);/* 将加密后的字符串进行编码 */
上一篇:js rsa sign使用笔记(加密,解密,签名,验签)


下一篇:java加密算法