6

RSA-SHA1 署名と .PFX 証明書の秘密鍵を使用して文字列に署名する必要があります。これが私のコードです:

String rawString = "1234567890";

byte[] signed = null;

FileInputStream cert = new FileInputStream("/sdcard/cert.pfx");

KeyStore keyStore = KeyStore.getInstance("pkcs12");
keyStore.load(cert, "cert_password".toCharArray());

String alias = keyStore.aliases().nextElement();
PrivateKey privateKey = (PrivateKey)keyStore.getKey(alias, "cert_password".toCharArray());

Signature instance = Signature.getInstance("SHA1withRSA");
instance.initSign((PrivateKey)privateKey);
instance.update(rawString.getBytes());
signed = instance.sign();

TextView mTextView = (TextView) findViewById(R.id.signed_message);
mTextView.setText(md5(bytes2String(signed)));

見栄えの良いMD5を取得しますが、PHPでも同じことを行っており、PHPで得られる結果はAndroidのものとは異なります。私はPHPが正しいことを知っています...では、Androidバージョンの何が問題になっていますか?

new String(signed)の代わりに使用した場合、bytes2String(signed)または使用した場合でも、Android の結果が異なることに気付きましたsigned.toString()

私はこれを MD5 に使用します: https://stackoverflow.com/a/4846511/1176497

および bytes2String from ( java.security.Signature と MessageDigest および Cipher で SHA1 および RSA を使用):

private static String bytes2String(byte[] bytes) {
    StringBuilder string = new StringBuilder();
    for (byte b : bytes) {
        String hexString = Integer.toHexString(0x00FF & b);
        string.append(hexString.length() == 1 ? "0" + hexString : hexString);
    }
    return string.toString();
}
4

1 に答える 1

1

I've figured it out....

the md5 function I'm using requires a string, but coverts it to byte[]... since i already have byte[] there's no need to covert it!

now i get the same result as in PHP :)

于 2013-06-11T07:14:06.167 に答える