Example usage for org.bouncycastle.crypto.paddings ISO7816d4Padding ISO7816d4Padding

List of usage examples for org.bouncycastle.crypto.paddings ISO7816d4Padding ISO7816d4Padding

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.paddings ISO7816d4Padding ISO7816d4Padding.

Prototype

ISO7816d4Padding

Source Link

Usage

From source file:com.licel.jcardsim.crypto.SymmetricCipherImpl.java

License:Apache License

private void selectCipherEngine(Key theKey) {
    if (theKey == null) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }//from   www  . ja  v a  2 s . c  o m
    if (!theKey.isInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }
    if (!(theKey instanceof SymmetricKeyImpl)) {
        CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
    }
    SymmetricKeyImpl key = (SymmetricKeyImpl) theKey;
    switch (algorithm) {
    case ALG_DES_CBC_NOPAD:
    case ALG_AES_BLOCK_128_CBC_NOPAD:
        engine = new BufferedBlockCipher(new CBCBlockCipher(key.getCipher()));
        break;
    case ALG_DES_CBC_ISO9797_M1:
        engine = new PaddedBufferedBlockCipher(new CBCBlockCipher(key.getCipher()), new ZeroBytePadding());
        break;
    case ALG_DES_CBC_ISO9797_M2:
        engine = new PaddedBufferedBlockCipher(new CBCBlockCipher(key.getCipher()), new ISO7816d4Padding());
        break;
    case ALG_DES_CBC_PKCS5:
        engine = new PaddedBufferedBlockCipher(new CBCBlockCipher(key.getCipher()), new PKCS7Padding());
        break;
    case ALG_DES_ECB_NOPAD:
    case ALG_AES_BLOCK_128_ECB_NOPAD:
        engine = new BufferedBlockCipher(key.getCipher());
        break;
    case ALG_DES_ECB_ISO9797_M1:
        engine = new PaddedBufferedBlockCipher(key.getCipher(), new ZeroBytePadding());
        break;
    case ALG_DES_ECB_ISO9797_M2:
        engine = new PaddedBufferedBlockCipher(key.getCipher(), new ISO7816d4Padding());
        break;
    case ALG_DES_ECB_PKCS5:
        engine = new PaddedBufferedBlockCipher(key.getCipher(), new PKCS7Padding());
        break;
    default:
        CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM);
        break;
    }
}

From source file:com.licel.jcardsim.crypto.SymmetricSignatureImpl.java

License:Apache License

public void init(Key theKey, byte theMode, byte[] bArray, short bOff, short bLen) throws CryptoException {
    if (theKey == null) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }/*from  w  w w.  jav  a2  s. c om*/
    if (!theKey.isInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }
    if (!(theKey instanceof SymmetricKeyImpl)) {
        CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
    }
    CipherParameters cipherParams = null;
    BlockCipher cipher = ((SymmetricKeyImpl) theKey).getCipher();
    if (bArray == null) {
        cipherParams = ((SymmetricKeyImpl) theKey).getParameters();
    } else {
        if (bLen != cipher.getBlockSize()) {
            CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
        }
        cipherParams = new ParametersWithIV(((SymmetricKeyImpl) theKey).getParameters(), bArray, bOff, bLen);
    }
    switch (algorithm) {
    case ALG_DES_MAC4_NOPAD:
        engine = new CBCBlockCipherMac(cipher, 32, null);
        break;
    case ALG_DES_MAC8_NOPAD:
        engine = new CBCBlockCipherMac(cipher, 64, null);
        break;
    case ALG_DES_MAC4_ISO9797_M1:
        engine = new CBCBlockCipherMac(cipher, 32, new ZeroBytePadding());
        break;
    case ALG_DES_MAC8_ISO9797_M1:
        engine = new CBCBlockCipherMac(cipher, 64, new ZeroBytePadding());
        break;
    case ALG_DES_MAC4_ISO9797_M2:
        engine = new CBCBlockCipherMac(cipher, 32, new ISO7816d4Padding());
        break;
    case ALG_DES_MAC8_ISO9797_M2:
        engine = new CBCBlockCipherMac(cipher, 64, new ISO7816d4Padding());
        break;
    case ALG_DES_MAC8_ISO9797_1_M2_ALG3:
        engine = new ISO9797Alg3Mac(new DESEngine(), 64, new ISO7816d4Padding());
        break;
    case ALG_DES_MAC4_PKCS5:
        engine = new CBCBlockCipherMac(cipher, 32, new PKCS7Padding());
        break;
    case ALG_DES_MAC8_PKCS5:
        engine = new CBCBlockCipherMac(cipher, 64, new PKCS7Padding());
        break;
    case ALG_AES_MAC_128_NOPAD:
        engine = new CBCBlockCipherMac(cipher, 128, null);
        break;
    case ALG_HMAC_SHA1:
        engine = new HMac(new SHA1Digest());
        break;
    case ALG_HMAC_SHA_256:
        engine = new HMac(new SHA256Digest());
        break;
    case ALG_HMAC_SHA_384:
        engine = new HMac(new SHA384Digest());
        break;
    case ALG_HMAC_SHA_512:
        engine = new HMac(new SHA512Digest());
        break;
    case ALG_HMAC_MD5:
        engine = new HMac(new MD5Digest());
        break;
    case ALG_HMAC_RIPEMD160:
        engine = new HMac(new RIPEMD160Digest());
        break;
    default:
        CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM);
        break;
    }
    engine.init(cipherParams);
    isInitialized = true;
}

From source file:de.tsenger.animamea.crypto.AmAESCrypto.java

License:Open Source License

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);
}

From source file:de.tsenger.animamea.crypto.AmCryptoProvider.java

License:Open Source License

/**
 * Diese Methode fllt ein Byte-Array mit dem Wert 0x80 und mehreren 0x00
 * bis die Lnge des bergebenen Byte-Array ein Vielfaches der Blocklnge
 * ist. Dies ist die ISO9797-1 Padding-Methode 2 bzw. ISO7816d4-Padding
 * //from   w ww  . j a v  a2 s.c  o  m
 * @param data
 *            Das Byte-Array welches aufgefllt werden soll.
 * @return Das gefllte 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;
}

From source file:de.tsenger.animamea.crypto.AmDESCrypto.java

License:Open Source License

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);
}

From source file:de.tsenger.animamea.crypto.AmDESCrypto.java

License:Open Source License

@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);//from  w  ww.j  a  v  a 2s . c o  m
    mac.update(n, 0, n.length);

    byte[] out = new byte[8];

    mac.doFinal(out, 0);

    return out;
}

From source file:de.tsenger.animamea.crypto.AmDESCrypto.java

License:Open Source License

@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);/*from w w w.j a  va 2s .  c  o m*/
    mac.update(data, 0, data.length);

    byte[] out = new byte[8];

    mac.doFinal(out, 0);

    return out;
}

From source file:my.adam.smo.common.SymmetricEncryptionBox.java

License:Open Source License

public byte[] encrypt(byte[] plainText, byte[] key) {
    if (key.length != 32) {
        throw new IllegalArgumentException("key have to be 32 bytes long (256 bits)");
    }//from  w  w w  . java  2s . c  om

    byte[] seed = new byte[seedLength];
    secureRandom.nextBytes(seed);
    byte[] seededPlainText = addSeedToMessage(plainText, seed);

    byte[] out = seededPlainText.clone();

    byte[] iv = new byte[ivLength];
    secureRandom.nextBytes(iv);

    CipherParameters cp = new ParametersWithIV(new KeyParameter(key), iv);

    PaddedBufferedBlockCipher encCipher;
    encCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()), new ISO7816d4Padding());
    encCipher.init(true, cp);

    encCipher.processBytes(seededPlainText, 0, seededPlainText.length, out, 0);
    return appendIV(out, iv);
}

From source file:org.cryptacular.spec.BufferedBlockCipherSpec.java

License:Open Source License

/**
 * Gets a instance of block cipher padding from a padding name string.
 *
 * @param  padding  Name of padding algorithm.
 *
 * @return  Block cipher padding instance.
 *///  w w  w.  j a  v  a 2  s  .  c o  m
private static BlockCipherPadding getPadding(final String padding) {
    final String name;
    final int pIndex = padding.indexOf("Padding");
    if (pIndex > -1) {
        name = padding.substring(0, pIndex);
    } else {
        name = padding;
    }

    BlockCipherPadding blockCipherPadding;
    if ("ISO7816d4".equalsIgnoreCase(name) | "ISO7816".equalsIgnoreCase(name)) {
        blockCipherPadding = new ISO7816d4Padding();
    } else if ("ISO10126".equalsIgnoreCase(name) || "ISO10126-2".equalsIgnoreCase(name)) {
        blockCipherPadding = new ISO10126d2Padding();
    } else if ("PKCS7".equalsIgnoreCase(name) || "PKCS5".equalsIgnoreCase(name)) {
        blockCipherPadding = new PKCS7Padding();
    } else if ("TBC".equalsIgnoreCase(name)) {
        blockCipherPadding = new TBCPadding();
    } else if ("X923".equalsIgnoreCase(name)) {
        blockCipherPadding = new X923Padding();
    } else if ("NULL".equalsIgnoreCase(name) || "Zero".equalsIgnoreCase(name)
            || "None".equalsIgnoreCase(name)) {
        blockCipherPadding = new ZeroBytePadding();
    } else {
        throw new IllegalArgumentException("Invalid padding " + padding);
    }
    return blockCipherPadding;
}

From source file:org.cryptomator.crypto.aes256.AesSivCipherUtil.java

License:Open Source License

/**
 * First bit 1, following bits 0./*from  w w  w. j  a  va  2 s  .  c o m*/
 */
private static byte[] pad(byte[] in) {
    final byte[] result = Arrays.copyOf(in, 16);
    new ISO7816d4Padding().addPadding(result, in.length);
    return result;
}