对源内容及base64密文进行验签处理

@param content
@param base64Cipher
@param pubKey
@return
438059
public static boolean verify(String content, String base64Cipher, String pubKey) {
        return RSA.verify(content.getBytes(), Base64Util.decode(base64Cipher), decryptPublicKey(pubKey));
    }
784315
public static String encode(String content, String secret) {
        try {
            //1.构造密钥生成器，指定为AES算法,不区分大小写
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            //2.根据encodeRules规则初始化密钥生成器
            //生成一个256位的随机源,根据传入的字节数组
            keygen.init(256, new SecureRandom(secret.getBytes()));
            //3.产生原始对称密钥
            SecretKey original_key = keygen.generateKey();
            //4.获得原始对称密钥的字节数组
            byte[] raw = original_key.getEncoded();
            //5.根据字节数组生成AES密钥
            SecretKey key = new SecretKeySpec(raw, "AES");
            //6.根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance("AES");
            //7.初始化密码器，第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作，第二个参数为使用的KEY
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
            byte[] byte_encode = content.getBytes(StandardCharsets.UTF_8);
            //9.根据密码器的初始化方式--加密：将数据加密
            byte[] byte_AES = cipher.doFinal(byte_encode);
            //10.将加密后的数据转换为字符串
            return new BASE64Encoder().encode(byte_AES);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //如果有错就返加null
        return null;
    }
256192
public byte[] sign(byte[] content,PrivateKey privateKey)  
    {  
        try   
        {  
            java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);  
            signature.initSign(privateKey);  
            signature.update(content);  
            return signature.sign();  
        }catch (Exception e)   
        {  
            log.error(e.getMessage(),e);
        }  
        return null;  
    }
1014559
public String encryptBase64(String data, String charset) {
		return Base64.encode(encrypt(data, charset));
	}
146906
private static PublicKey commonGetPublickeyByText(String keyText)
            throws NoSuchAlgorithmException, InvalidKeySpecException {
        byte[] keyBytes = Base64.base64ToByteArray(keyText);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA);
        return keyFactory.generatePublic(x509KeySpec);
    }
1349789
public static byte[] AesDecryptByBase64Str(String base64String,byte[] decryptKey) throws Exception {
		byte[] content=Base64Decode(base64String);
		return AesDecryptByBytes(content, decryptKey);
	}
792030
static String toSignedBase64(SignableSAMLObject signableObj,
                                 Credential signingCredential,
                                 String signatureAlgorithm) {
        sign(signableObj, signingCredential, signatureAlgorithm);
        final String messageStr = nodeToString(serialize(signableObj));
        return Base64.getEncoder().encodeToString(messageStr.getBytes(StandardCharsets.UTF_8));
    }
776205
public static byte[] addUtf8Marker(byte[] content) {

        if ((content != null)
            && (content.length >= 3)
            && (content[0] == UTF8_MARKER[0])
            && (content[1] == UTF8_MARKER[1])
            && (content[2] == UTF8_MARKER[2])) {
            return content;
        }

        if (content == null) {
            content = new byte[0];
        }

        byte[] ret = new byte[UTF8_MARKER.length + content.length];

        System.arraycopy(UTF8_MARKER, 0, ret, 0, UTF8_MARKER.length);
        System.arraycopy(content, 0, ret, UTF8_MARKER.length, content.length);

        return ret;
    }
993775
public static String decryptByPrivateKey(String data, String privateKey, String fillMode) throws Exception {
		byte[] encryptedData = base64.decode(data);
		byte[] keyBytes = base64.decode(privateKey);
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
		// Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
		Cipher cipher = Cipher.getInstance(fillMode);

		cipher.init(Cipher.DECRYPT_MODE, privateK);
		int inputLen = encryptedData.length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offSet = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段解密
		while (inputLen - offSet > 0) {
			if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
				cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
			}
			out.write(cache, 0, cache.length);
			i++;
			offSet = i * MAX_DECRYPT_BLOCK;
		}
		byte[] decryptedData = out.toByteArray();
		out.close();
		return new String(decryptedData);
	}
662099
public static String encodeBase64(String content, Charset charset, TestContext context) {
        return new EncodeBase64Function().execute(Arrays.asList(content, charset.displayName()), context);
    }
557063
public static void decodeTo(String content, MultiMap map, String charset)
    {
        if (charset==null)
            charset=StringUtil.__ISO_8859_1;

        synchronized(map)
        {
            String key = null;
            String value = null;
            int mark=-1;
            boolean encoded=false;
            for (int i=0;i<content.length();i++)
            {
                char c = content.charAt(i);
                switch (c)
                {
                  case '&':
                      value = encoded
                          ?decodeString(content,mark+1,i-mark-1,charset)
                          :content.substring(mark+1,i);
                      
                      mark=i;
                      encoded=false;
                      if (key != null)
                      {
                          map.add(key,value);
                          key = null;
                      }
                      break;
                  case '=':
                      if (key!=null)
                          break;
                      key = encoded
                          ?decodeString(content,mark+1,i-mark-1,charset)
                          :content.substring(mark+1,i);
                      mark=i;
                      encoded=false;
                      break;
                  case '+':
                      encoded=true;
                      break;
                  case '%':
                      encoded=true;
                      break;
                }                
            }
            
            if (key != null)
            {
                value =  encoded
                    ?decodeString(content,mark+1,content.length()-mark-1,charset)
                    :content.substring(mark+1);
                map.add(key,value);
            }
            else if (mark<content.length())
            {
                key = encoded
                    ?decodeString(content,mark+1,content.length()-mark-1,charset)
                    :content.substring(mark+1);
                map.add(key,"");
            }
        }
    }
468263
public boolean signVerify(byte[] content,byte[] sign,PublicKey pubKey)  
    {  
        try   
        {  
            java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);  
            signature.initVerify(pubKey);  
            signature.update(content);  
            return signature.verify(sign);  
        }catch (Exception e)   
        {  
            log.error(e.getMessage(),e);
        }  
        return false;  
    }
746041
public static String encode(String content) {
		if (content == null || content.length() == 0) {
			return content;
		}
		return org.apache.commons.codec.binary.Base64.encodeBase64String(content.getBytes()).trim();

	}
925096
public static String decryptToBase64(String content) {
        byte[] decodedValue = DatatypeConverter.parseBase64Binary(content);
        return new String(decodedValue, CHARSET);
    }
741238
public static String encryptContent(String content, String encryptType, String encryptKey,
                                        String charset) throws AlipayApiException {

        if (AES_ALG.equals(encryptType)) {

            return aesEncrypt(content, encryptKey, charset);

        } else {

            throw new AlipayApiException("当前不支持该算法类型：encrypeType=" + encryptType);
        }

    }
12553
private static boolean verifySignature(String content, String secret, String signature) throws InvalidTokenException{
        try {
            SecretKey secretKey = new SecretKeySpec(secret.getBytes(), ALG_HMACSHA256);
            Mac mac = Mac.getInstance(ALG_HMACSHA256);
            mac.init(secretKey);
            byte[] bytes = mac.doFinal(content.getBytes());
            String base64 = base64UrlEncode(bytes);
            return  Strings.equals(base64,signature);
        } catch (Exception e) {
            throw new RuntimeException("Verify signature error", e);
        }
    }
436666
public static String base64Encode(byte[] bytes) {
    if (bytes == null) {
      throw new IllegalArgumentException("Input bytes must not be null.");
    }
    if (bytes.length >= BASE64_UPPER_BOUND) {
      throw new IllegalArgumentException(
          "Input bytes length must not exceed " + BASE64_UPPER_BOUND);
    }

    // Every three bytes is encoded into four characters.
    //
    // Example:
    //           input |0 1 0 1 0 0 1 0|0 1 1 0 1 1 1 1|0 1 1 0 0 0 1 0|
    // encode grouping |0 1 0 1 0 0|1 0 0 1 1 0|1 1 1 1 0 1|1 0 0 0 1 0|
    //   encoded ascii |     U     |     m     |     9     |     i     |

    int triples = bytes.length / 3;

    // If the number of input bytes is not a multiple of three, padding characters will be added.
    if (bytes.length % 3 != 0) {
      triples += 1;
    }

    // The encoded string will have four characters for every three bytes.
    char[] encoding = new char[triples << 2];

    for (int in = 0, out = 0; in < bytes.length; in += 3, out += 4) {
      int triple = (bytes[in] & 0xff) << 16;
      if (in + 1 < bytes.length) {
        triple |= ((bytes[in + 1] & 0xff) << 8);
      }
      if (in + 2 < bytes.length) {
        triple |= (bytes[in + 2] & 0xff);
      }
      encoding[out] = BASE64_CHARS.charAt((triple >> 18) & 0x3f);
      encoding[out + 1] = BASE64_CHARS.charAt((triple >> 12) & 0x3f);
      encoding[out + 2] = BASE64_CHARS.charAt((triple >> 6) & 0x3f);
      encoding[out + 3] = BASE64_CHARS.charAt(triple & 0x3f);
    }

    // Add padding characters if needed.
    for (int i = encoding.length - (triples * 3 - bytes.length); i < encoding.length; i++) {
      encoding[i] = '=';
    }

    return String.valueOf(encoding);
  }