Signing data in Android and Verifying it in python
Question: I have written the following code to Sign data in android:

    
    
    import java.security.KeyFactory;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.Signature;
    import java.security.spec.RSAPublicKeySpec;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class TestActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
    
            try{
            String m ="This is my message";
            System.out.println(m);
    
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
            keyPairGen.initialize(1024);
            KeyPair kp = keyPairGen.generateKeyPair();
            PrivateKey priKey = kp.getPrivate();
            PublicKey pubKey = kp.getPublic();
    
    
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            RSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(pubKey, RSAPublicKeySpec.class);
    
            System.out.println("WITH toString: ");
            System.out.println("Mod :" + publicKeySpec.getModulus().toString());
            System.out.println("Exp :" + publicKeySpec.getPublicExponent().toString());
            System.out.println("PublicKey:" + pubKey.toString());
    
    
    
    
            System.out.println("PublicKey:" + pubKey);
            System.out.println("PublicKey Base64:" +MyBase64.encode(pubKey.getEncoded()));
    
            Signature instance = Signature.getInstance("SHA1withRSA");
            instance.initSign(priKey);
            instance.update(m.getBytes());
            byte[] signature = instance.sign();
            System.out.println("Signature: " + MyBase64.encode(signature));
            }catch(Exception e){
                e.printStackTrace();
            }
    
        }
    }
    

I am copy pasting values from adb logcat into python and I am verifying it in
python using:

    
    
    mod=#I paste mod here
    exp=#I paste exp here
    signature=#I paste signature here
    
    message="This is my message"
    
    publicKey = RSA.construct((mod,exp))
    print 'PublicKey Base64: ' + publicKey.exportKey()
    print str(publicKey)
    test = SHA.new(message)
    verifier = PKCS1_v1_5.new(publicKey)
    signature_base = base64.b64decode(signature)
    print "Verification: " + str(verifier.verify(test, signature_base))
    

I find that MyBase64.encode(pubKey.getEncoded()) (in java) is same as
publicKey.exportKey() (in python)

However, the verification always results to false.

The java code seems to work fine , if I run it using javac .

Any help, what may be going wrong?


Answer: Often an error like this has to do with extra whitespace sneaking into the
data string. Try stripping it before computing the hashes.

