我正在使用IBM 3624算法进行Pin Generation,然后使用它的Offset。以下是IBM Website的算法:
我不确定顺序,必须使用输入数据。例如,该算法需要验证数据(起始位置,长度,PAN填充字符和引脚长度),十进制表(0123456789012345),Pin,Pan& PDK。
编辑:这是输入数据格式 - PAN(帐号或卡号):16位六角字符串。 引脚(通常为4位数,但可根据要求进行更改):4位数字(十六进制值从十进制表中替换) PDK(PAN提供的加密密钥):32位数字
开始位置&长度:从PAN中选择的数字,最后一位是校验位,将被忽略。 PAN的这些选定数字后来被填充回16位数。
Pad Char:单个字符(十六进制数字)。
以下是我用来执行此操作的代码:
public static void CalculatePINOffset(String PAN, String Pin, String PDKkey, String DecTab, int StartPos, int Length,
char PadChar) throws Exception{
int PANLength = PAN.length();
if(Length != (PANLength - StartPos)){
throw new Exception(
"Invalid 'Start Pos and Length' format.");
}
//Padding the PAN before Start POS with Pad Chars back to 16 digits.
String block = ISOUtil.padleft(PAN.substring(StartPos, Length + (StartPos - 1)), PAN.length(), PadChar);
/*
* Doing encryption stuff on block with PDKKey.
* The execute function basically encrypts block and PDKKey, and any algorithm could do the work.
*/
String result = execute(block , PDKkey, "2TDES");
Map<Character, Character> decTab = new HashMap<Character, Character>();
decTab.put('A', '0');
decTab.put('B', '1');
decTab.put('C', '2');
decTab.put('D', '3');
decTab.put('E', '4');
decTab.put('F', '5');
//Replacing Hex Characters with numbers from Decmalization table.
char[] Inpin = result.substring(0, 4).toCharArray();
for(int i = 0; i < Inpin.length; i++){
if(decTab.containsKey(Inpin[i])){
Inpin[i] = decTab.get(Inpin[i]);
}
}
result = new String(Inpin);
System.out.println("Intermediate PIN: "+result);
//Calculating offset from Intermediate Pin.
int[] Offset = new int[4];
int Cpin;
int Ipin;
for(int i = 0; i < result.length(); i++){
Ipin = Integer.parseInt(result.substring(i, i+1));
Cpin = Integer.parseInt(Pin.substring(i, i+1));
if((Cpin - Ipin) >= 0){
Offset[i] = (Cpin - Ipin);
}
else{
Offset[i] = (Ipin - Cpin)%10;
}
}
String PinOffset = Arrays.toString(Offset);
System.out.println("Pin Offset: " + PinOffset);
}
注意:我不是在寻找代码或实现。我提供了这段代码,以便更好地解释使用顺序。所有可以帮助我的是正确的加密顺序,验证数据的使用和十进制表。
使用BP-Tools等开源工具进行交叉检查时,生成的偏移量和引脚不匹配。我哪里出错了?
答案 0 :(得分:3)
好的,我明白了。如果有人在寻找同样的东西,这就是完成它的过程:
1. Generate validation data from PAN:
- Get PAN Digits from Start Position to Length, and add Pad characters to the right, until it was 16 digits in length.
2. Encrypt validation data with pdk key using DES, 3DES or AES algorithm. This will generate an encrypted code.
3. Decimalize encrypted code using decimalization table.
4. Convert to Hex String, and get the first n digits (n = pin length). This will generate intermediate Pin.
5. Finally, Modulo Subtract intermediate pin with customer pin. This generates the Offset.
希望这会有所帮助。 - 谢谢!
答案 1 :(得分:1)
问题是两个:
public String calculateOffset(String pin, String pan, String pvk) throws Exception{
System.out.println("CryptoProcessor.calculateOffset");
Map decTab = Arrays.asList("0:0", "1:1", "2:2", "3:3", "4:4", "5:5","6:6", "7:7", "8:8", "9:9","A:0", "B:1", "C:2", "D:3", "E:4", "F:5")
.stream()
.map(elem -> elem.split(":"))
.filter(elem -> elem.length==2)
.collect(Collectors.toMap(e -> e[0], e -> e[1]));
if(pan.length() = 0){
Offset[i] = (Cpin - Ipin);
}else{
Offset[i] = (Cpin + 10 - Ipin);
}
}
StringBuffer offset = new StringBuffer();
IntStream.of(Offset).boxed().collect(Collectors.toList()).forEach(a -> offset.append(a.toString()));
System.out.println("Pin offset: " + offset.toString());
return offset.toString();
}
Pan:1234567899876543
Pin:3196
pvk:0123456789ABCDEFFEDCBA9876543210
Validation data 456789987654FFFF
Jan 28, 2017 4:13:23 PM com.ocularminds.oswitch.crypto.CryptoProcessor process
INFO: Data : 456789987654FFFF
encrypted data 3DE7489FEBC9340D
Intermediate PIN: 3347
Pin offset: 0859