자바에서 서명을 확인해야합니다. 여러 매개 변수가있는 URL을 얻습니다. 그 중 하나는 서명 (16 진수 형식)입니다. 서명 된 메시지는 다른 모든 매개 변수의 연결에 대한 SHA-256 해시입니다. 수표에 사용할 공개 키가있는 인증서도 있습니다. 내가 테스트에 사용하는 모든 값은 올바른 것으로 가정되는 예제에 의해 나에게 주어지며, 단지 연결 문자열을 만듭니다. 서명 길이가 올바르지 않음
이
코드는 내가 실행 :// signed message --> hash of concat
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(concat.getBytes());
byte[] message = digest.digest();
System.out.println("message length "+message.length); // --> 32
// signature belonging to the message --> checkValue
System.out.println("check value length " +checkValue.length()); // --> 512
byte[] sigBytes = checkValue.getBytes();
System.out.println("check value bytes "+sigBytes.length); // --> 512
// public certificate of the CA
File file3 = new File(certificatePath);
byte[] encCertRSA = new byte[(int) file3.length()];
FileInputStream fis3 = new FileInputStream(file3);
fis3.read(encCertRSA);
fis3.close();
InputStream is = new ByteArrayInputStream(encCertRSA);
CertificateFactory f = CertificateFactory.getInstance("X.509");
X509Certificate certRSA = (X509Certificate)f.generateCertificate(is);
certRSA.checkValidity();
PublicKey pubKeyRSA = certRSA.getPublicKey();
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initVerify(pubKeyRSA);
// supply the Signature object with the data for which a signature was generated --> hash of concat
sig.update(message);
boolean isValid = sig.verify(sigBytes);
System.out.println("The signature of the email verifies: " + isValid);
이 오류가 난 얻을 수있다 :
java.security.SignatureException: Signature length not correct: got 512 but was expecting 256
at sun.security.rsa.RSASignature.engineVerify(Unknown Source)
at java.security.Signature$Delegate.engineVerify(Unknown Source)
at java.security.Signature.verify(Unknown Source)
내가 뭔가 잘못을하고 있습니까? 서명이 512가 아닌 256의 길이를 갖기를 기대했습니다. 서명 값의 하위 문자열을 256의 길이와 일치시키는 테스트를 실행하고 위의 오류가 발생하지 않지만 sig.verify는 false를 반환합니다. .
는 예, 값 i가에서 얻을 경우 이해하고 싶습니다 예제가 잘못되었거나 뭔가 빠졌을 때 .. – user4219558
'sigBytes' 및'checkValue' 변수를 변경하지 않았기 때문에 값이 잘못되었거나 잘못 나온 것처럼 보입니다. 아마도 checkValue 변수 값을 보내는 클래스의 값을 검사 할 수 있습니다. –