java.security.SignatureException:签名长度不正确:得到66但是期待64

时间:2012-12-13 23:09:32

标签: java security rsa encryption-asymmetric

这就是我生成密钥对的方式:

private void generateKeyPair() throws NoSuchAlgorithmException, IOException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
//        kpg.initialize(1024);
kpg.initialize(512);
KeyPair kp = kpg.genKeyPair();

System.out.println("Generated Key Pair");
dumpKeyPair(kp);
}

以下创建令牌和令牌签名:

public String makeTokenSignature(String message) throws GeneralSecurityException {
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initSign(getPrivateKey());
signature.update(message.getBytes());

byte[] signatureValue = signature.sign();
return encodeBASE64(signatureValue);
}

public String createTokenString(String userName) {
long currentTime = System.currentTimeMillis();
String tokenString = userName + "some other values..";

return tokenString;
}

这是验证码:

public boolean isValidTokenSignature(String message, byte[] signatureValue) throws     GeneralSecurityException {
Signature signature2 = Signature.getInstance("SHA1withRSA");
signature2.initVerify(getPublicKey());
signature2.update(message.getBytes());
return signature2.verify(signatureValue);
}

我看到的问题: 当userName.length> = 16&& userName< 18,验证码工作正常。有人能告诉我密钥大小如何影响userName的允许长度? 当我在GenerateKeyPair代码中使用kpg.initialize(1024)时,用户名的下限是18个字符。

0 个答案:

没有答案