buc算法java实现_银行业加密算法,CVV算法(java-标密)-仅供参考

public class CVV {

/**

* 计算cvv

*

* @param pan

* @param expireDate

* @param serviceCode

* @param cvk

* @return

* @throws InvalidKeyException

* @throws NoSuchAlgorithmException

* @throws NoSuchProviderException

* @throws NoSuchPaddingException

* @throws ShortBufferException

* @throws IllegalBlockSizeException

* @throws BadPaddingException

* @throws InvalidAlgorithmParameterException

* @throws InvalidKeySpecException

* @throws SecurityException

*/

public static String calculateCVV(String pan, String expireDate, String serviceCode, String cvk)

throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,

ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException,

InvalidKeySpecException, SecurityException {

if (expireDate.length() != 4) {

throw new SecurityException("bad expire date");

}

if (serviceCode.length() != 3) {

throw new SecurityException("bad service code");

}

if (cvk.length() != 32) {

throw new SecurityException("bad service cvk");

}

byte[] src = Convert.hexToByte(String.format("%-32s", pan + expireDate + serviceCode).replace(' ', '0'));

byte[] leftSrc = new byte[8];

byte[] rightSrc = new byte[8];

System.arraycopy(src, 0, leftSrc, 0, 8);

System.arraycopy(src, 8, rightSrc, 0, 8);

byte[] cvkByte = Convert.hexToByte(cvk);

byte[] keyLeft = new byte[8];

byte[] keyRight = new byte[8];

System.arraycopy(cvkByte, 0, keyLeft, 0, 8);

System.arraycopy(cvkByte, 8, keyRight, 0, 8);

byte[] e1 = DES.encryptECB(leftSrc, keyLeft);

e1[0] = (byte) (e1[0] ^ rightSrc[0]);

e1[1] = (byte) (e1[1] ^ rightSrc[1]);

e1[2] = (byte) (e1[2] ^ rightSrc[2]);

e1[3] = (byte) (e1[3] ^ rightSrc[3]);

e1[4] = (byte) (e1[4] ^ rightSrc[4]);

e1[5] = (byte) (e1[5] ^ rightSrc[5]);

e1[6] = (byte) (e1[6] ^ rightSrc[6]);

e1[7] = (byte) (e1[7] ^ rightSrc[7]);

byte[] e2 = DES.encryptECB(e1, keyLeft);

byte[] e3 = DES.decryptECB(e2, keyRight);

byte[] e4 = DES.encryptECB(e3, keyLeft);

StringBuilder num = new StringBuilder("");

StringBuilder bcd = new StringBuilder("");

for (int i = 0; i < e4.length; i++) {

int b = e4[i] & 0xFF;

int up = b >> 4;

if (up >= 10) {

bcd.append(up - 10);

} else {

num.append(up);

}

int low = b & 0xF;

if (low >= 10) {

bcd.append(low - 10);

} else {

num.append(low);

}

}

String cvv = num.append(bcd.toString()).toString().substring(0, 3);

return cvv;

}

/**

* 校验cvv

*

* @param pan

* @param expireDate

* @param serviceCode

* @param cvv

* @param cvk

* @return

* @throws InvalidKeyException

* @throws NoSuchAlgorithmException

* @throws NoSuchProviderException

* @throws NoSuchPaddingException

* @throws ShortBufferException

* @throws IllegalBlockSizeException

* @throws BadPaddingException

* @throws InvalidAlgorithmParameterException

* @throws InvalidKeySpecException

* @throws SecurityException

*/

public static boolean verifyCVV(String pan, String expireDate, String serviceCode, String cvv, String cvk)

throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,

ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException,

InvalidKeySpecException, SecurityException {

String cvvLocal = calculateCVV(pan, expireDate, serviceCode, cvk);

return cvv.equals(cvvLocal);

}

public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException,

NoSuchProviderException, NoSuchPaddingException, ShortBufferException, IllegalBlockSizeException,

BadPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException, SecurityException {

String pan = "4123456789012";

String expirDate = "8701";

String serviceCode = "101";

String key = "0123456789ABCDEFFEDCBA9876543210";

String cvv = calculateCVV(pan, expirDate, serviceCode, key);

System.out.println(cvv);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值