Java 签名(SHA1WithRSA、SHA256WithRSA、SHA256…

2019-09-30 06:50:45来源:博客园 阅读 ()

Java 签名(SHA1WithRSA、SHA256WithRSA、SHA256withECDSA)

RSA1、RSA256 签名

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

 

RSA1、RSA256 验签

 1     public static boolean VeriSign(String Data_ori, String Singnature) {
 2       try {
 3         byte[] signed = base64String2Byte(Singnature);
 4       
 5         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(base64String2Byte(PublicKey));
 6         KeyFactory keyFactory = null;
 7         keyFactory = KeyFactory.getInstance("RSA");
 8         PublicKey publicKey = keyFactory.generatePublic(keySpec);
 9 
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       }
18   }

 

SHA256withECDSA验签

 1  public static boolean verifySign(String _data, String _key, String _sign) {
 2 
 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);
 8 
 9             byte[] data = hexStringToBytes(_data);
10             byte[] sig = hexStringToBytes(_sign);
11 
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;
21         }

 

 1  public static String bytesToHexString(byte[] src) {
 2         StringBuilder stringBuilder = new StringBuilder("");
 3         if (src == null || src.length <= 0) {
 4             return null;
 5         }
 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     }
16 
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     }
31 
32     public static byte charToByte(char c) {
33         return (byte) "0123456789ABCDEF".indexOf(c);
34     }
HEX和Byte[]互转

原文链接:https://www.cnblogs.com/lene-y/p/11579997.html
如有疑问请与原作者联系

标签:QtheAVPSloadsys

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:1.配置数据库开发环境

下一篇:Java13新特性