I'm trying to sign an encrypted message with a private key and verify it in Java. This is my first time working with encryption and signatures so I'm not sure how it is supposed to work and I'm kind of stuck here. The verification always returns false.
I am posting the code here with the most important parts included:
import android.util.Base64;
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
public class SignatureTest {
// testing signature
PublicKey javaPublicKey;
PrivateKey javaPrivateKey;
String message = "Hello world";
public void test() {
try {
GenerateKeys();
byte[] messageBytes = message.getBytes("UTF-8");
byte[] signature = rsaSign(messageBytes);
boolean success = rsaVerify(messageBytes, signature);
if(success){
Log.e("yay", "yay");
}
else {
Log.e("nay", "nay");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void GenerateKeys() {
SecureRandom random = null;
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
javaPrivateKey = pair.getPrivate();
javaPublicKey = pair.getPublic();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public byte[] rsaSign (byte[] data) {
byte[] cipherData = null;
try {
Signature s = Signature.getInstance("SHA1withRSA");
s.initSign(javaPrivateKey);
s.update(data);
Log.e("s.sign()", Base64.encodeToString(s.sign(), Base64.DEFAULT));
return s.sign();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (SignatureException e) {
e.printStackTrace();
}
return cipherData;
}
public boolean rsaVerify (byte[] data, byte[] signature) {
boolean success = false;
try {
Signature s = Signature.getInstance("SHA1withRSA");
s.initVerify(javaPublicKey);
s.update(data);
success = s.verify(Base64.decode(signature, Base64.DEFAULT));
if(success == true) {
Log.i("yeay", "yay");
}
else {
Log.i("nay", "nay");
}
return success;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (SignatureException e) {
e.printStackTrace();
}
return false;
}
}
Can anyone tell me what I'm doing wrong here?