1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public static boolean checkSign(String word1, String word2, String word3, String word4, String word5, String word6, String receivedSignature) {
// Add a Bouncy Castle security provider
Security.addProvider(new BouncyCastleProvider());
FileInputStream certifIS = new FileInputStream("myCert.cer");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Collection c = cf.generateCertificates(certifIS);
certifIS.close();
Iterator iterator = c.iterator();
X509Certificate cert = null;
while (iterator.hasNext()) {
cert = (X509Certificate)iterator.next();
}
MessageDigest sha = MessageDigest.getInstance("MD5");
sha.reset();
sha.update(word1.getBytes());
sha.update(word2.getBytes());
sha.update(word3.getBytes());
sha.update(word4.getBytes());
sha.update(word5.getBytes());
sha.update(word6.getBytes());
byte[] hash = sha.digest();
byte signedContent[] = Base64.decode(receivedSignature);
CMSSignedData signedData = new CMSSignedData(signedContent);
if (signedData.getSignedContent() == null){
CMSProcessableByteArray process = new CMSProcessableByteArray(hash);
signedData = new CMSSignedData(process, signedContent);
}
SignerInformation signer = (SignerInformation) signedData.getSignerInfos().getSigners().iterator().next();
boolean isOk = signer.verify(cert, "BC");
return isOk;
} |
Partager