已阅读5页,还剩24页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/* * (#)MacUtil.java 1.0 2013-3-28 * * Copyright (c) 2009 Sunyard System Engineering Co., Ltd. * All rights reserved. * * This software is the confidential and proprietary information of * Sunyard System Engineering Co., Ltd. (Confidential Information). * You shall not disclose such Confidential Information and shall use it * only in accordance with the terms of the license agreement you entered * into with Sunyard. */ /* Modification History: * Date Author Version Description * - * */ package com.sunyard.etp.ag.util;import java.io.UnsupportedEncodingException;import java.util.Random;/* * * * version 1.0 2013-3-28 * author openzhang */public class MacUtil private static final int s1 = 14,4, 13,1, 2, 15,11,8, 3, 10,6, 12,5, 9, 0, 7,0, 15,7, 4, 14,2, 13,1, 10,6, 12,11,9, 5, 3, 8,4, 1, 14,8, 13,6, 2, 11,15,12,9, 7, 3, 10,5, 0,15,12,8, 2, 4, 9, 1, 7, 5, 11,3, 14,10,0, 6, 13 ;private static final int s2 = 15,1, 8, 14,6, 11,3, 4, 9, 7, 2, 13,12,0, 5, 10,3, 13,4, 7, 15,2, 8, 14,12,0, 1, 10,6, 9, 11,5,0, 14,7, 11,10,4, 13,1, 5, 8, 12,6, 9, 3, 2, 15,13,8, 10,1, 3, 15,4, 2, 11,6, 7, 12,0, 5, 14,9;private static final int s3 = 10,0, 9, 14,6, 3, 15,5, 1, 13,12,7, 11,4, 2, 8, 13,7, 0, 9, 3, 4, 6, 10,2, 8, 5, 14,12,11,15,1, 13,6, 4, 9, 8, 15,3, 0, 11,1, 2, 12,5, 10,14,7, 1, 10,13,0, 6, 9, 8, 7, 4, 15,14,3, 11,5, 2, 12;private static final int s4 = 7, 13,14,3, 0, 6, 9, 10,1, 2, 8, 5, 11,12,4, 15, 13,8, 11,5, 6, 15,0, 3, 4, 7, 2, 12,1, 10,14,9, 10,6, 9, 0, 12,11,7, 13,15,1, 3, 14,5, 2, 8, 4, 3, 15,0, 6, 10,1, 13,8, 9, 4, 5, 11,12,7, 2, 14;private static final int s5 = 2, 12,4, 1, 7, 10,11,6, 8, 5, 3, 15,13,0, 14,9, 14,11,2, 12,4, 7, 13,1, 5, 0, 15,10,3, 9, 8, 6, 4, 2, 1, 11,10,13,7, 8, 15,9, 12,5, 6, 3, 0, 14, 11,8, 12,7, 1, 14,2, 13,6, 15,0, 9, 10,4, 5, 3;private static final int s6 = 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13;private static final int s7 =4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12;private static final int s8 = 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11;private static final int ip = 58, 50, 42, 34, 26, 18, 10, 2,60, 52, 44, 36, 28, 20, 12, 4,62, 54, 46, 38, 30, 22, 14, 6,64, 56, 48, 40, 32, 24, 16, 8,57, 49, 41, 33, 25, 17, 9, 1,59, 51, 43, 35, 27, 19, 11, 3,61, 53, 45, 37, 29, 21, 13, 5,63, 55, 47, 39, 31, 23, 15, 7;private static final int _ip = 40, 8, 48, 16, 56, 24, 64, 32,39, 7, 47, 15, 55, 23, 63, 31,38, 6, 46, 14, 54, 22, 62, 30,37, 5, 45, 13, 53, 21, 61, 29,36, 4, 44, 12, 52, 20, 60, 28,35, 3, 43, 11, 51, 19, 59, 27,34, 2, 42, 10, 50, 18, 58, 26,33, 1, 41, 9, 49, 17, 57, 25;private static final int LS = 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1; private static int subKey = new int1648;private static int HEX = 0;private static int ASC = 1;/* * 将16进制字符转换成对应的int类型值:如字符A被转换后为10;B将被转换为11 * param ch 需要被转换的目标字符 * return转换后的int类型值 * throws Exception 如果传入字符为非16进制字符,将会报getIntByChar was wrong自定义异常 */public static int getIntByChar(char ch) throws Exceptionchar t = Character.toUpperCase(ch);int i = 0;switch(t)case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:i = Integer.parseInt(Character.toString(t);break;case A:i = 10;break;case B:i = 11;break;case C:i = 12;break;case D:i = 13;break;case E:i = 14;break;case F:i = 15;break;default:throw new Exception(getIntByChar was wrong);return i;/* * 将16进制表示的字符串转换为0,1组成的int数组 * 列如:ABC 被转换后的结果为1,0,1,0,1,0,1,1,1,1,0,0 * param source 十六进制字符串 * return 0,1组成的int数组 */public static int string2Binary(String source)int len = source.length();int dest = new intlen*4;char arr = source.toCharArray();for(int i=0; i0; j-)destk = Integer.parseInt(strj);k-;return dest;/* * 完成对底数X的Y次方操作,如输入值分别为:2和4 返回值为16 * param x 执行幂操作的底数 * param y执行幂操作的指数 * return幂操作的结果 */public static int getXY(int x,int y)int temp = x;if(y = 0) x = 1;for(int i=2; i 4)return null;for(int i=len; i0; i-)result += Integer.parseInt(s.substring(i-1, i)*getXY(2,k);k+;switch(result)case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:return +result;case 10:return A;case 11:return B;case 12:return C;case 13:return D;case 14:return E;case 15:return F;default :return null;/* * 将一个小于16的数转换为一个对应的十六进制字符 * param i 小于16的整数 * return 十六进制字符 */public static String int2Hex(int i)switch(i)case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:return +i;case 10:return A;case 11:return B;case 12:return C;case 13:return D;case 14:return E;case 15:return F;default :return null;/* * 将0,1组成的字符串转换为对应的十六进制字符串,例如输入字符串111100001111输出为FOF * 此方法输入的字符串长度不限 * param s 0,1组成的字符串 * return十六进制字符串 */public static String binary2ASC(String s)String str = ;int ii = 0;int len = s.length();if(len%4 != 0)while(ii4-len%4)s = 0 + s;for(int i=0; ilen/4; i+)str += binary2Hex(s.substring(i*4, i*4+4);return str;public static int changeIP(int source)int dest = new int64;for(int i=0; i64; i+)desti = sourceipi-1;return dest;public static int changeInverseIP(int source)int dest = new int64;for(int i=0; i64; i+)desti = source_ipi-1;return dest; public static int expend(int source)int ret = new int48;int temp = 32, 1, 2, 3, 4, 5,4, 5, 6, 7, 8, 9,8, 9, 10, 11, 12, 13,12, 13, 14, 15, 16, 17,16, 17, 18, 19, 20, 21,20, 21, 22, 23, 24, 25,24, 25, 26, 27, 28, 29,28, 29, 30, 31, 32, 1;for(int i=0; i48; i+)reti = sourcetempi-1;return ret; public static int press(int source)int ret = new int32;int temp = new int86;int s = s1,s2,s3,s4,s5,s6,s7,s8;StringBuffer str = new StringBuffer();for(int i=0; i8; i+)for(int j=0; j6; j+)tempij = sourcei*6+j;for(int i=0; i8; i+)int x = tempi0*2 + tempi5;int y = tempi1*8 + tempi2*4 + tempi3*2 + tempi4;int val = sixy;String ch = int2Hex(val);str.append(ch);ret = string2Binary(str.toString();ret = dataP(ret);return ret;public static int dataP(int source)int dest = new int32;int temp = 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25;int len = source.length;for(int i=0; ilen; i+)desti = sourcetempi-1;return dest;public static int f(int R,int K)int dest = new int32;int temp = new int48; int expendR = expend(R);temp = diffOr(expendR, K);dest = press(temp);return dest;/* * 对两个int类型数组执行异或操作 * param source1 操作数1 * param source2 操作数2 * return 异或结果 */public static int diffOr(int source1,int source2)int len = source1.length;int dest = new intlen;for(int i=0; ilen; i+)desti = source1i source2i;return dest;/* * 数据加密 * param D 被加密数据 * param K 加密密钥 * return 加密结果 */public static String encryption(String D, String K)String str = ;int temp = new int64;int data = string2Binary(D);data = changeIP(data);int left = new int1732;int right = new int1732;for(int j=0; j32; j+)left0j = dataj;right0j = dataj+32;setKey(K);for(int i=1; i17; i+)int key = subKeyi-1;lefti = righti-1;int fTemp = f(righti-1,key);righti = diffOr(lefti-1,fTemp);for(int i=0; i32; i+)tempi = right16i;temp32+i = left16i;temp = changeInverseIP(temp);str = binary2ASC(intArr2Str(temp);return str;/* * 解密 * param source 解密目标 * param key 解密密钥 * return 解密结果 */public static String discryption(String source,String key)String str = ;int data = string2Binary(source);data = changeIP(data);int left = new int32;int right = new int32;int tmp = new int32;for(int j=0; j0; i-)int sKey = subKeyi-1;tmp = left;left = right;int fTemp = f(right,sKey);right = diffOr(tmp,fTemp);for(int i=0; i32; i+)datai = righti;data32+i = lefti;data = changeInverseIP(data);for(int i=0; idata.length; i+)str += datai;str = binary2ASC(str);return str;/* * DES加解密 type 参数为0 表示加密,type 为1 表示解密 * param source 目标数据 * param key 密钥 * param type 加解密类型 * return 加加解密结果 */public static String DES_1(String source,String key,int type)if(source.length() != 16 | key.length() != 16)return null;if(type=0)return encryption(source, key);if(type=1)return discryption(source, key);return null;/* * 3DES加解密 目标数据只能为16为长度 * type参数为0表示加密,参数为1表示解密 * param source 目标数据 16位长度的16进制字符串 * param key 密钥 * param type 加解密类型 * return 加解密结果 */public static String DES_3(String source,String key,int type)if(key.length() != 32 | source.length() != 16)return null;String temp = null;String K1 = key.substring(0, key.length()/2);String K2 = key.substring(key.length()/2);if(type=0)temp = encryption(source, K1);temp = discryption(temp, K2);return encryption(temp, K1);if(type=1)temp = discryption(source, K1);temp = encryption(temp, K2);return discryption(temp, K1);return null;/* * 3DES加解密 目标数据为32为长度 * type参数为0表示加密,参数为1表示解密 * param source 目标数据 16位长度的16进制字符串 * param key 密钥 * param type 加解密类型 * return 加解密结果 */public static String DES_3_32(String source,String key,int type)if(key.length() != 32 | source.length() != 32)return null;String srcfirst = source.substring(0,16);String srcsecond = source.substring(16);return DES_3(srcfirst,key,type) + DES_3(srcsecond,key,type);public static int keyPC_1(int source)int dest = new int56;int temp = 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4;for(int i=0; i56; i+)desti = sourcetempi-1;return dest;public static int keyLeftMove(int source, int i)int temp = 0;int len = source.length;int ls = LSi;for(int k=0; kls; k+)temp = source0;for(int j=0; jlen-1; j+)sourcej = sourcej+1;sourcelen-1 = temp;return source;public static int keyPC_2(int source)int dest = new int48;int temp = 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32;for(int i=0; i 0)subKey = new int1648;int temp = string2Binary(source);int left = new int28;int right = new int28;int temp1 = new int56;temp1 = keyPC_1(temp);for(int i=0; i28; i+)lefti = temp1i;righti = temp1i+28;for(int i=0; i16; i+)left = keyLeftMove(left, LSi);right = keyLeftMove(right, LSi);for(int j=0; j28; j+)temp1j = leftj;temp1j+28 = rightj;subKeyi = keyPC_2(temp1);/* * 将字符串转换为16进制表示,首先调用getBytes方法获取字符串的字节表示方法 * 再将字节数组转换为16进制字符串表示 * param asc 转换目标 * return 转换结果 */public static String ASC_2_HEX(String asc)StringBuffer hex = new StringBuffer();try byte bs = asc.toUpperCase().getBytes(UTF-8);for(byte b : bs)hex.append(Integer.toHexString(new Byte(b).intValue(); catch (UnsupportedEncodingException e) e.printStackTrace();return hex.toString();public static String HEX_2_ASC(String hex)String asc = null;int len = hex.length();byte bs = new bytelen/2;for(int i=0; ilen/2; i+)bsi = Byte.parseByte(hex.substring(i*2, i*2+2), 16);try asc = new String(bs,UTF-8); catch (UnsupportedEncodingException e) e.printStackTrace();return asc;public static String xOr(String s1,String s2)int iArr = diffOr(string2Binary(s1), string2Binary(s2);return binary2ASC(intArr2Str(iArr);public static String intArr2Str(int arr)StringBuffer sb = new StringBuffer();for(int i=0; iarr.length; i+)sb.append(arri);return sb.toString();public static String divData(String data,String key,int type)String left = null;String right = null;if(type = HEX)left = key.substring(0, 16);right = key.substring(16,32);if(type = ASC)left = ASC_2_HEX(key.substring(0, 8);right = ASC_2_HEX(key.substring(8,16);data = DES_1(data, left, 0);data = DES_1(data, right, 1);data = DES_1(data, left, 0);return data;public static String reverse(String source)int data = string2Binary(source);int j = 0;for(int i : data)dataj+ = 1 - i;return binary2ASC(intArr2Str(data);public static String getDPK(String issuerFlag,String appNo,String mpk)StringBuffer issuerMPK = new StringBuffer();issuerMPK.append(divData(issuerFlag, mpk, 0);issuerMPK.append(divData(reverse(issuerFlag), mpk, 0);StringBuffer dpk = new StringBuffer();dpk.append(divData(appNo, issuerMPK.toString(), 0);dpk.append(divData(reverse(appNo), issuerMPK.toString(), 0);return dpk.toString();public static String xOrString(String pan,String pin)if(pan.length() != pin.length()new Exception(异或因子长度不一致).printStackTrace();return null;byte bytepan = ByteUtil.getHexByte(pan);byte bytepin = ByteUtil.getHexByte(pin);byte result = new bytebytepan.length;for(int i = 0;i result.length;i+)result i = (byte)(bytepani bytepini);return ByteUtil.getHexStr(result);/* * 跟据卡号和密码明文生成pin * param cardNo 卡号 * param pwd 密码明文 * param key 密钥 * return加密后的数据,16进制表示的字符串 */public static String createPwd(String cardNo,String pwd,String key)String last6 = cardNo.substring(cardNo.length() - 13,cardNo.length() - 1);String first2 = 0000;String pan = first2 + last6;String pin = 06 + pwd + FFFFFFFF;String result = xOrString(pan,pin);String ret = DES_3(result,key,0);return ret;/* * 生成ANSI-X9.9-MAC校验码 * 所有的输入输出数据必须是16进制字符串 * 密钥只能为16位长度的16进制字符串,否则将返回null * 初始向量为16为长度的16进制字符串,如果不符合条件,自动设置为0000
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年合作项目进度汇报与调整函8篇范文
- 订单处理超时催办函(8篇)
- 文化旅游产业的数字化推广及运营模式研究
- 2026陕西榆林吴堡县大学生到政府机关见习34人参考题库及参考答案详解(B卷)
- 关于某项合作协议变更的通知函7篇
- 2026浙江杭州市萧山区文学艺术界联合会下属事业单位选用事业编制工作人员1人模拟试卷附参考答案详解【能力提升】
- 2026河北保定阜平中学选聘专任教师8名笔试题库及参考答案详解【新】
- M公司电商数据中台项目风险管理研究
- 2025年中国凹球包角数据监测报告
- 2025年中国全PU振动光饰机数据监测报告
- 葡萄作物栽培技术
- 2025至2030中国医疗美容行业市场供需格局与投资价值分析报告
- 2026年陕西省中考语文试题及答案
- 精益工程师培训
- (正式版)DBJ33∕T 1286-2022 《住宅工程质量常见问题控制标准》
- 2025年卫生管理(副高)高级职称考试题库及答案
- 2025年安徽九华山旅游发展股份有限公司招聘66人笔试参考题库附答案
- 2024年中国铁路广州局集团有限公司招聘高校毕业生考试真题
- 国开2025年《数据库应用技术》形考作业1-4答案
- 湘江战役教学课件
- WeleUnitDiscoveringUsefulStructures句子基本结构课件-高中英语人教版
评论
0/150
提交评论