private void initCiphers(byte[] key, byte[] iv) { // get the keyBytes keyBytes = new byte[key.length]; System.arraycopy(key, 0, keyBytes, 0, key.length); keyP = new KeyParameter(keyBytes); // get the IV IV = new byte[blockSize]; System.arraycopy(iv, 0, IV, 0, IV.length); // create the ciphers // AES block cipher in CBC mode with ISO7816d4 padding encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher( new AESFastEngine()), new ISO7816d4Padding()); decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher( new AESFastEngine()), new ISO7816d4Padding()); // create the IV parameter ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV); encryptCipher.init(true, parameterIV); decryptCipher.init(false, parameterIV); }
private void initCiphers(byte[] key, byte[] iv) { // get the keyBytes keyBytes = new byte[key.length]; System.arraycopy(key, 0, keyBytes, 0, key.length); // get the IV IV = new byte[blockSize]; System.arraycopy(iv, 0, IV, 0, iv.length); keyP = new KeyParameter(keyBytes); encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher( new DESedeEngine()), new ISO7816d4Padding()); decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher( new DESedeEngine()), new ISO7816d4Padding()); // create the IV parameter ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV); encryptCipher.init(true, parameterIV); decryptCipher.init(false, parameterIV); }
public int doFinal(byte[] out, int outOff) { int blockSize = cipher.getBlockSize(); byte[] lu; if (bufOff == blockSize) { lu = Lu; } else { new ISO7816d4Padding().addPadding(buf, bufOff); lu = Lu2; } for (int i = 0; i < mac.length; i++) { buf[i] ^= lu[i]; } cipher.processBlock(buf, 0, mac, 0); System.arraycopy(mac, 0, out, outOff, macSize); reset(); return macSize; }
private static void initBlockCipherPaddings() { blockCipherPadding.put("ISO10126d2Padding", ISO10126d2Padding.class); blockCipherPadding.put("ISO7816d4Padding", ISO7816d4Padding.class); blockCipherPadding.put("PKCS7Padding", PKCS7Padding.class); blockCipherPadding.put("TBCPadding", TBCPadding.class); blockCipherPadding.put("X923Padding", X923Padding.class); blockCipherPadding.put("ZeroBytePadding", ZeroBytePadding.class); }
@Override public byte[] getMAC(byte[] key, byte[] data) { BlockCipher cipher = new DESEngine(); Mac mac = new ISO9797Alg3Mac(cipher, 64, new ISO7816d4Padding()); KeyParameter keyP = new KeyParameter(key); mac.init(keyP); mac.update(data, 0, data.length); byte[] out = new byte[8]; mac.doFinal(out, 0); return out; }
/** * Diese Methode füllt ein Byte-Array mit dem Wert 0x80 und mehreren 0x00 * bis die Länge des übergebenen Byte-Array ein Vielfaches der Blocklänge * ist. Dies ist die ISO9797-1 Padding-Methode 2 bzw. ISO7816d4-Padding * * @param data * Das Byte-Array welches aufgefüllt werden soll. * @return Das gefüllte Byte-Array. */ public byte[] addPadding(byte[] data) { int len = data.length; int nLen = ((len / getBlockSize()) + 1) * getBlockSize(); byte[] n = new byte[nLen]; System.arraycopy(data, 0, n, 0, data.length); new ISO7816d4Padding().addPadding(n, len); return n; }
public DES64with7816d4() { super(new CBCBlockCipherMac(new DESEngine(), 64, new ISO7816d4Padding())); }
public DES9797Alg3with7816d4() { super(new ISO9797Alg3Mac(new DESEngine(), new ISO7816d4Padding())); }
public DESede64with7816d4() { super(new CBCBlockCipherMac(new DESedeEngine(), 64, new ISO7816d4Padding())); }
protected void engineSetPadding( String padding) throws NoSuchPaddingException { String paddingName = Strings.toUpperCase(padding); if (paddingName.equals("NOPADDING")) { if (cipher.wrapOnNoPadding()) { cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(cipher.getUnderlyingCipher())); } } else if (paddingName.equals("WITHCTS")) { cipher = new BufferedGenericBlockCipher(new CTSBlockCipher(cipher.getUnderlyingCipher())); } else { padded = true; if (isAEADModeName(modeName)) { throw new NoSuchPaddingException("Only NoPadding can be used with AEAD modes."); } else if (paddingName.equals("PKCS5PADDING") || paddingName.equals("PKCS7PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher()); } else if (paddingName.equals("ZEROBYTEPADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ZeroBytePadding()); } else if (paddingName.equals("ISO10126PADDING") || paddingName.equals("ISO10126-2PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ISO10126d2Padding()); } else if (paddingName.equals("X9.23PADDING") || paddingName.equals("X923PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new X923Padding()); } else if (paddingName.equals("ISO7816-4PADDING") || paddingName.equals("ISO9797-1PADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new ISO7816d4Padding()); } else if (paddingName.equals("TBCPADDING")) { cipher = new BufferedGenericBlockCipher(cipher.getUnderlyingCipher(), new TBCPadding()); } else { throw new NoSuchPaddingException("Padding " + padding + " unknown."); } } }
public void performTest() { SecureRandom rand = new SecureRandom(new byte[20]); rand.setSeed(System.currentTimeMillis()); testPadding(new PKCS7Padding(), rand, Hex.decode("ffffff0505050505"), Hex.decode("0000000004040404")); PKCS7Padding padder = new PKCS7Padding(); try { padder.padCount(new byte[8]); fail("invalid padding not detected"); } catch (InvalidCipherTextException e) { if (!"pad block corrupted".equals(e.getMessage())) { fail("wrong exception for corrupt padding: " + e); } } testPadding(new ISO10126d2Padding(), rand, null, null); testPadding(new X923Padding(), rand, null, null); testPadding(new TBCPadding(), rand, Hex.decode("ffffff0000000000"), Hex.decode("00000000ffffffff")); testPadding(new ZeroBytePadding(), rand, Hex.decode("ffffff0000000000"), null); testPadding(new ISO7816d4Padding(), rand, Hex.decode("ffffff8000000000"), Hex.decode("0000000080000000")); testOutputSizes(); }
private static byte[] pad(byte[] in) { final byte[] result = Arrays.copyOf(in, 16); new ISO7816d4Padding().addPadding(result, in.length); return result; }
@Override public byte[] getMAC(byte[] data) { byte[] n = new byte[8 + data.length]; System.arraycopy(sscBytes, 0, n, 0, 8); System.arraycopy(data, 0, n, 8, data.length); BlockCipher cipher = new DESEngine(); Mac mac = new ISO9797Alg3Mac(cipher, 64, new ISO7816d4Padding()); ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV); mac.init(parameterIV); mac.update(n, 0, n.length); byte[] out = new byte[8]; mac.doFinal(out, 0); return out; }
@Override public MACCalculator _createMACCalculator() { final BlockCipher cipher = new DESEngine(); return new MACCalculatorImpl(new ISO9797Alg3Mac(cipher, new ISO7816d4Padding()), 24, cipher.getBlockSize()); }
public void performTest() { SecureRandom rand = new SecureRandom(new byte[20]); rand.setSeed(System.currentTimeMillis()); testPadding(new PKCS7Padding(), rand, Hex.decode("ffffff0505050505"), Hex.decode("0000000004040404")); PKCS7Padding padder = new PKCS7Padding(); try { padder.padCount(new byte[8]); fail("invalid padding not detected"); } catch (InvalidCipherTextException e) { if (!"pad block corrupted".equals(e.getMessage())) { fail("wrong exception for corrupt padding: " + e); } } testPadding(new ISO10126d2Padding(), rand, null, null); testPadding(new X923Padding(), rand, null, null); testPadding(new TBCPadding(), rand, Hex.decode("ffffff0000000000"), Hex.decode("00000000ffffffff")); testPadding(new ZeroBytePadding(), rand, Hex.decode("ffffff0000000000"), null); testPadding(new ISO7816d4Padding(), rand, Hex.decode("ffffff8000000000"), Hex.decode("0000000080000000")); }