Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.Arrays;

public class Main {
    public static byte[] createPinEncryption(String pin, String pan) {
        byte[] decryption = new byte[8];
        byte[] bytePin = new byte[8];
        byte[] bytePan = new byte[8];
        Arrays.fill(bytePin, (byte) 0xFF);
        Arrays.fill(bytePan, (byte) 0x00);

        byte[] srcPin = HexStringToByteArray(pin);
        byte[] srcPan = HexStringToByteArray(pan);

        bytePin[0] = 0x06;
        System.arraycopy(srcPin, 0, bytePin, 1, srcPin.length);
        System.arraycopy(srcPan, 2, bytePan, 2, srcPan.length - 2);

        for (int i = 0; i < 8; i++) {
            decryption[i] = (byte) (bytePin[i] ^ bytePan[i]);
        }

        return decryption;
    }

    public static byte[] HexStringToByteArray(String hexString) {//
        if (hexString == null || hexString.equals("")) {
            return new byte[] {};
        }
        if (hexString.length() == 1 || hexString.length() % 2 != 0) {
            hexString = "0" + hexString;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}