public static void testVerifySignature(String inputStr){
try {
Map<String, Object> keyMap = RSAEncoder.initKey();
byte[] publicKey = RSAEncoder.getPublicKey(keyMap);
byte[] privateKey = RSAEncoder.getPrivateKey(keyMap);
String publicKeyX16=ByteUtil.byteToChar(publicKey);
String privateKeyX16=ByteUtil.byteToChar(privateKey);
System.out.println("------------------------------------------------------");
System.out.println("1:初始化RSA密钥对,16进制字符如下");
System.out.println("公钥: \n" + publicKeyX16);
System.out.println("私钥: \n" + privateKeyX16);
System.out.println("-------------------------------------------------------");
System.out.println("2:模拟建行进行数字签名");
byte[] inputData =Encoder.encryptMD5(inputStr.getBytes());
System.out.println("先对原文进行MD5加密,16进制字符如下:");
System.out.println(ByteUtil.byteToChar(inputData));
System.out.println("再对MD5加密后的数据进行签名,签名16进制字符如下:");
byte[] sign=RSAEncoder.sign(inputData, privateKey);
String signx16=ByteUtil.byteToChar(sign);
System.out.println(signx16);
System.out.println("-------------------------------------------------------");
System.out.println("3:校验数字签名");
boolean status = CCBBankService.verifySignature(signx16, inputStr, publicKeyX16);
System.out.println("状态:" + status);
System.out.println("--------------------------------------------------------");
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
String s="woainizhongguo";
testVerifySignature(s);
}
}
执行main方法,输出结果如下
------------------------------------------------------------
1:初始化RSA密钥对,16进制字符如下
公钥:
30819f300d06092a864886f70d010101050003818d003081890281810087b3310ad
822eb5a4f0e3be938904583b6e71415ee9f5edbcce85285e45ba24809fb8c2a4c3d
e212917fb1988789383e96d6005bed04efb6f0b8090775e3d4ccedf98d2b6de19fae
766c6edac1bddd33c64d0c74358cf1398127d0cb0c3c0d6ef0b4c4267eb5088a322
2e49fe5f0b41aecb85bafafca6be5b22491e0b00cb4810203010001
私钥:
30820276020100300d06092a864886f70d0101010500048202603082025c0201000
281810087b3310ad822eb5a4f0e3be938904583b6e71415ee9f5edbcce85285e45b
a24809fb8c2a4c3de212917fb1988789383e96d6005bed04efb6f0b8090775e3d4cc
edf98d2b6de19fae766c6edac1bddd33c64d0c74358cf1398127d0cb0c3c0d6ef0b4
c4267eb5088a3222e49fe5f0b41aecb85bafafca6be5b22491e0b00cb48102030100
01028180537f11ad0f82a4f8036ec9b45e267f6f652a58348a882b70e96b05e3f6b7d
f699e22d682ec38a393334db3f41b69765f24a676c0195e1135aa7642b0017cf7615
fa548f7abf1326db140e5c1f221a4c6b180e6d48e4245e15c07ab565d91a17fa1cba4
a49e9a634d9b3150aa5e6a560499cf8127cd7897f2635455dbd7d97cc1024100bcb
c75f7a4e38a5ac457b4453f3d8c948f58ceede3800282800d6918d20714696aae9ba
72face378be705e92425ed8b91858053e5d552f44475f534c4b388fb9024100b80fea
f0fe75b1076a404f8818a4d9c562cc7f4279718c35724376dcc32af11a7a50d9d5164
542f9c1a9acc2f7e42e35daed5e360fddeb6bf2ed41e1348a5f09024100bc1694f9d7
396c0848e39ea3e40252b73aa42694acc445bc0821553b8713c723c74d7b9181d1a
a600a69b1d5d08ef52c813637038fbff4faf6a5e93d7f393f0102402967f34b49830253
0fb4edef48d3f4e571875d6d7fd26a5f2f983baa0801a60175d8b0583e4d5076ccf935
931a9eeb425440a7bbf3afd1a11422097af70dd5b90240587bfd335fa2ba4103e02aa
994f9536c1f298fd4b3a10a3a9b3ab5a7a5ab64b76e8c3b2e150c8533e0d0c536195
351559b3bf552bd1241ee25eff8bdac50deb4
------------------------------------------------------------
2:模拟建行进行数字签名
先对原文进行MD5加密,16进制字符如下:
7a8ebba1779e3fcdba6a4274911f48c0
再对MD5加密后的数据进行签名,签名16进制字符如下:
0ce488500271a3d8e56e893ffc0b16ba9f880c9250469c2c7a3f6ceb0532edef0322f4
f5b1010a44f89c2645db7fd58560c0f175a5c7ed7e859ca48097f81e05d2c8b16d9e9c
8bb827a604c23aa4ac1519eaefa3a5d730501b056e9b2b7fd2718c796c42263c7dbf4
ae164035561b288311ceb71a203c699ef5af1f8e8d3c5dd
------------------------------------------------------------
3:校验数字签名
状态:true
------------------------------------------------------------