tips & tricks


Calendario
ottobre 2024
lmmgvsd
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

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: 14.502
  • Views Posts: 57.378
  • Views Gallerie: 2.837
  • n° Posts: 117
  • n° Commenti: 1
Copyright © 2002-2007 - Blogs 2.0
dotNetHell.it | Home Page Blogs
ASP.NET 2.0 Windows 2003