Giz | Bon voilà, je n'arrive à vérifier une signature. Le programme affiche toujours "false", pourquoi ? ... bien qu'ayant parcouru un peu tout (livre, google, forum), j'ai du rater une étape laquelle ?
Code :
- package sign;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.security.KeyStore;
- import java.security.PrivateKey;
- import java.security.Provider;
- import java.security.Security;
- import java.security.Signature;
- import java.security.cert.Certificate;
- import org.bouncycastle.jce.provider.BouncyCastleProvider;
- public class SignTest {
- private static byte[] sign(byte[] dataToSign) {
- File certificat = new File("d:\\txt\\gadmin.p12" );
- String password = "gadmin";
- // provider cryptographic algorithm implementation
- Security.addProvider(new BouncyCastleProvider());
- Provider provBC = Security.getProvider("BC" );
- try {
- KeyStore keyStore = KeyStore.getInstance("PKCS12" );
- keyStore.load(new FileInputStream(certificat), password
- .toCharArray());
- PrivateKey privatekey = (PrivateKey) keyStore.getKey(
- (String) keyStore.aliases().nextElement(), password
- .toCharArray());
- Signature sigInst = Signature.getInstance("MD5withRSA", provBC);
- sigInst.initSign(privatekey);
- sigInst.update(dataToSign);
- return sigInst.sign();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- private static boolean verify(byte[] sigToVerify) {
- File certificat = new File("d:\\txt\\gadmin.p12" );
- String password = "gadmin";
- // provider cryptographic algorithm implementation
- Security.addProvider(new BouncyCastleProvider());
- Provider provBC = Security.getProvider("BC" );
- try {
- KeyStore keyStore = KeyStore.getInstance("PKCS12" );
- keyStore.load(new FileInputStream(certificat), password
- .toCharArray());
- Certificate cert = keyStore.getCertificate((String) keyStore
- .aliases().nextElement());
- Signature sigInst = Signature.getInstance("MD5withRSA", provBC);
- sigInst.initVerify(cert.getPublicKey());
- sigInst.update(sigToVerify);
- return sigInst.verify(sigToVerify);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- public static void main(String args[]) {
- // load zip file to sign
- File inputZipDataFile = new File("d:\\test.zip" );
- byte[] dataToSign = new byte[(int) inputZipDataFile.length()];
- DataInputStream in;
- try {
- in = new DataInputStream(new FileInputStream(inputZipDataFile));
- in.readFully(dataToSign);
- in.close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- byte[] signature = sign(dataToSign);
- boolean verification = verify(signature);
- System.out.println(verification);
- }
- }
|
---------------
Asus P5Q Pro | C2D E8400 3GHz@4GHz + Noctua NH-C12P | 2x2Go Patriot Extreme PC-8500 | GeForce GTX 460@Stock 1Go GLH | Crucial SSD M4 64Go Sata3
|