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);/* 將加密后的字符串進行編碼 */