有人可以给我一个关于如何使用 javacard 签名并验证它的小建议吗?我想我在签名生成和验证方面做错了:
...
SignatureMessageRecovery sig;
...
private Cardlet(byte[] bArray, short bOffset, byte bLength) {
/* Generate our RSA key */
keyPair = new KeyPair(KeyPair.ALG_RSA, (short) 512);
keyPair.genKeyPair();
/* signature buffer is 64 + 2 (offset + length) */
sigBuff = JCSystem.makeTransientByteArray((short) (SIG_LENGTH + 2), JCSystem.CLEAR_ON_DESELECT);
sig = (SignatureMessageRecovery) Signature.getInstance(Signature.ALG_RSA_SHA_ISO9796_MR, false);
}
...
private void insCryptoSigne(APDU apdu) {
byte[] buffer = apdu.getBuffer();
short bytesRead = apdu.setIncomingAndReceive();
short[] m1Data = JCSystem.makeTransientShortArray((short) 1, JCSystem.CLEAR_ON_DESELECT);
sigLen = sig.sign(buffer, ISO7816.OFFSET_CDATA, bytesRead, sigBuff, (short) 0, m1Data, (short) 0);
// set m1Length into sigBuff array
sigBuff[sigLen] = (byte) ((short) (m1Data[(short) 0] & ((short) 0xFF00)) >> ((short) 8));
sigBuff[(short) (sigLen + 1)] = (byte) (m1Data[(short) 0] & ((short) 0x00FF));
apdu.setOutgoing();
apdu.setOutgoingLength((short) (sigLen + 2));// The extra 2 bytes for
apdu.sendBytesLong(sigBuff, (short) 0, (short) (sigLen + 2));
}
在 Java 方面 ,我已经将公钥作为 X509Certificate 保存在本地文件中。很确定它们与证书和卡中的模数和指数相同。
FileInputStream certis = new FileInputStream(cert);
X509Certificate c1 = new X509Certificate(certis);
...
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] testsig = new byte[64];
System.arraycopy(raw_signature,0, testsig, 0, 64);
Signature signature = Signature.getInstance("SHA1withRSA", "BC");
signature.initVerify(c1.getPublicKey());
System.out.println(c1.getPublicKey());
System.out.println(signature);
System.out.println(file_data.length+":> "+new String(file_data));
signature.update(file_data);
System.out.println("VERIFY > "+signature.verify(testsig)+" <");
结果是假的:(
感谢和问候图里