JAVA银企直连建设银行云直连模式超详细讲解

建行银企直连

客户端加密方式:服务方使用 DES算法对密钥加密,使用DES/ECB/PKCS5Padding填充;

DES密钥约定为:企业客户号+交换当日日期(YYMMDD 6 位)

/**
	 * 初始化DES密钥(DES密钥约定为:企业客户号  + 交换当日日期-YYMMDD)
	 * @param customerNo 取后 10 位,不足 10 位的,前面补 0
	 * @return
	 * @throws Exception
	 */
	public static byte[] initDesKey(String customerNo) throws Exception{
		
		if(customerNo.length() >= 10){
			customerNo = customerNo.substring(customerNo.length() - 10);
		}else{
			customerNo = String.format("%0" + (10 - customerNo.length()) + "d", 0) + customerNo;
		}
		
        String sKey = customerNo + new SimpleDateFormat("yyMMdd").format(new Date());
        return asc2bin(sKey);
        
    }

数据签名

/**
     * 数字签名
     * @param data 待签名数据
     * @return 签名
     */
    public static String sign(String data,String rsaKey) throws Exception{
        
         byte[] keyBytes = Base64.decodeBase64(rsaKey);
         PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
         PrivateKey key = keyFactory.generatePrivate(keySpec);
         Signature signature = Signature.getInstance("MD5withRSA");
         signature.initSign(key);
         signature.update(data.getBytes("UTF-8"));
         return new String(Base64.encodeBase64(signature.sign()),"UTF-8");
         
    }

    报文加密

 /**
     * 报文加密 DESede (3DES)
     * @throws Exception 
     */
    public static String encryptWithDESede(String data,String desKey) throws Exception {
        
         byte[] sourceBytes = data.getBytes("UTF-8");
         byte[] keyBytes = Base64.decodeBase64(desKey);
         Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_XML);
         cipher.init(Cipher.ENCRYPT_MODE,new SecretKeySpec(keyBytes, KEY_ALGORITHM_XML));
         byte[] decrypted = cipher.doFinal(sourceBytes);
         return Base64.encodeBase64String(decrypted);
        
    }

    报文解密

/**
     * 报文解密 DESede (3DES)
     * @param encryptStr
     * @param key
     * @return
     * @throws Exception
     */
    public static String decryptWithDESede(byte[] data, String key) throws Exception {
        
        byte[] keyBytes = Base64.decodeBase64(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_XML);
        cipher.init(Cipher.DECRYPT_MODE,new SecretKeySpec(keyBytes, KEY_ALGORITHM_XML));
        byte[] decoded = cipher.doFinal(data);
        return new String(decoded, "UTF-8");
        
    }

    返回验签

 /**
     * 验签
     * @param sign
     * @param srcData
     * @param public_Key
     * @return
     * @throws Exception
     */
    public static boolean verify(String sign, String srcData ,String public_Key) throws Exception {
        
        byte[] keyBytes = Base64.decodeBase64(public_Key);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey key = keyFactory.generatePublic(keySpec);
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initVerify(key);
        signature.update(srcData.getBytes("UTF-8"));
        return signature.verify(Base64.decodeBase64(sign.getBytes("UTF-8")));
        
    }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咕噜轱辘2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值