2012-07-03 8 views
0

Signature.verify() 메서드는 Signature 클래스를 사용하여 서명을 확인하려고 시도 할 때 항상 false를 반환합니다. 내 코드는 다음과 같습니다. 당신이 SHA1withRSA와를 사용하는 경우Signature.verify()는 항상 false를 반환합니다.

package test; 

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; 

import java.io.UnsupportedEncodingException; 

import java.math.BigInteger; 

import java.security.*; 
import java.security.spec.InvalidKeySpecException; 
import java.security.spec.RSAPublicKeySpec; 

import java.util.logging.Level; 
import java.util.logging.Logger; 

/* this method converts the string into hash using SHA1 encryption algorithm */ 


public class SignatureTest { 

    static byte[] sha1(String input) throws NoSuchAlgorithmException { 
     MessageDigest mDigest = MessageDigest.getInstance("SHA1"); 
     byte[] result = mDigest.digest(input.getBytes()); 
     System.out.println("sha1 hashed data" + result); 
     return result; 

    } 

    public static void main(String[] args) throws InvalidKeySpecException { 
     try { 

      String dataString = "70:F3:95:11:14:B3" + "AIG_OCR" + "5"; 
      byte[] data = sha1(dataString); 
      System.out.println(dataString); 
      System.out.println("data::::::::::::" + data); 
      String data2 = Base64.encode(data); 
      System.out.println(data2); 

      /* here goes ur signed data */ 


      byte[] signedData = 
       Base64.decode("Q2BD1bPUKcs/iWpsBlvFtcJfrnzofucHIhc+m3qvKibLO13Z7FLSUQyOUUL9oh5KKpxiQY6b4mcLKYP2N9UPNZlCB6WBowJyDTEGajKNNyBgnn6nm7I/7X+eboQkEEqmSsGC0WA1XFXE+QQD6EXWzEkXzPornC3RnNJoR/fEIc8="); 
      System.out.println(" lenghtof signedData:::::" + 
           signedData.length); 


      /* here is the modulus reqired to generate the public key*/ 


      byte[] modulusBytes = 
       Base64.decode("pggfRN033/0G9HA5/x5v257h5Zdb13Lown8MTYDVF4ItZRuA6mC148omMvAdOO6wdaak/4atCvrIFrsNlFK8NSY8eb7WKN8WCdamXvHQ6G7FUDMOWs0WNokbhJRabUey/cXtG5b7ODTSr1VX2lV9uEfZEruw5YhmLXof39uftWc="); 

      /* here is the exponentBytes reqired to generate the public key*/ 


      byte[] exponentBytes = Base64.decode("AQAB"); 
      System.out.println(" lenghtof modulusBytes:::::" + 
           modulusBytes.length); 
      BigInteger modulus = new BigInteger(1, modulusBytes); 
      BigInteger exponent = new BigInteger(1, exponentBytes); 
      RSAPublicKeySpec rsaPubKey = 
       new RSAPublicKeySpec(modulus, exponent); 
      KeyFactory fact = KeyFactory.getInstance("RSA"); 
      PublicKey rsaPublicKey = fact.generatePublic(rsaPubKey); 
      Signature sig = Signature.getInstance("SHA1withRSA"); 
      sig.initVerify(rsaPublicKey); // initiate the signature with public key 
      sig.update(data); // update signature with the data that was signed by the card 
      Boolean flag = 
       sig.verify(signedData); // Test card signature - this always returns false 
      System.out.println("flag:::::::::::::" + flag); 
     } catch (SignatureException ex) { 
      Logger.getLogger(SignatureTest.class.getName()).log(Level.SEVERE, 
                   null, ex); 
     } catch (InvalidKeyException ex) { 

      Logger.getLogger(SignatureTest.class.getName()).log(Level.SEVERE, 
                   null, ex); 
     } catch (NoSuchAlgorithmException ex) { 
      Logger.getLogger(SignatureTest.class.getName()).log(Level.SEVERE, 
                   null, ex); 
     } catch (Exception ex) { 
      Logger.getLogger(SignatureTest.class.getName()).log(Level.SEVERE, 
                   null, ex); 
     } 
    } 
} 

답변

1

1) 해시를 계산 할 필요가 없습니다 (서명 엔진이
sig.update(data); 요구 사항이 자동으로) 라인을 sig.update(dataString.getBytes());

2) 서명 텍스트로되어 변경 될 수 있습니다 작은 이상한 "70 : F3 : 95 : 11 : 14 : B3"+ "AIG_OCR"+ "5", 당신은 바이트 시퀀스를 의미 했습니까 - 0x70,0xF3 ..?

관련 문제