【问题标题】:XML String encryption giving me Data must not be longer than 245 bytes when using RSAXML字符串加密给我使用RSA时数据不能超过245个字节
【发布时间】:2018-03-04 20:40:02
【问题描述】:

我已经创建了以下方法

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。

【问题讨论】:

    标签: java xml encryption rsa


    【解决方案1】:

    通常,您会使用对称密码来加密文档(使用随机密钥),然后只使用 RSA 加密密钥。这不仅克服了长度问题,而且速度更快。

    【讨论】:

    • 我已经用随机密钥完成了这个,但是我怎样才能将我的公钥用于相同的密钥
    猜你喜欢
    • 2020-06-10
    • 2012-04-17
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2014-10-29
    • 1970-01-01
    相关资源
    最近更新 更多