當前位置: 首頁>>代碼示例>>Java>>正文


Java CBCBlockCipher類代碼示例

本文整理匯總了Java中org.bouncycastle.crypto.modes.CBCBlockCipher的典型用法代碼示例。如果您正苦於以下問題:Java CBCBlockCipher類的具體用法?Java CBCBlockCipher怎麽用?Java CBCBlockCipher使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


CBCBlockCipher類屬於org.bouncycastle.crypto.modes包,在下文中一共展示了CBCBlockCipher類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: decrypt

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
@Override
    public String decrypt(byte[] encrypted) {
//        Cipher cipher = null;
        String plain;
        try {
//            Security.addProvider(new BouncyCastlePQCProvider());
//            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastlePQCProvider());
//            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(encryptionKey, "AES"), new IvParameterSpec(iv));
//            plain = new String(cipher.doFinal(encrypted), "UTF-8");
            KeyParameter keyParam = new KeyParameter(encryptionKey);
            CipherParameters params = new ParametersWithIV(keyParam, iv);
            BlockCipherPadding padding = new PKCS7Padding();
            BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                    new CBCBlockCipher(new AESEngine()), padding);
            cipher.reset();
            cipher.init(false, params);
            byte[] buffer = new byte[cipher.getOutputSize(encrypted.length)];
            int len = cipher.processBytes(encrypted, 0, encrypted.length, buffer, 0);
            len += cipher.doFinal(buffer, len);
            byte[] out = Arrays.copyOfRange(buffer, 0, len);
            plain = new String(out, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException("decrypt error in SimpleAesManaged", e);
        }
        return plain;
    }
 
開發者ID:timerickson,項目名稱:lastpass-java,代碼行數:27,代碼來源:SimpleAesManaged.java

示例2: EncryptAes256

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
private static byte[] EncryptAes256(byte[] data, byte[] encryptionKey)
{
    try {
        KeyParameter keyParam = new KeyParameter(encryptionKey);
        BlockCipherPadding padding = new PKCS7Padding();
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                new CBCBlockCipher(new AESEngine()), padding);
        cipher.reset();
        cipher.init(true, keyParam);
        byte[] buffer = new byte[cipher.getOutputSize(data.length)];
        int len = cipher.processBytes(data, 0, data.length, buffer, 0);
        len += cipher.doFinal(buffer, len);
        return Arrays.copyOfRange(buffer, 0, len);
    } catch (Exception e) {
        throw new RuntimeException("decrypt error in SimpleAesManaged", e);
    }
}
 
開發者ID:timerickson,項目名稱:lastpass-java,代碼行數:18,代碼來源:ParserHelperTest.java

示例3: testEncryptRijndael

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
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;
}
 
開發者ID:David-Hackro,項目名稱:ExamplesAndroid,代碼行數:18,代碼來源:Metodos.java

示例4: encryptDESFile

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
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;
  }
 
開發者ID:PacktPublishing,項目名稱:Spring-MVC-Blueprints,代碼行數:17,代碼來源:UploadEncryptFileController.java

示例5: BlockCipherMac

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * create a standard MAC based on a block cipher with the size of the
 * MAC been given in bits.
 * <p>
 * Note: the size of the MAC must be at least 16 bits (FIPS Publication 113),
 * and in general should be less than the size of the block cipher as it reduces
 * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
 *
 * @param cipher the cipher to be used as the basis of the MAC generation.
 * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8.
 * @deprecated use CBCBlockCipherMac
 */
public BlockCipherMac(
    BlockCipher     cipher,
    int             macSizeInBits)
{
    if ((macSizeInBits % 8) != 0)
    {
        throw new IllegalArgumentException("MAC size must be multiple of 8");
    }

    this.cipher = new CBCBlockCipher(cipher);
    this.macSize = macSizeInBits / 8;

    mac = new byte[cipher.getBlockSize()];

    buf = new byte[cipher.getBlockSize()];
    bufOff = 0;
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:30,代碼來源:BlockCipherMac.java

示例6: CBCBlockCipherMac

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * create a standard MAC based on a block cipher with the size of the
 * MAC been given in bits. This class uses CBC mode as the basis for the
 * MAC generation.
 * <p>
 * Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
 * or 16 bits if being used as a data authenticator (FIPS Publication 113),
 * and in general should be less than the size of the block cipher as it reduces
 * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
 *
 * @param cipher the cipher to be used as the basis of the MAC generation.
 * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8.
 * @param padding the padding to be used to complete the last block.
 */
public CBCBlockCipherMac(
    BlockCipher         cipher,
    int                 macSizeInBits,
    BlockCipherPadding  padding)
{
    if ((macSizeInBits % 8) != 0)
    {
        throw new IllegalArgumentException("MAC size must be multiple of 8");
    }

    this.cipher = new CBCBlockCipher(cipher);
    this.padding = padding;
    this.macSize = macSizeInBits / 8;

    mac = new byte[cipher.getBlockSize()];

    buf = new byte[cipher.getBlockSize()];
    bufOff = 0;
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:34,代碼來源:CBCBlockCipherMac.java

示例7: ISO9797Alg3Mac

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * create a standard MAC based on a block cipher with the size of the
 * MAC been given in bits. This class uses single DES CBC mode as the basis for the
 * MAC generation. The final block is decrypted and then encrypted using the
 * middle and right part of the key.
 * <p>
 * Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
 * or 16 bits if being used as a data authenticator (FIPS Publication 113),
 * and in general should be less than the size of the block cipher as it reduces
 * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
 *
 * @param cipher the cipher to be used as the basis of the MAC generation.
 * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8.
 * @param padding the padding to be used to complete the last block.
 */
public ISO9797Alg3Mac(
    BlockCipher         cipher,
    int                 macSizeInBits,
    BlockCipherPadding  padding)
{
    if ((macSizeInBits % 8) != 0)
    {
        throw new IllegalArgumentException("MAC size must be multiple of 8");
    }

    if (!(cipher instanceof DESEngine))
    {
        throw new IllegalArgumentException("cipher must be instance of DESEngine");
    }

    this.cipher = new CBCBlockCipher(cipher);
    this.padding = padding;
    this.macSize = macSizeInBits / 8;

    mac = new byte[cipher.getBlockSize()];

    buf = new byte[cipher.getBlockSize()];
    bufOff = 0;
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:40,代碼來源:ISO9797Alg3Mac.java

示例8: aesEncrypt

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
public static byte[] aesEncrypt(byte[] plaintext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) {
    try {
        byte[] dhSharedSecret = new byte[32];
        Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey);
        for (int i = 0; i < 32; i++) {
            dhSharedSecret[i] ^= nonce[i];
        }
        byte[] key = sha256().digest(dhSharedSecret);
        byte[] iv = new byte[16];
        secureRandom.get().nextBytes(iv);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
                new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(true, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(plaintext.length)];
        int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
        ciphertextLength += aes.doFinal(output, ciphertextLength);
        byte[] result = new byte[iv.length + ciphertextLength];
        System.arraycopy(iv, 0, result, 0, iv.length);
        System.arraycopy(output, 0, result, iv.length, ciphertextLength);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
開發者ID:muhatzg,項目名稱:burstcoin,代碼行數:26,代碼來源:Crypto.java

示例9: encrypt

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * Encrypt.
 *
 * @param instr the instr
 * @return the string
 * @throws java.security.GeneralSecurityException the general security exception
 */
@Override
public String encrypt(String instr) throws GeneralSecurityException {
    long t1 = System.currentTimeMillis();
    byte[] in = instr.getBytes();
    PaddedBufferedBlockCipher encryptor = new PaddedBufferedBlockCipher(
            new CBCBlockCipher(new DESedeEngine()));
    encryptor.init(true, keyParameter);
    byte[] cipherText = new byte[encryptor.getOutputSize(in.length)];
    int outputLen = encryptor.processBytes(in, 0, in.length, cipherText, 0);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        encryptor.doFinal(cipherText, outputLen);
        Hex.encode(cipherText, os);
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeneralSecurityException(e);
    }
    long t2 = System.currentTimeMillis();
    logger.debug("Time taken to encrypt(millis) :" + (t2 - t1));
    return ENC_PREFIX + os.toString();
}
 
開發者ID:oneops,項目名稱:oneops,代碼行數:29,代碼來源:CmsCryptoDES.java

示例10: decryptStr

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
private String decryptStr(String instr) throws GeneralSecurityException {
    if(StringUtils.isEmpty(instr)){
        return instr;
    }
    long t1 = System.currentTimeMillis();
    PaddedBufferedBlockCipher decryptor = new PaddedBufferedBlockCipher(
            new CBCBlockCipher(new DESedeEngine()));
    decryptor.init(false, keyParameter);
    byte[] in = null;
    byte[] cipherText = null;

    try {
    	in = Hex.decode(instr);
    	cipherText = new byte[decryptor.getOutputSize(in.length)];

     int outputLen = decryptor.processBytes(in, 0, in.length, cipherText, 0);
        decryptor.doFinal(cipherText, outputLen);
    } catch (Exception e) {
        throw new GeneralSecurityException(e);
    }
    long t2 = System.currentTimeMillis();
    logger.debug("Time taken to decrypt(millis) : " + (t2 - t1));
    return (new String(cipherText)).replaceAll("\\u0000+$", "");
}
 
開發者ID:oneops,項目名稱:oneops,代碼行數:25,代碼來源:CmsCryptoDES.java

示例11: aesEncrypt

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
public static byte[] aesEncrypt(byte[] plaintext, byte[] myPrivateKey, byte[] theirPublicKey) {
    try {
        byte[] dhSharedSecret = new byte[32];
        Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey);
        byte[] key = sha256().digest(dhSharedSecret);
        byte[] iv = new byte[16];
        secureRandom.get().nextBytes(iv);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
                new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(true, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(plaintext.length)];
        int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
        ciphertextLength += aes.doFinal(output, ciphertextLength);
        byte[] result = new byte[iv.length + ciphertextLength];
        System.arraycopy(iv, 0, result, 0, iv.length);
        System.arraycopy(output, 0, result, iv.length, ciphertextLength);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
開發者ID:de-luxe,項目名稱:burstcoin-faucet,代碼行數:23,代碼來源:Crypto.java

示例12: aesDecrypt

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] myPrivateKey, byte theirPublicKey[]) {
    try {
        if (ivCiphertext.length < 16 || ivCiphertext.length % 16 != 0) {
            throw new InvalidCipherTextException("invalid ciphertext");
        }
        byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16);
        byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length);
        byte[] dhSharedSecret = new byte[32];
        Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey);
        byte[] key = sha256().digest(dhSharedSecret);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
                new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(false, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(ciphertext.length)];
        int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0);
        plaintextLength += aes.doFinal(output, plaintextLength);
        byte[] result = new byte[plaintextLength];
        System.arraycopy(output, 0, result, 0, result.length);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
開發者ID:de-luxe,項目名稱:burstcoin-faucet,代碼行數:25,代碼來源:Crypto.java

示例13: doCbc

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
private void doCbc(byte[] key, byte[] iv, byte[] pt, byte[] expected)
    throws Exception
{
    PaddedBufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(new SerpentEngine()), new PKCS7Padding());

    byte[] ct = new byte[expected.length];

    c.init(true, new ParametersWithIV(new KeyParameter(key), iv));

    int l = c.processBytes(pt, 0, pt.length, ct, 0);

    c.doFinal(ct, l);

    if (!Arrays.areEqual(expected, ct))
    {
        fail("CBC test failed");
    }
}
 
開發者ID:ttt43ttt,項目名稱:gwt-crypto,代碼行數:19,代碼來源:SerpentTest.java

示例14: encryptBlock

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
private void encryptBlock(byte[] key, byte[] iv, byte[] cekBlock)
{
    BlockCipher engine = new CBCBlockCipher(new DESEngine());

    engine.init(true, new ParametersWithIV(new KeyParameter(key), iv));

    for (int i = 0; i < cekBlock.length; i += 8)
    {
        engine.processBlock(cekBlock, i, cekBlock, i);
    }

    for (int i = 0; i < cekBlock.length; i += 8)
    {
        engine.processBlock(cekBlock, i, cekBlock, i);
    }
}
 
開發者ID:ttt43ttt,項目名稱:gwt-crypto,代碼行數:17,代碼來源:RFC3211WrapTest.java

示例15: InitCiphers

import org.bouncycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
public void InitCiphers() {
    // create the ciphers
    // AES block cipher in CBC mode with padding
    encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
            new AESEngine()));

    decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
            new AESEngine()));

    // create the IV parameter
    ParametersWithIV parameterIV = new ParametersWithIV(new KeyParameter(
            key), IV);

    encryptCipher.init(true, parameterIV);
    decryptCipher.init(false, parameterIV);
}
 
開發者ID:kiwitechnologies,項目名稱:SecurityAndroid,代碼行數:17,代碼來源:BouncyCastleAPI_AES_CBC.java


注:本文中的org.bouncycastle.crypto.modes.CBCBlockCipher類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。