private static byte[] sign() throws Exception { RSAEngine rsa = new RSAEngine(); Digest dig = new SHA1Digest(); RSAPrivateKey privateKey = (RSAPrivateKey) getPrivate(privateKeyFilename); BigInteger big = ((RSAKey) privateKey).getModulus(); ISO9796d2Signer eng = new ISO9796d2Signer(rsa, dig, true); RSAKeyParameters rsaPriv = new RSAKeyParameters(true, big, privateKey.getPrivateExponent()); eng.init(true, rsaPriv); eng.update(message[0]); eng.update(message, 1, message.length - 1); byte[] signature = eng.generateSignature(); return signature; }
private static String verify() throws Exception { RSAEngine engine = new RSAEngine(); Digest digest = new SHA1Digest(); RSAPublicKey publicKey = (RSAPublicKey) getPublic(publicKeyFilename); BigInteger big = ((RSAKey) publicKey).getModulus(); RSAKeyParameters rsaPublic = new RSAKeyParameters(false, big, publicKey.getPublicExponent()); ISO9796d2Signer verifier = new ISO9796d2Signer(engine, digest, true); verifier.init(false, rsaPublic); // false for verify if (!verifier.verifySignature(signature)) { System.err.println("Signature was modified, could not verify correctness!"); return ""; } String recoveredMessage = ""; try { if (verifier.hasFullMessage()) { verifier.updateWithRecoveredMessage(signature); } byte[] message = verifier.getRecoveredMessage(); recoveredMessage = new String(message, "UTF-8"); } catch (Exception exception) { System.err.println("Recover failed!"); } return recoveredMessage; }
protected ISOSignatureSpi( Digest digest, AsymmetricBlockCipher cipher) { super(digest.getAlgorithmName() + "withRSA/ISO9796-2"); signer = new ISO9796d2Signer(cipher, digest, true); }
protected ISOSignatureSpi( Digest digest, AsymmetricBlockCipher cipher) { signer = new ISO9796d2Signer(cipher, digest, true); }
public void doTest12() throws Exception { BigInteger mod = new BigInteger("B3ABE6D91A4020920F8B3847764ECB34C4EB64151A96FDE7B614DC986C810FF2FD73575BDF8532C06004C8B4C8B64F700A50AEC68C0701ED10E8D211A4EA554D", 16); BigInteger pubExp = new BigInteger("65537", 10); BigInteger priExp = new BigInteger("AEE76AE4716F77C5782838F328327012C097BD67E5E892E75C1356E372CCF8EE1AA2D2CBDFB4DA19F703743F7C0BA42B2D69202BA7338C294D1F8B6A5771FF41", 16); RSAKeyParameters pubParameters = new RSAKeyParameters(false, mod, pubExp); RSAKeyParameters privParameters = new RSAKeyParameters(true, mod, priExp); RSAEngine rsa = new RSAEngine(); byte[] data; byte[] m1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}; byte[] m2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; byte[] m3 = {1, 2, 3, 4, 5, 6, 7, 8}; // // ISO 9796-2 - Signing // Digest dig = new SHA1Digest(); ISO9796d2Signer eng = new ISO9796d2Signer(rsa, dig); // // check message bounds // eng.init(true, privParameters); eng.update(m1, 0, m1.length); data = eng.generateSignature(); eng.init(false, pubParameters); eng.update(m2, 0, m2.length); if (eng.verifySignature(data)) { fail("failed ISO9796-2 m2 verify Test 12"); } eng.init(false, pubParameters); eng.update(m3, 0, m3.length); if (eng.verifySignature(data)) { fail("failed ISO9796-2 m3 verify Test 12"); } eng.init(false, pubParameters); eng.update(m1, 0, m1.length); if (!eng.verifySignature(data)) { fail("failed ISO9796-2 verify Test 12"); } }