我创建了以下方法
public static PublicKey readPublicKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
PublicKey key = null;
CertificateFactory fact;
try {
// MBFS certificate to be used
FileInputStream is = new FileInputStream(filename);
fact = CertificateFactory.getInstance("X.509");
System.out.println(is.toString());
X509Certificate cer = (X509Certificate) fact.generateCertificate(is);
key = cer.getPublicKey();
System.out.println(key.getAlgorithm());
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return key;
}
用于加密
public static byte[] encrypt(PublicKey key, byte[] plaintext) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plaintext);
}
我有很长的xml字符串,并使用以下两种方法
byte[] message = xmlMessage.getBytes();
byte[] secret = encrypt(publicKey, message);
但是它给了我使用rsa时数据不能超过256个字节
证书是客户端的碎片,它表示签名算法sha256RS。
答案 0 :(得分:0)
通常,您将使用对称密码来加密文档(使用随机密钥),然后使用RSA加密密钥。这不仅克服了长度问题,而且速度也快得多。