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

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

Introduction

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

Prototype

ZeroBytePadding

Source Link

Usage

From source file:com.android.isoma.enc.Encryption.java

License:Open Source License

public synchronized byte[] encrypt(byte[] data)
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    byte[] iv = null;
    if (data == null || data.length == 0) {
        return new byte[0];
    }//w w w .  j a v  a 2  s  .c o m
    BufferedBlockCipher encryptcipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new ZeroBytePadding());

    String source = "00000000";
    iv = source.getBytes();

    CipherParameters ivAndKey = new ParametersWithIV(key, iv);
    encryptcipher.init(true, ivAndKey);

    return callCipher(encryptcipher, data);
}

From source file:com.android.isoma.enc.Encryption.java

License:Open Source License

public synchronized byte[] decrypt(byte[] data)
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    byte[] iv = null;
    if (data == null || data.length == 0) {
        return new byte[0];
    }//from ww  w.ja va 2  s .c o m

    BufferedBlockCipher decryptcipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new ZeroBytePadding());

    String source = "00000000";
    iv = source.getBytes();

    CipherParameters ivAndKey = new ParametersWithIV(key, iv);

    decryptcipher.init(false, ivAndKey);

    return callCipher(decryptcipher, data);
}

From source file:com.googlecode.jsendnsca.encryption.AESEncryptor.java

License:Apache License

public void encrypt(byte[] passiveCheckBytes, byte[] initVector, String password) {
    RijndaelEngine engine = new RijndaelEngine(_keyByteLength * 8);
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(engine, 8),
            new ZeroBytePadding());

    try {//from  w  w w  .j  av  a2s  .  co m
        byte[] sessionKey = new byte[_keyByteLength];
        byte[] passwordBytes = password.getBytes("US-ASCII");
        System.arraycopy(passwordBytes, 0, sessionKey, 0, Math.min(_keyByteLength, passwordBytes.length));

        byte[] iv = new byte[_keyByteLength];
        System.arraycopy(initVector, 0, iv, 0, Math.min(_keyByteLength, initVector.length));

        cipher.init(true, new ParametersWithIV(new KeyParameter(sessionKey), iv));

        byte[] cipherText = new byte[cipher.getOutputSize(passiveCheckBytes.length)];
        int cipherLength = cipher.processBytes(passiveCheckBytes, 0, passiveCheckBytes.length, cipherText, 0);
        cipherLength = cipherLength + cipher.doFinal(cipherText, cipherLength);

        int bytesToCopy = Math.min(passiveCheckBytes.length, cipherLength);
        System.arraycopy(cipherText, 0, passiveCheckBytes, 0, bytesToCopy);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.googlecode.jsendnsca.encryption.BlowfishEncryptor.java

License:Apache License

@Override
public void encrypt(final byte[] passiveCheckBytes, final byte[] initVector, final String password) {

    final BlowfishEngine engine = new BlowfishEngine();
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(engine, 8),
            new ZeroBytePadding());

    try {// w w w  . ja  va  2s .c  om
        final byte[] passwordBytes = password.getBytes("US-ASCII");

        assertValidPasswordBytesLength(passwordBytes);

        final byte[] sessionKey = new byte[KEY_BYTES_LENGTH];
        System.arraycopy(passwordBytes, 0, sessionKey, 0, Math.min(KEY_BYTES_LENGTH, passwordBytes.length));

        final byte[] iv = new byte[KEY_BYTES_LENGTH];
        System.arraycopy(initVector, 0, iv, 0, Math.min(KEY_BYTES_LENGTH, initVector.length));

        cipher.init(true, new ParametersWithIV(new KeyParameter(sessionKey), iv));

        final byte[] cipherText = new byte[cipher.getOutputSize(passiveCheckBytes.length)];
        int cipherLength = cipher.processBytes(passiveCheckBytes, 0, passiveCheckBytes.length, cipherText, 0);
        cipherLength = cipherLength + cipher.doFinal(cipherText, cipherLength);

        final int bytesToCopy = Math.min(passiveCheckBytes.length, cipherLength);
        System.arraycopy(cipherText, 0, passiveCheckBytes, 0, bytesToCopy);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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   ww w. j  a v a 2  s  . c  om*/
    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 .j a  va2s.c  o  m*/
    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:com.rcythr.masq.util.AES.java

License:Open Source License

/**
 * Uses inputs to encrypt/decrypt based on the value of encrypt
 * @param forEncryption if true encrypt, if false decrypt
 * @param input the data to work with//from w ww  . j av a2  s  . c o  m
 * @param key the key to use
 * @return the result
 * 
 * @throws InvalidCipherTextException if something goes wrong
 */
public static byte[] handle(boolean forEncryption, byte[] input, byte[] key) throws InvalidCipherTextException {
    CipherParameters cipherParameters = new KeyParameter(key);
    BufferedBlockCipher bufferedBlockCipher = new PaddedBufferedBlockCipher(new AESEngine(),
            new ZeroBytePadding());

    bufferedBlockCipher.init(forEncryption, cipherParameters);

    int inputOffset = 0;
    int inputLength = input.length;

    int maximumOutputLength = bufferedBlockCipher.getOutputSize(inputLength);
    byte[] output = new byte[maximumOutputLength];
    int outputOffset = 0;
    int outputLength = 0;

    int bytesProcessed;

    bytesProcessed = bufferedBlockCipher.processBytes(input, inputOffset, inputLength, output, outputOffset);
    outputOffset += bytesProcessed;
    outputLength += bytesProcessed;

    bytesProcessed = bufferedBlockCipher.doFinal(output, outputOffset);
    outputOffset += bytesProcessed;
    outputLength += bytesProcessed;

    if (outputLength == output.length) {
        return output;
    } else {
        byte[] truncatedOutput = new byte[outputLength];
        System.arraycopy(output, 0, truncatedOutput, 0, outputLength);
        return truncatedOutput;
    }
}

From source file:de.gs_sys.kp2016.crypto.SymmetricCipher.java

License:Open Source License

public static boolean decryptFile(byte[] password, String filenameIn, String filenameOut)
        throws DecryptionException {

    byte[] key = extendKey(password);

    try (InputStream inputStream = new FileInputStream(filenameIn)) {
        OutputStream outputStream = new FileOutputStream(filenameOut);

        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),
                new ZeroBytePadding());
        cipher.init(false, new KeyParameter(key));

        byte[] buffer = new byte[BUFFER_SPACE];
        int readSize;
        int writeSize;

        byte[] out;

        while ((readSize = inputStream.read(buffer)) != -1) {
            writeSize = cipher.getOutputSize(buffer.length);
            out = new byte[writeSize];

            writeSize = cipher.processBytes(buffer, 0, readSize, out, 0);
            outputStream.write(out, 0, writeSize);
        }//from  ww w .j av  a2 s  .c o  m
        outputStream.flush();
        try {
            out = new byte[1024];
            writeSize = cipher.doFinal(out, 0);
            outputStream.write(out, 0, writeSize);
            outputStream.flush();
        } catch (Exception ce) {
            ce.printStackTrace();
            throw new DecryptionException(SymmetricCipher.class.getName());
        }

        outputStream.close();
        inputStream.close();
        return true;
    } catch (FileNotFoundException e) {

    } catch (IOException e1) {

    }
    return false;
}

From source file:de.gs_sys.kp2016.crypto.SymmetricCipher.java

License:Open Source License

public static boolean encryptFile(byte[] password, String filenameIn, String filenameOut) {

    byte[] key = extendKey(password);

    try (InputStream inputStream = new FileInputStream(filenameIn)) {
        OutputStream outputStream = new FileOutputStream(filenameOut);

        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),
                new ZeroBytePadding());
        cipher.init(true, new KeyParameter(key));

        byte[] buffer = new byte[BUFFER_SPACE];
        int readSize;
        int writeSize;

        byte[] out;

        while ((readSize = inputStream.read(buffer)) != -1) {
            writeSize = cipher.getOutputSize(buffer.length);
            out = new byte[writeSize];

            writeSize = cipher.processBytes(buffer, 0, readSize, out, 0);

            outputStream.write(out, 0, writeSize);
        }//from w ww .  j ava2 s . co  m

        try {
            out = new byte[1024];
            writeSize = cipher.doFinal(out, 0);
            outputStream.write(out, 0, writeSize);
            outputStream.flush();
        } catch (Exception ce) {
            ce.printStackTrace();
            return false;
        }
        outputStream.close();
        inputStream.close();
        return true;
    } catch (FileNotFoundException e) {

    } catch (IOException e1) {

    }
    return false;
}

From source file:org.apache.zeppelin.user.Encryptor.java

License:Apache License

public Encryptor(String encryptKey) {
    encryptCipher = new PaddedBufferedBlockCipher(new AESEngine(), new ZeroBytePadding());
    encryptCipher.init(true, new KeyParameter(encryptKey.getBytes()));

    decryptCipher = new PaddedBufferedBlockCipher(new AESEngine(), new ZeroBytePadding());
    decryptCipher.init(false, new KeyParameter(encryptKey.getBytes()));
}