相关文章推荐
3 try { 4 byte [] data = Data.getBytes(); 5 byte [] keyBytes = base64String2Byte(PrivateKey); 7 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); 9 KeyFactory keyFactory = KeyFactory.getInstance("RSA" ); 11 PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); 13 Signature signature = Signature.getInstance("SHA256withRSA" );//这个根据需求填充SHA1WithRSA或SHA256WithRSA 14 signature.initSign(priKey); 15 signature.update(data); 17 return byte2Base64String(signature.sign()); 18 } catch (Exception e) { 19 return "" ; 20 }
1 // base64字符串转字节数组
2   public static byte[] base64String2Byte(String base64Str) {
3     return Base64.decodeBase64(base64Str);

RSA1、RSA256 验签

 1     public static boolean VeriSign(String Data_ori, String Singnature) {
 2       try {
 3         byte[] signed = base64String2Byte(Singnature);
 5         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(base64String2Byte(PublicKey));
 6         KeyFactory keyFactory = null;
 7         keyFactory = KeyFactory.getInstance("RSA");
 8         PublicKey publicKey = keyFactory.generatePublic(keySpec);
10         Signature signature2 = Signature.getInstance("Sha256WithRSA");//这个根据需求填充SHA1WithRSA或SHA256WithRSA
11         signature2.initVerify(publicKey);
12         signature2.update(Data_ori.getBytes("UTF-8"));
13         boolean verify = signature2.verify(signed);
14         return verify;
15       } catch (Exception e) {
16         return false;
17       }

SHA256withECDSA验签

 1  public static boolean verifySign(String _data, String _key, String _sign) {
 3         try {
 4             java.security.spec.X509EncodedKeySpec bobPubKeySpec = new java.security.spec.X509EncodedKeySpec(
 5                     new BASE64Decoder().decodeBuffer(_key));
 6             KeyFactory keyf = KeyFactory.getInstance("EC"); //ECC 可根据需求更改
 7             PublicKey publicKey = keyf.generatePublic(bobPubKeySpec);
 9             byte[] data = hexStringToBytes(_data);
10             byte[] sig = hexStringToBytes(_sign);
12             Signature signer = Signature.getInstance("SHA256withECDSA");
13             signer.initVerify(publicKey);
14             signer.update(data);
15             return (signer.verify(sig));
16         }
17         catch(Exception ex)
18         {
19             System.out.println(ex.getMessage());
20             return false;
 1  public static String bytesToHexString(byte[] src) {
 2         StringBuilder stringBuilder = new StringBuilder("");
 3         if (src == null || src.length <= 0) {
 4             return null;
 6         for (int i = 0; i < src.length; i++) {
 7             int v = src[i] & 0xFF;
 8             String hv = Integer.toHexString(v);
 9             if (hv.length() < 2) {
10                 stringBuilder.append(0);
11             }
12             stringBuilder.append(hv);
13         }
14         return stringBuilder.toString();
15     }
17     public static byte[] hexStringToBytes(String hexString) {
18         if (hexString == null || hexString.equals("")) {
19             return new byte[0];
20         }
21         hexString = hexString.toUpperCase();
22         int length = hexString.length() / 2;
23         char[] hexChars = hexString.toCharArray();
24         byte[] d = new byte[length];
25         for (int i = 0; i < length; i++) {
26             int pos = i * 2;
27             d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
28         }
29         return d;
30     }
32     public static byte charToByte(char c) {
33         return (byte) "0123456789ABCDEF".indexOf(c);
HEX和Byte[]互转