java非对称加密数据大小,RSA的加密数据长度非常大

I want that encrypted data have the same length as my original text, because I must respect the constraint of length. I am using BouncyCastle.

Here's my code to generate the keys pair:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

keyGen.initialize(512);

KeyPair keypair = keyGen.genKeyPair();

PublicKey pub = keypair.getPublic();

byte[] pubs = pub.getEncoded();

And here's my code to encrypt:

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");

PublicKey pk = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(pubs));

cipher.init(Cipher.ENCRYPT_MODE, pk);

byte[] cipherBytes = cipher.doFinal(plainArray);

The encoded data is very large, what can I do to make as small as original data ?

解决方案

Nothing, at least not regarding the RSA. RSA requires a certain amount of padding to be secure, and as the data will be seen as random data by any compression method, you cannot compress it either.

Of course, you should not directly encrypt data directly using RSA, you should encrypt a random session/data key instead. But even then, you will have the encrypted session key as overhead.

You can remove some bits for the asymmetric encrypted data/key by using Elliptic Curve Cryptography (the output of which is double the key size minimum, but key sizes are much smaller to achieve the same level of security). EC cryptography is not for the meek though, it has lots of complexity.

512 bit RSA is considered insecure by the way. Use 1024 as minimum or keep to the ECRYPT II or NIST recommendations listed at http://www.keylength.com/ .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值