I have an android program that writes data and signs ECDSA to NFC Tag, and another program that reads that data and verifies the signature from the NFC Tag.
I managed to write and sign the data to the NFC Tag, but whenever I try to verify the signature, the program always returns true
, even though I deliberately changed the signature.
Can someone tell me what's wrong?
Here is the code I used to sign:
public static String Generate(String x) throws Exception{
KeyPairGenerator kpg;
kpg = KeyPairGenerator.getInstance("EC","BC");
ECGenParameterSpec ecsp;
ecsp = new ECGenParameterSpec("prime192v1");
kpg.initialize(ecsp);
KeyPair kp = kpg.genKeyPair();
PrivateKey privKey = kp.getPrivate();
Signature ecdsaSign;
ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initSign(privKey);
byte[] baText = x.getBytes("UTF-8");
ecdsaSign.update(baText);
byte[] baSignature = ecdsaSign.sign();
String signature = (new BigInteger(1,baSignature).toString(16)).toUpperCase();
return signature;
}
And here is the code I used to verify:
public static boolean Verify(String x) throws Exception{
KeyPairGenerator kpg;
kpg = KeyPairGenerator.getInstance("EC","BC");
ECGenParameterSpec ecsp;
ecsp = new ECGenParameterSpec("prime192v1");
kpg.initialize(ecsp);
KeyPair kp = kpg.genKeyPair();
PrivateKey privKey = kp.getPrivate();
PublicKey pubKey = kp.getPublic();
Signature ecdsaSign;
ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initSign(privKey);
byte[] baText = x.getBytes("UTF-8");
ecdsaSign.update(baText);
byte[] baSignature = ecdsaSign.sign();
Signature ecdsaVerify;
ecdsaVerify = Signature.getInstance("SHA256withECDSA");
ecdsaVerify.initVerify(pubKey);
ecdsaVerify.update(baText);
boolean result = ecdsaVerify.verify(baSignature);
return result;
}