2

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?

1
  • By the way, the scheme you've implemented has little in common with digital signatures because you don't use any certificates. Just hashing your data (with SHA-1 or other algoritmh) will provide EXACTLY the same guarantees as you get with the current implementation. Commented Jul 7, 2012 at 19:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried printing the size of the mod , exp and signature they are same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.