zoukankan      html  css  js  c++  java
  • 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[]互转
  • 相关阅读:
    How to Create a site at the specified URL and new database (CommandLine Operation)
    Using Wppackager to Package and Deploy Web Parts for Microsoft SharePoint Products and Technologies
    SQL Server Monitor v0.5 [Free tool]
    How to build Web Part
    Deploy web part in a virtual server by developing a Web Part Package file(.cab)
    How to recreate "sites" link if you delete it accidentally
    SharePoint Portal Server管理匿名访问设置
    Monitor sql connection from .Net SqlClient Data Provider
    Brief installation instruction of Sharepoint Portal Server
    How to Use SharePoint Alternate URL Access
  • 原文地址:https://www.cnblogs.com/lene-y/p/11579997.html
Copyright © 2011-2022 走看看