org.bouncycastle.crypto.BlockCipher Java Examples

The following examples show how to use org.bouncycastle.crypto.BlockCipher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CredStashBouncyCastleCrypto.java    From jcredstash with Apache License 2.0 6 votes vote down vote up
private byte[] encryptOrDecrypt(byte[] key, byte[] contents, boolean forEncryption) {

        // Credstash uses standard AES
        BlockCipher engine = new AESFastEngine();

        // Credstash uses CTR mode
        StreamBlockCipher cipher = new SICBlockCipher(engine);

        cipher.init(forEncryption, new ParametersWithIV(new KeyParameter(key), INITIALIZATION_VECTOR));

        byte[] resultBytes = new byte[contents.length];
        int contentsOffset = 0;
        int resultOffset = 0;
        cipher.processBytes(contents, contentsOffset, contents.length, resultBytes, resultOffset);
        return resultBytes;
    }
 
Example #2
Source File: DPAESCBCCipher.java    From InflatableDonkey with MIT License 6 votes vote down vote up
DPAESCBCCipher(
        BlockCipher cipher,
        int blockLength, IntFunction<byte[]> ivGenerator,
        KeyParameter key,
        boolean forEncryption,
        int index,
        int offset) {

    this.cipher = Objects.requireNonNull(cipher, "cipher");
    this.blockLength = blockLength;
    this.ivGenerator = ivGenerator;
    this.key = key;
    this.forEncryption = forEncryption;
    this.index = index;
    this.offset = offset;
}
 
Example #3
Source File: UploadEncryptFileController.java    From Spring-MVC-Blueprints with MIT License 6 votes vote down vote up
private byte[] encryptDESFile(String keys, byte[] plainText) {
BlockCipher engine = new DESEngine();

      byte[] key = keys.getBytes();
      byte[] ptBytes = plainText;
      BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
      cipher.init(true, new KeyParameter(key));
      byte[] rv = new byte[cipher.getOutputSize(ptBytes.length)];
      int tam = cipher.processBytes(ptBytes, 0, ptBytes.length, rv, 0);
      try {
          cipher.doFinal(rv, tam);
      } catch (Exception ce) {
          ce.printStackTrace();
      }
      return rv;
  }
 
Example #4
Source File: Metodos.java    From ExamplesAndroid with Apache License 2.0 6 votes vote down vote up
public String testEncryptRijndael(String value,String key) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    BlockCipher engine = new RijndaelEngine(256);
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine), new ZeroBytePadding());

    byte[] keyBytes = key.getBytes();
    cipher.init(true, new KeyParameter(keyBytes));

    byte[] input = value.getBytes();
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];

    int cipherLength = cipher.processBytes(input, 0, input.length, cipherText, 0);
    cipher.doFinal(cipherText, cipherLength);

    String result = new String(Base64.encode(cipherText));
    //Log.e("testEncryptRijndael : " , result);
    return  result;
}
 
Example #5
Source File: BouncyCastleV1CryptoProvider.java    From paseto with MIT License 5 votes vote down vote up
private BufferedBlockCipher ase256CtrCipher(boolean forEncryption, byte[] key, byte[] iv) {
	BlockCipher engine = new AESEngine();
	BufferedBlockCipher cipher = new BufferedBlockCipher(new SICBlockCipher(engine));
	CipherParameters params = new ParametersWithIV(new KeyParameter(key), iv);

	cipher.init(forEncryption, params);
	return cipher;
}
 
Example #6
Source File: XFileKeyFactory.java    From InflatableDonkey with MIT License 5 votes vote down vote up
Supplier<BlockCipher> ciphers(KeyBlob blob) {
    // u3 - 0x00FF0000;
    // experimental
    return (blob.u3() & 0x00FF0000) == 0
            ? DPCipherFactories.AES_CBC
            : DPCipherFactories.AES_XTS;
}
 
Example #7
Source File: XTSTweak.java    From InflatableDonkey with MIT License 5 votes vote down vote up
XTSTweak(BlockCipher cipher, LongFunction<byte[]> tweakFunction, byte[] tweak) {
    this.cipher = Objects.requireNonNull(cipher);
    this.tweakFunction = Objects.requireNonNull(tweakFunction);
    this.tweak = Objects.requireNonNull(tweak);

    if (cipher.getBlockSize() != BLOCK_SIZE) {
        throw new IllegalArgumentException("bad block size: " + cipher.getBlockSize());
    }
}
 
Example #8
Source File: GPCrypto.java    From GlobalPlatformPro with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static byte[] scp03_kdf(byte[] key, byte[] a, byte[] b, int bytes) {
    BlockCipher cipher = new AESEngine();
    CMac cmac = new CMac(cipher);
    KDFCounterBytesGenerator kdf = new KDFCounterBytesGenerator(cmac);
    kdf.init(new KDFCounterParameters(key, a, b, 8)); // counter size is in bits
    byte[] cgram = new byte[bytes];
    kdf.generateBytes(cgram, 0, cgram.length);
    return cgram;
}
 
Example #9
Source File: PseudoRandomFunctionAES.java    From protect with MIT License 5 votes vote down vote up
public PseudoRandomFunctionAES(final PrfKey key)  {
	super(key);

	// Create CMAC instance based on AES
	final BlockCipher cipher = new AESEngine();
    this.cipherMac = new CMac(cipher);
    
    // Initialize with key
    final KeyParameter params = new KeyParameter(key.getKeyBytes());
    cipherMac.init(params);
}
 
Example #10
Source File: Metodos.java    From ExamplesAndroid with Apache License 2.0 5 votes vote down vote up
public String testDecryptRijndael(String value,String key) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
        BlockCipher engine = new RijndaelEngine(256);
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine), new ZeroBytePadding());

        byte[] keyBytes = key.getBytes();
        cipher.init(false, new KeyParameter(keyBytes));

        byte[] output = Base64.decode(value.getBytes());
        byte[] cipherText = new byte[cipher.getOutputSize(output.length)];

        int cipherLength = cipher.processBytes(output, 0, output.length, cipherText, 0);
        int outputLength = cipher.doFinal(cipherText, cipherLength);
        outputLength += cipherLength;

        byte[] resultBytes = cipherText;
        if (outputLength != output.length) {
            resultBytes = new byte[outputLength];
            System.arraycopy(
                    cipherText, 0,
                    resultBytes, 0,
                    outputLength
            );
        }

        String result = new String(resultBytes);
return  result;
    }
 
Example #11
Source File: DownloadDecryptFileController.java    From Spring-MVC-Blueprints with MIT License 5 votes vote down vote up
public byte[] decryptDESFile(String key, byte[] cipherText) {
BlockCipher engine = new DESEngine();
      byte[] bytes = key.getBytes();
      BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
      cipher.init(false, new KeyParameter(bytes));
      byte[] rv = new byte[cipher.getOutputSize(cipherText.length)];
      int tam = cipher.processBytes(cipherText, 0, cipherText.length, rv, 0);
      try {
          cipher.doFinal(rv, tam);
      } catch (Exception ce) {
          ce.printStackTrace();
      }
      return rv;
  }
 
Example #12
Source File: AESCipher.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Creates a new instance of AESCipher */
public AESCipher(boolean forEncryption, byte[] key, byte[] iv) {
    BlockCipher aes = new AESFastEngine();
    BlockCipher cbc = new CBCBlockCipher(aes);
    bp = new PaddedBufferedBlockCipher(cbc);
    KeyParameter kp = new KeyParameter(key);
    ParametersWithIV piv = new ParametersWithIV(kp, iv);
    bp.init(forEncryption, piv);
}
 
Example #13
Source File: GPBouncy.java    From openjavacard-tools with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static byte[] scp03_kdf(byte[] key, byte[] a, byte[] b, int bytes) {
    BlockCipher cipher = new AESEngine();
    CMac cmac = new CMac(cipher);
    KDFCounterBytesGenerator kdf = new KDFCounterBytesGenerator(cmac);
    kdf.init(new KDFCounterParameters(key, a, b, 8));
    byte[] cgram = new byte[bytes];
    kdf.generateBytes(cgram, 0, cgram.length);
    return cgram;
}
 
Example #14
Source File: GPBouncy.java    From openjavacard-tools with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static byte[] scp03_mac(byte[] keybytes, byte[] msg, int lengthBits) {
    // Use BouncyCastle light interface.
    BlockCipher cipher = new AESEngine();
    CMac cmac = new CMac(cipher);
    cmac.init(new KeyParameter(keybytes));
    cmac.update(msg, 0, msg.length);
    byte[] out = new byte[cmac.getMacSize()];
    cmac.doFinal(out, 0);
    return Arrays.copyOf(out, lengthBits / 8);
}
 
Example #15
Source File: GPCrypto.java    From GlobalPlatformPro with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static byte[] scp03_mac(byte[] keybytes, byte[] msg, int lengthBits) {
    // Use BouncyCastle light interface.
    BlockCipher cipher = new AESEngine();
    CMac cmac = new CMac(cipher);
    cmac.init(new KeyParameter(keybytes));
    cmac.update(msg, 0, msg.length);
    byte[] out = new byte[cmac.getMacSize()];
    cmac.doFinal(out, 0);
    return Arrays.copyOf(out, lengthBits / 8);
}
 
Example #16
Source File: FileStreamWriter.java    From InflatableDonkey with MIT License 4 votes vote down vote up
static InputStream decryptStream(InputStream in, XFileKey keyCipher) {
    BlockCipher cipher = keyCipher.ciphers().get();
    cipher.init(false, new KeyParameter(keyCipher.key()));
    return new CipherInputStream(in, new BufferedBlockCipher(cipher));
}
 
Example #17
Source File: CrmfKeyWrapper.java    From xipki with Apache License 2.0 4 votes vote down vote up
/**
 * Encrypt the key with the following output.
 * <pre>
 * ECIES-Ciphertext-Value ::= SEQUENCE {
 *     ephemeralPublicKey ECPoint,
 *     symmetricCiphertext OCTET STRING,
 *     macTag OCTET STRING
 * }
 *
 * ECPoint ::= OCTET STRING
 * </pre>
 */
@Override
public byte[] generateWrappedKey(byte[] keyToWrap) throws OperatorException {
  try {
    BlockCipher cbcCipher = new CBCBlockCipher(new AESEngine());
    IESCipher cipher = new IESCipher(
        new IESEngine(new ECDHBasicAgreement(),
            new KDF2BytesGenerator(new SHA1Digest()),
            new HMac(new SHA1Digest()),
            new PaddedBufferedBlockCipher(cbcCipher)), 16);

    // According to the ยง3.8 in SEC 1, Version 2.0:
    // "Furthermore here the 16 octet or 128 bit IV for AES in CBC mode should always take
    //  the value 0000000000000000_{16}"
    byte[] iv = new byte[16];
    IESParameterSpec spec = new IESParameterSpec(null, null, aesKeySize, aesKeySize, iv);
    cipher.engineInit(Cipher.ENCRYPT_MODE, publicKey, spec, new SecureRandom());
    byte[] bcResult = cipher.engineDoFinal(keyToWrap, 0, keyToWrap.length);
    // convert the result to ASN.1 format
    ASN1Encodable[] array = new ASN1Encodable[3];
    // ephemeralPublicKey ECPoint
    byte[] ephemeralPublicKey = new byte[ephemeralPublicKeyLen];

    System.arraycopy(bcResult, 0, ephemeralPublicKey, 0, ephemeralPublicKeyLen);
    array[0] = new DEROctetString(ephemeralPublicKey);

    // symmetricCiphertext OCTET STRING
    int symmetricCiphertextLen = bcResult.length - ephemeralPublicKeyLen - macLen;
    byte[] symmetricCiphertext = new byte[symmetricCiphertextLen];
    System.arraycopy(bcResult, ephemeralPublicKeyLen,
        symmetricCiphertext, 0, symmetricCiphertextLen);
    array[1] = new DEROctetString(symmetricCiphertext);

    // macTag OCTET STRING
    byte[] macTag = new byte[macLen];
    System.arraycopy(bcResult, ephemeralPublicKeyLen + symmetricCiphertextLen,
        macTag, 0, macLen);
    array[2] = new DEROctetString(macTag);
    return new DERSequence(array).getEncoded();
  } catch (Exception ex) {
    throw new OperatorException("error while generateWrappedKey", ex);
  }
}
 
Example #18
Source File: XFileKeyMutatorFactory.java    From InflatableDonkey with MIT License 4 votes vote down vote up
static Optional<XFileKey> mutateCipher(Optional<XFileKey> key, Supplier<BlockCipher> mode) {
    return key.map(u -> new XFileKey(u.key(), mode, u.flags()));
}
 
Example #19
Source File: XFileKeyMutatorFactory.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public static UnaryOperator<Optional<XFileKey>> create(Supplier<BlockCipher> cipherFactory) {
    return key -> mutateCipher(key, Objects.requireNonNull(cipherFactory, "cipherFactory"));
}
 
Example #20
Source File: XFileKey.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public Supplier<BlockCipher> ciphers() {
    return ciphers;
}
 
Example #21
Source File: XFileKey.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public XFileKey(byte[] key, Supplier<BlockCipher> ciphers) {
    this(key, ciphers, new byte[]{});
}
 
Example #22
Source File: XFileKey.java    From InflatableDonkey with MIT License 4 votes vote down vote up
public XFileKey(byte[] key, Supplier<BlockCipher> ciphers, byte[] flags) {
    this.key = Arrays.copyOf(key, key.length);
    this.ciphers = Objects.requireNonNull(ciphers, "ciphers");
    this.flags = Objects.requireNonNull(flags, "flags");
}
 
Example #23
Source File: XTSTweak.java    From InflatableDonkey with MIT License 4 votes vote down vote up
XTSTweak(BlockCipher cipher, LongFunction<byte[]> tweakFunction) {
    this(cipher, tweakFunction, new byte[cipher.getBlockSize()]);
}
 
Example #24
Source File: XTSCore.java    From InflatableDonkey with MIT License 4 votes vote down vote up
XTSCore(BlockCipher cipher, XTSTweak tweak) {
    this.cipher = Objects.requireNonNull(cipher);
    this.tweak = Objects.requireNonNull(tweak);
}
 
Example #25
Source File: DPAESCBCCipher.java    From InflatableDonkey with MIT License 4 votes vote down vote up
DPAESCBCCipher(BlockCipher cipher, int blockLength) {
    this(cipher, blockLength, null, null, false, 0, 0);
}
 
Example #26
Source File: DPAESCBCBlockIVGenerator.java    From InflatableDonkey with MIT License 4 votes vote down vote up
DPAESCBCBlockIVGenerator(BlockCipher cipher) {
    this.cipher = Objects.requireNonNull(cipher, "cipher");
}