Blame view

parking-module-pay/src/main/java/com/zteits/pay/util/SignUtils.java 1.03 KB
55f68655   王彪总   新项目
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  package com.zteits.pay.util;
  
  import java.security.KeyFactory;
  import java.security.PrivateKey;
  import java.security.spec.PKCS8EncodedKeySpec;
  
  public class SignUtils {
  
     private static final String ALGORITHM = "RSA";
  
     private static final String SIGN_ALGORITHMS = "SHA1WithRSA";
  
     private static final String DEFAULT_CHARSET = "UTF-8";
  
     public static String sign(String content, String privateKey) {
        try {
           PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(
                 Base64.decode(privateKey));
           KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
           PrivateKey priKey = keyf.generatePrivate(priPKCS8);
  
           java.security.Signature signature = java.security.Signature
                 .getInstance(SIGN_ALGORITHMS);
  
           signature.initSign(priKey);
           signature.update(content.getBytes(DEFAULT_CHARSET));
  
           byte[] signed = signature.sign();
  
           return Base64.encode(signed);
        } catch (Exception e) {
           e.printStackTrace();
        }
  
        return null;
     }
  
  }