开发者

Is there a size restriction on signatures in Java (java.security)?

开发者 https://www.devze.com 2022-12-27 19:05 出处:网络
I am trying to sign a token object using SHA1. I am using bouncycastle as the security provider. Whenever the program tries to sign something it gives me this error.

I am trying to sign a token object using SHA1. I am using bouncycastle as the security provider. Whenever the program tries to sign something it gives me this error.

java.security开发者_运维知识库.SignatureException: java.lang.IllegalArgumentException: input data too large.

What is the maximum size for signing something? Do you have any suggestions about how I can sign this object?


The input size is limited to the size of the key. If you use a 1024 bit key, you are limited to 128 bytes.

Typically, you are signing the digest (hash value), not the actual data.


To fix that error one just need to use a larger key size. For example, if SHA 512 bit is chosen, the key could be a 1024 bit one. But you will fail with a key of the same (512) or lesser length.

BouncyCastle just gives us an unusable error message. But the std lib does its job right. Compare them:

// using a 512 bit key here
// leads to this error message if Sun's standard provider is used
Signature sig = Signature.getInstance("SHA512withRSA", "SunRsaSign");
rsa.initSign(privateKey);
rsa.update(data);
rsa.sign();

java.security.InvalidKeyException: Key is too short for this signature algorithm
    at sun.security.rsa.RSASignature.initCommon(RSASignature.java:129)
    at sun.security.rsa.RSASignature.engineInitSign(RSASignature.java:111)
    at sun.security.rsa.RSASignature.engineInitSign(RSASignature.java:101)
    at java.security.Signature$Delegate.engineInitSign(Signature.java:1127)
    at java.security.Signature.initSign(Signature.java:511)


// using a 512 bit key here
// leads to this error message if the BounceCastle provider is used
Signature sig = Signature.getInstance("SHA512withRSA", "BC");
    ...

java.security.SignatureException: java.lang.IllegalArgumentException: input data too large
    at org.bouncycastle.jce.provider.JDKDigestSignature.engineSign(Unknown Source)
    at java.security.Signature$Delegate.engineSign(Signature.java:1160)
    at java.security.Signature.sign(Signature.java:553)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号