android rsa签名验证,无法验证Android上的rsa签名

签名验证失败,因为您在验证方法中使用了不同的public key。 使用public key验证与用于rsaSign()方法的private key一致的签名。

希望这会帮助你。需要注意的是,这是public key与在签名生成方法所使用的private key一致:

/**

* This method will sign message with RSA 2048 key

* @return Void

*/

public void rsaSign (String message) throws Exception {

//key generation

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

keyGen.initialize(2048, random);

KeyPair keyPair = keyGen.generateKeyPair();

PrivateKey priv = keyPair.getPrivate();

PublicKey pub = keyPair.getPublic();

System.out.println("RSAPub key Mod for Sign/Verify : " + Helper.toHex(((RSAPublicKey)pub).getModulus().toByteArray()));

System.out.println("RSAPub key Exp for Sign/Verify : " + Helper.toHex(((RSAPublicKey)pub).getPublicExponent().toByteArray()));

//sign

Signature dsa = Signature.getInstance(signALG);

dsa.initSign(priv);

dsa.update(Helper.toByte(message));

byte[] realSig = dsa.sign();

System.out.println("RSA Sign-Data : " + Helper.toHex(realSig));

}

/**

* This method verify signature with RSA public key

* @param message The plain message

* @param rsaMOD RSA Public key Modulus in string

* @param rsaEXP RSA Public key Exponent in string

* @param rsaSignData Signature which will be verified

* @return true if verifications success, false otherwise

*/

public boolean rsaVerify(String message, String rsaMOD, String rsaEXP, String rsaSignData) throws Exception {

BigInteger modBigInteger = new BigInteger(Helper.toByte(rsaMOD));

BigInteger exBigInteger = new BigInteger(Helper.toByte(rsaEXP));

RSAPublicKeySpec spec = new RSAPublicKeySpec(modBigInteger, exBigInteger);

KeyFactory factory = KeyFactory.getInstance("RSA");

PublicKey publicKey = factory.generatePublic(spec);

Signature signature = Signature.getInstance(signALG);

signature.initVerify(publicKey);

signature.update(Helper.toByte(message));

return signature.verify(Helper.toByte(rsaSignData));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值