private byte[] decrypt(byte[] encoded) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchProviderException, InvalidAlgorithmParameterException { byte[] iv = new byte[16]; byte[] cryptogram = new byte[encoded.length - iv.length]; System.arraycopy(encoded, 0, iv, 0, iv.length); System.arraycopy(encoded, iv.length, cryptogram, 0, cryptogram.length); final Cipher cipher = Cipher.getInstance(AES); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ISOUtil.xor(iv, SystemSeed.getSeed(iv.length, iv.length)))); return cipher.doFinal(cryptogram); }
public byte[] getSalt(byte[] salt) { return ISOUtil.xor(salt, getSalt()); }
public byte[] getSalt() { return ISOUtil.xor(salt, SystemSeed.getSeed(salt.length, salt.length)); }
private byte[] encrypt(byte[] b) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { final Cipher cipher = Cipher.getInstance(AES); final byte[] iv = randomIV(); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); byte[] enc = cipher.doFinal(b); ByteBuffer buf = ByteBuffer.allocate(iv.length + enc.length); buf.put(ISOUtil.xor(iv, SystemSeed.getSeed(iv.length, iv.length))); buf.put(enc); return buf.array(); } private byte[] decrypt(byte[] encoded)
/** * Derive the session key used for Application Cryptogram verification * or for secure messaging, the diversification value is the ATC * @param mkac unique ICC Master Key for Application Cryptogams or Secure Messaging * @param atc ICC generated Application Transaction Counter as diversification value * @return derived 16-bytes Session Key with adjusted DES parity * @throws JCEHandlerException */ private Key deriveSK_VISA(Key mkac, byte[] atc) throws JCEHandlerException { byte[] skl = new byte[8]; System.arraycopy(atc, atc.length-2, skl, 6, 2); skl = ISOUtil.xor(skl, Arrays.copyOfRange(mkac.getEncoded(),0 ,8)); byte[] skr = new byte[8]; System.arraycopy(atc, atc.length-2, skr, 6, 2); skr = ISOUtil.xor(skr, ISOUtil.hex2byte("000000000000FFFF")); skr = ISOUtil.xor(skr, Arrays.copyOfRange(mkac.getEncoded(),8 ,16)); Util.adjustDESParity(skl); Util.adjustDESParity(skr); return jceHandler.formDESKey(SMAdapter.LENGTH_DES3_2KEY, ISOUtil.concat(skl,skr)); }
private byte[] specialDecrypt(byte[] data, byte[] key) throws JCEHandlerException { if (key.length == 8) { data = ISOUtil.xor(data, key); data = decrypt64(data, key); return ISOUtil.xor(data, key); } byte[] keyL = new byte[8]; byte[] keyR = new byte[8]; System.arraycopy(key, 0, keyL, 0, 8); System.arraycopy(key, 8, keyR, 0, 8); data = decrypt64(data, keyL); data = encrypt64(data, keyR); data = decrypt64(data, keyL); return data; }
protected byte[] getKey() { return ISOUtil.xor(SystemSeed.getSeed(8, 8), ISOUtil.hex2byte("BD653F60F980F788")); } protected Document encrypt (Document doc)
private byte[] specialEncrypt(byte[] data, byte[] key) throws JCEHandlerException { if (key.length == 8) { data = ISOUtil.xor(data, key); data = encrypt64(data, key); return ISOUtil.xor(data, key); } byte[] keyL = new byte[8]; byte[] keyR = new byte[8]; System.arraycopy(key, 0, keyL, 0, 8); System.arraycopy(key, 8, keyR, 0, 8); data = encrypt64(data, keyL); data = decrypt64(data, keyR); data = encrypt64(data, keyL); return data; }
/** * Bitwise XOR between corresponding byte arrays represented in hex * @param op1 hexstring 1 * @param op2 hexstring 2 * @return an array of length = the smallest between op1 and op2 */ public static String hexor (String op1, String op2) { byte[] xor = xor (hex2byte (op1), hex2byte (op2)); return hexString (xor); }
@Test(expected = NullPointerException.class) public void testXorThrowsNullPointerException() throws Throwable { byte[] op2 = new byte[0]; ISOUtil.xor(null, op2); }
private String calculateCVD(String accountNo, Key cvk, String expDate, String serviceCode) throws SMException { Key udka = jceHandler.formDESKey(SMAdapter.LENGTH_DES ,Arrays.copyOfRange(cvk.getEncoded(), 0, 8)); byte[] block = ISOUtil.hex2byte( ISOUtil.zeropadRight(accountNo + expDate + serviceCode, 32)); byte[] ba = Arrays.copyOfRange(block, 0, 8); byte[] bb = Arrays.copyOfRange(block, 8,16); //Encrypt ba with udka byte[] bc = jceHandler.encryptData(ba, udka); byte[] bd = ISOUtil.xor(bc, bb); //Encrypt bd Tripple DES byte[] be = jceHandler.encryptData(bd, cvk); return decimalizeVisa(be).substring(0,3); }
/** * Calculate MAC according to ISO/IEC 9797-1 Alg 3 * @param key DES double length key * @param d data to calculate MAC on it * @return 8 byte of mac value * @throws JCEHandlerException */ private byte[] calculateMACISO9797Alg3(Key key, byte[] d) throws JCEHandlerException { Key kl = jceHandler.formDESKey(SMAdapter.LENGTH_DES ,Arrays.copyOfRange(key.getEncoded(), 0, 8)); Key kr = jceHandler.formDESKey(SMAdapter.LENGTH_DES ,Arrays.copyOfRange(key.getEncoded(), 8, 16)); if (d.length%8 != 0) { //Padding with 0x00 bytes byte[] t = new byte[d.length - d.length%8 + 8]; System.arraycopy(d, 0, t, 0, d.length); d = t; } //MAC_CBC alg 3 byte[] y_i = ISOUtil.hex2byte("0000000000000000"); byte[] yi = new byte[8]; for ( int i=0;i<d.length;i+=8){ System.arraycopy(d, i, yi, 0, yi.length); y_i = jceHandler.encryptData(ISOUtil.xor(yi, y_i), kl); } y_i = jceHandler.decryptData(y_i, kr); y_i = jceHandler.encryptData(y_i, kl); return y_i; }
/** * Calculate ARPC. * <p> * Entry point e.g. for simulator systems */ byte[] calculateARPC(Key skarpc, byte[] arqc, ARPCMethod arpcMethod ,byte[] arc, byte[] propAuthData) throws SMException { if (arpcMethod == null) arpcMethod = ARPCMethod.METHOD_1; byte[] b = new byte[8]; switch(arpcMethod) { case METHOD_1: System.arraycopy(arc, arc.length-2, b, 0, 2); b = ISOUtil.xor(arqc, b); return jceHandler.encryptData(b, skarpc); case METHOD_2: b = ISOUtil.concat(arqc, arc); if (propAuthData != null) b = ISOUtil.concat(b, propAuthData); b = paddingISO9797Method2(b); b = calculateMACISO9797Alg3(skarpc, b); return Arrays.copyOf(b, 4); default: throw new SMException("ARPC Method "+arpcMethod+" not supported"); } }
@Test public void testXor1() throws Throwable { byte[] op2 = new byte[4]; byte[] op1 = new byte[0]; byte[] result = ISOUtil.xor(op1, op2); assertEquals("result.length", 0, result.length); }
byte[] r = Arrays.copyOfRange(panpsn, 0, 8); r = ISOUtil.xor(r, JCESecurityModule.fPaddingBlock);
@Test public void testXor() throws Throwable { byte[] op2 = new byte[0]; byte[] result = ISOUtil.xor(ISOUtil.str2bcd("testISOUtils", true), op2); assertEquals("result.length", 0, result.length); }
@Test public void testXor2() throws Throwable { byte[] op1 = new byte[3]; byte[] op2 = new byte[2]; byte[] result = ISOUtil.xor(op1, op2); assertEquals("result.length", 2, result.length); assertEquals("result[0]", (byte) 0, result[0]); }
@Test public void testXor3() throws Throwable { byte[] op1 = new byte[3]; byte[] op2 = new byte[5]; byte[] result = ISOUtil.xor(op1, op2); assertEquals("result.length", 3, result.length); assertEquals("result[0]", (byte) 0, result[0]); }
@Test public void testWellKnownSeeds() throws Throwable { assertEquals("Invalid seed 0", "371B4CF737319AC15BD6B18DE9E8B8537D2D2CC6323B4E728C8A06C654FBBA06", ISOUtil.hexString(ISOUtil.xor(SystemSeed.getSeed(16,32), SystemSeed.getSeed(4064-32)))); assertEquals("Invalid seed 1", "6835BEFB07E7965940A99D46C7FAC87561DCB1FD7BF4932DB5ACC1E0529B1AE891B0368E14D5F9BFFC74E380426C3A7B", ISOUtil.hexString(ISOUtil.xor(SystemSeed.getSeed(0,1024), SystemSeed.getSeed(2048,48)))); assertEquals("Invalid seed 3", "6835BEFB07E7965940A99D46C7FAC87561DCB1FD7BF4932DB5ACC1E0529B1AE891B0368E14D5F9BFFC74E380426C3A7B", ISOUtil.hexString(ISOUtil.xor(SystemSeed.getSeed(0,1024), SystemSeed.getSeed(6144,48)))); assertEquals("Invalid seed 3", ISOUtil.hexString(new byte[8192]), ISOUtil.hexString(ISOUtil.xor(SystemSeed.getSeed(1,8192), SystemSeed.getSeed(4097,16384)))); } }