tips & tricks


Calendario
febbraio 2023
lmmgvsd
303112345
6789101112
13141516171819
20212223242526
272812345
6789101112

Verificare la firma in Java

import java.io.*;
import java.security.*;
import java.security.cert.*;
import java.security.spec.*;
import org.apache.commons.io.IOUtils;

import sun.misc.BASE64Decoder;



class VerSig {

    public static void main(String[] args) {

       
        try {
                
            String signedText = "SOME/CHALLENGE/GENERATED/BY/YOUR/SIT";
            
            String signature = "Ce3nAfKqekWzSbUaMcjQ1aFtRqZsvCUNs..........ucy81Pouq+NQplI5cZatJoyWYDmQ==";
            
            String sigAlg = "SHA256withRSA";
            
            String certificate ="MIIFwDCCA6igAwIBAgIII...........YBrEwsok/rEpmGViquYr9+dLP";
            
   
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] signatureByte = decoder.decodeBuffer(signature);
            
            
            byte[] certificateByte =decoder.decodeBuffer(certificate);
          
            //X509Certificate certUser= X509Certificate.getInstance(certificateByte);

            String certificatePath = "C:\\Users\\pippo\\Desktop\\test.cer";
            
    // convert String into InputStream
            InputStream inStream = new ByteArrayInputStream(certificateByte);
            //InputStream inStream = new FileInputStream(certificatePath);
            
            
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
            inStream.close();
            
            PublicKey pubKey = cert.getPublicKey();
           
            System.out.print(pubKey.getAlgorithm()) ;
            
            
            Signature signatureVerifier = Signature.getInstance("SHA256withRSA");//SHA1withRSA
            signatureVerifier.initVerify(pubKey);
            
            System.out.println(signatureVerifier.verify(signatureByte));
            
        
        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }
    }
}

Categoria: Java
mercoledì, 05 giu 2013 Ore. 10.04

Messaggi collegati


Archivio Posts
Anno 2020

Anno 2018

Anno 2015

Anno 2013

Anno 2011

Anno 2010

Anno 2009

Anno 2008
Statistiche
  • Views Home Page: 11.244
  • Views Posts: 55.108
  • Views Gallerie: 2.837
  • n° Posts: 120
  • n° Commenti: 1
Copyright © 2002-2007 - Blogs 2.0
dotNetHell.it | Home Page Blogs
ASP.NET 2.0 Windows 2003