Example usage for org.bouncycastle.crypto.modes CFBBlockCipher CFBBlockCipher

List of usage examples for org.bouncycastle.crypto.modes CFBBlockCipher CFBBlockCipher

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.modes CFBBlockCipher CFBBlockCipher.

Prototype

public CFBBlockCipher(BlockCipher cipher, int bitBlockSize) 

Source Link

Document

Basic constructor.

Usage

From source file:me.grapebaba.hyperledger.fabric.Crypto.java

License:Apache License

public ByteString eciesDecrypt(PrivateKey recipientPrivateKey, ByteString cipherText) {
    BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) recipientPrivateKey;
    ECNamedCurveSpec ecNamedCurveSpec = (ECNamedCurveSpec) bcecPrivateKey.getParams();
    int level = SecurityLevel.from(ecNamedCurveSpec.getName()).size();

    //cipherText = ephemeralPubKeyBytes + encryptedTokBytes + macBytes
    //ephemeralPubKeyBytes = first ((384+7)/8)*2 + 1 bytes = first 97 bytes
    //hmac is sha3_384 = 48 bytes or sha3_256 = 32 bytes
    int ephemeralPubKeyLength = ((level + 7) / 8) * 2 + 1;
    int hmacLength = level >> 3;
    int cipherTextLength = cipherText.size();

    if (cipherTextLength <= ephemeralPubKeyLength + hmacLength)
        throw new RuntimeException(String.format("Illegal cipherText length: %d must be > %d", cipherTextLength,
                ephemeralPubKeyLength + hmacLength));

    ByteString ephemeralPubKey = cipherText.substring(0, ephemeralPubKeyLength);
    ByteString encryptedContent = cipherText.substring(ephemeralPubKeyLength, cipherTextLength - hmacLength);
    ByteString hmac = cipherText.substring(cipherTextLength - hmacLength);

    ECPrivateKeyParameters ecdhPrivateKeyParameters;
    try {//w w w  .  ja  va  2s  .  c o  m
        ecdhPrivateKeyParameters = (ECPrivateKeyParameters) (PrivateKeyFactory
                .createKey(bcecPrivateKey.getEncoded()));
    } catch (IOException e) {
        logger.error("ECIES decrypt load private key exception", e);
        throw new RuntimeException(e);
    }
    ECDomainParameters ecDomainParameters = ecdhPrivateKeyParameters.getParameters();
    ECCurve ecCurve = ecDomainParameters.getCurve();
    ECPublicKeyParameters ecPublicKeyParameters = new ECPublicKeyParameters(
            ecCurve.decodePoint(ephemeralPubKey.toByteArray()), ecDomainParameters);
    BasicAgreement agree = new ECDHBasicAgreement();
    agree.init(ecdhPrivateKeyParameters);
    byte[] keyAgreement = agree.calculateAgreement(ecPublicKeyParameters).toByteArray();

    HKDFParameters hkdfParameters = new HKDFParameters(keyAgreement, null, null);
    HKDFBytesGenerator hkdfBytesGenerator = new HKDFBytesGenerator(digest);
    hkdfBytesGenerator.init(hkdfParameters);
    byte[] hkdfOutputBytes = new byte[AESKEY_LENGTH + HMACKEY_LENGTH];
    hkdfBytesGenerator.generateBytes(hkdfOutputBytes, 0, AESKEY_LENGTH + HMACKEY_LENGTH);
    ByteString hkdfOutput = ByteString.copyFrom(hkdfOutputBytes);
    ByteString aesKey = hkdfOutput.substring(0, AESKEY_LENGTH);
    ByteString hmacKey = hkdfOutput.substring(AESKEY_LENGTH, AESKEY_LENGTH + HMACKEY_LENGTH);
    HMac hMac = new HMac(digest);
    hMac.init(new KeyParameter(hmacKey.toByteArray()));
    hMac.update(encryptedContent.toByteArray(), 0, encryptedContent.size());
    byte[] recoveredHmac = new byte[hMac.getMacSize()];
    hMac.doFinal(recoveredHmac, 0);
    if (!MessageDigest.isEqual(hmac.toByteArray(), recoveredHmac)) {
        throw new RuntimeException("HMAC verify failed");
    }

    CFBBlockCipher aesCipher = new CFBBlockCipher(new AESEngine(), BLOCK_BIT_SIZE);
    ByteString iv = encryptedContent.substring(0, IV_LENGTH);
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(aesKey.toByteArray()), iv.toByteArray());
    aesCipher.init(false, ivAndKey);
    byte[] decryptedBytes = new byte[500];
    aesCipher.decryptBlock(encryptedContent.substring(IV_LENGTH).toByteArray(), 0, decryptedBytes, 0);
    return ByteString.copyFrom(decryptedBytes);
}

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

License:Open Source License

/**
 * Creates a new buffered block cipher from the specification in this
 * instance.//from w  w w  .  j ava2 s . c o m
 *
 * @return  New buffered block cipher instance.
 */
@Override
public BufferedBlockCipher newInstance() {
    BlockCipher cipher = getBlockCipherSpec().newInstance();

    switch (mode) {

    case "CBC":
        cipher = new CBCBlockCipher(cipher);
        break;

    case "OFB":
        cipher = new OFBBlockCipher(cipher, cipher.getBlockSize());
        break;

    case "CFB":
        cipher = new CFBBlockCipher(cipher, cipher.getBlockSize());
        break;

    default:
        break;
    }

    if (padding != null) {
        return new PaddedBufferedBlockCipher(cipher, getPadding(padding));
    }
    return new BufferedBlockCipher(cipher);
}

From source file:org.cryptacular.util.CipherUtilTest.java

License:Open Source License

@DataProvider(name = "block-cipher")
public Object[][] getBlockCipherData() {
    return new Object[][] { new Object[] {
            // Plaintext is NOT multiple of block size
            "Able was I ere I saw elba.", new CBCBlockCipher(new AESEngine()), new RBGNonce(16), },
            // Plaintext is multiple of block size
            new Object[] { "Four score and seven years ago, our forefathers ",
                    new CBCBlockCipher(new BlowfishEngine()), new RBGNonce(8), },
            // OFB
            new Object[] { "Have you passed through this night?", new OFBBlockCipher(new BlowfishEngine(), 64),
                    new LongCounterNonce(), },
            // CFB
            new Object[] {
                    "I went to the woods because I wished to live deliberately, to "
                            + "front only the essential facts of life",
                    new CFBBlockCipher(new AESEngine(), 128), new RBGNonce(16), }, };
}

From source file:org.spout.api.security.SecurityHandler.java

License:Open Source License

private BufferedBlockCipher addSymmetricWrapper(BlockCipher rawCipher, String wrapper) {
    if (wrapper.startsWith("CFB")) {
        int bits;
        try {//ww w.j av  a 2 s  .c  o m
            bits = Integer.parseInt(wrapper.substring(3));
        } catch (NumberFormatException e) {
            Spout.getLogger().info("Unable to parse bits for CFB wrapper from: " + wrapper);
            return null;
        }
        return new BufferedBlockCipher(new CFBBlockCipher(rawCipher, bits));
    }

    return new BufferedBlockCipher(rawCipher);
}

From source file:ovh.tgrhavoc.aibot.protocol.EncryptionUtil.java

License:Open Source License

public static BufferedBlockCipher createBlockCipher(SecretKey key, boolean out) {
    BufferedBlockCipher blockCipher = new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 8));
    blockCipher.init(out, new ParametersWithIV(new KeyParameter(key.getEncoded()), key.getEncoded(), 0, 16));
    return blockCipher;
}

From source file:shadowsocks.crypto.AESCrypto.java

License:Apache License

protected StreamBlockCipher getCipher(boolean isEncrypted) throws CryptoException {
    AESEngine engine = new AESEngine();
    StreamBlockCipher cipher;// w ww  .  j av a2  s  .c o m

    if (mName.equals(CIPHER_AES_128_CFB)) {
        cipher = new CFBBlockCipher(engine, getIVLength() * 8);
    } else if (mName.equals(CIPHER_AES_192_CFB)) {
        cipher = new CFBBlockCipher(engine, getIVLength() * 8);
    } else if (mName.equals(CIPHER_AES_256_CFB)) {
        cipher = new CFBBlockCipher(engine, getIVLength() * 8);
    } else if (mName.equals(CIPHER_AES_128_OFB)) {
        cipher = new OFBBlockCipher(engine, getIVLength() * 8);
    } else if (mName.equals(CIPHER_AES_192_OFB)) {
        cipher = new OFBBlockCipher(engine, getIVLength() * 8);
    } else if (mName.equals(CIPHER_AES_256_OFB)) {
        cipher = new OFBBlockCipher(engine, getIVLength() * 8);
    } else {
        throw new CryptoException("Invalid AlgorithmParameter: " + mName);
    }

    return cipher;
}

From source file:simpleserver.stream.Encryption.java

License:Open Source License

public BufferedBlockCipher getStreamCipher(boolean out) {
    BufferedBlockCipher cipher = new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 8));
    cipher.init(out,/*from   w w w  .  j a va2s.co m*/
            new ParametersWithIV(new KeyParameter(sharedKey.getEncoded()), sharedKey.getEncoded(), 0, 16));
    return cipher;
}