Example usage for org.bouncycastle.crypto.engines DESedeEngine DESedeEngine

List of usage examples for org.bouncycastle.crypto.engines DESedeEngine DESedeEngine

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.engines DESedeEngine DESedeEngine.

Prototype

public DESedeEngine() 

Source Link

Document

standard constructor.

Usage

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

License:Apache License

/**
 * Return the BouncyCastle <code>BlockCipher</code> for using with this key
 * @return <code>BlockCipher</code> for this key, or null for HMACKey
 * @throws CryptoException if key not initialized
 * @see BlockCipher//from   ww w  . j a  v a2  s. c om
 */
public BlockCipher getCipher() throws CryptoException {
    if (!key.isInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }
    BlockCipher cipher = null;
    switch (type) {
    case KeyBuilder.TYPE_DES:
    case KeyBuilder.TYPE_DES_TRANSIENT_DESELECT:
    case KeyBuilder.TYPE_DES_TRANSIENT_RESET:
        if (size == KeyBuilder.LENGTH_DES) {
            cipher = new DESEngine();
        }
        if (size == KeyBuilder.LENGTH_DES3_2KEY || size == KeyBuilder.LENGTH_DES3_3KEY) {
            cipher = new DESedeEngine();
        }
        break;
    case KeyBuilder.TYPE_AES:
    case KeyBuilder.TYPE_AES_TRANSIENT_DESELECT:
    case KeyBuilder.TYPE_AES_TRANSIENT_RESET:
        cipher = new AESEngine();
        break;
    }
    return cipher;
}

From source file:com.oneops.cms.crypto.CmsCryptoDES.java

License:Apache License

/**
 * Encrypt./*from   w w  w  . j  a  va2 s  . com*/
 *
 * @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();
}

From source file:com.oneops.cms.crypto.CmsCryptoDES.java

License:Apache License

private String decryptStr(String instr) throws GeneralSecurityException {
    if (StringUtils.isEmpty(instr)) {
        return instr;
    }/*  w  w w.  j a v  a  2  s. c o m*/
    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+$", "");
}

From source file:com.zotoh.crypto.BCOfuscator.java

License:Open Source License

private String decrypt(String encrypted) throws Exception {
    if (isEmpty(encrypted)) {
        return encrypted;
    }/*from  www .  ja v  a  2  s.co m*/
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
    byte[] p = Base64.decodeBase64(encrypted), out = new byte[1024];
    ByteOStream baos = new ByteOStream();
    int c;

    // initialise the cipher with the key bytes, for encryption
    cipher.init(false, new KeyParameter(getKey()));
    c = cipher.processBytes(p, 0, p.length, out, 0);
    if (c > 0) {
        baos.write(out, 0, c);
    }

    c = cipher.doFinal(out, 0);
    if (c > 0) {
        baos.write(out, 0, c);
    }

    return asString(baos.asBytes());
}

From source file:com.zotoh.crypto.BCOfuscator.java

License:Open Source License

private String encrypt(String clearText) throws Exception {
    if (isEmpty(clearText)) {
        return clearText;
    }//  w  w w.  j  a  v  a 2  s  .  c  o m
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
    ByteOStream baos = new ByteOStream();
    byte[] p = asBytes(clearText), out = new byte[4096];
    int c;

    // initialise the cipher with the key bytes, for encryption
    cipher.init(true, new KeyParameter(getKey()));
    c = cipher.processBytes(p, 0, p.length, out, 0);
    if (c > 0) {
        baos.write(out, 0, c);
    }

    c = cipher.doFinal(out, 0);
    if (c > 0) {
        baos.write(out, 0, c);
    }

    return Base64.encodeBase64String(baos.asBytes());
}

From source file:de.carne.certmgr.store.provider.bouncycastle.BouncyCastleStoreProvider.java

License:Open Source License

@Override
public byte[] encodePKCS12(X509Certificate[] crtChain, KeyPair key, PKCS10Object csr, X509CRL crl,
        PasswordCallback password, String resource) throws IOException, PasswordRequiredException {
    String passwordInput = (password != null ? password.queryPassword(resource) : null);

    if (password != null && passwordInput == null) {
        throw new PasswordRequiredException("Password input cancelled while writing PKCS#12 file");
    }/*from  www.jav a  2  s  .  c  om*/

    PKCS12SafeBagBuilder[] crtBagBuilders = new PKCS12SafeBagBuilder[crtChain != null ? crtChain.length : 0];
    DERBMPString crt0FriendlyName = null;
    SubjectKeyIdentifier subjectKeyIdentifier = null;

    if (crtChain != null) {
        int crtIndex = 0;

        for (X509Certificate crt : crtChain) {
            PKCS12SafeBagBuilder crtBagBuilder = crtBagBuilders[crtIndex] = new JcaPKCS12SafeBagBuilder(crt);
            DERBMPString crtFriendlyName = new DERBMPString(crt.getSubjectX500Principal().toString());

            crtBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, crtFriendlyName);
            if (crtIndex == 0) {
                crt0FriendlyName = crtFriendlyName;
                try {
                    JcaX509ExtensionUtils extensionUtils = new JcaX509ExtensionUtils();

                    subjectKeyIdentifier = extensionUtils.createSubjectKeyIdentifier(crt.getPublicKey());
                } catch (NoSuchAlgorithmException e) {
                    throw new StoreProviderException(e);
                }
            }
            crtIndex++;
        }
    }

    PKCS12SafeBagBuilder keyBagBuilder = null;

    if (key != null) {
        if (passwordInput != null) {
            BcPKCS12PBEOutputEncryptorBuilder keyBagEncryptorBuilder = new BcPKCS12PBEOutputEncryptorBuilder(
                    PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC,
                    new CBCBlockCipher(new DESedeEngine()));
            OutputEncryptor keyBagEncrypter = keyBagEncryptorBuilder.build(passwordInput.toCharArray());

            keyBagBuilder = new JcaPKCS12SafeBagBuilder(key.getPrivate(), keyBagEncrypter);
        } else {
            keyBagBuilder = new JcaPKCS12SafeBagBuilder(key.getPrivate());
        }
        if (crtBagBuilders.length > 0) {
            crtBagBuilders[0].addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, subjectKeyIdentifier);
            keyBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, subjectKeyIdentifier);
            keyBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, crt0FriendlyName);
        }
    }

    PKCS12SafeBag[] crtBags = new PKCS12SafeBag[crtBagBuilders.length];
    int crtBagIndex = 0;

    for (PKCS12SafeBagBuilder crtBagBuilder : crtBagBuilders) {
        crtBags[crtBagIndex] = crtBagBuilder.build();
        crtBagIndex++;
    }

    PKCS12PfxPduBuilder pkcs12Builder = new PKCS12PfxPduBuilder();

    if (passwordInput != null) {
        BcPKCS12PBEOutputEncryptorBuilder crtBagEncryptorBuilder = new BcPKCS12PBEOutputEncryptorBuilder(
                PKCSObjectIdentifiers.pbeWithSHAAnd40BitRC2_CBC, new CBCBlockCipher(new RC2Engine()));
        OutputEncryptor crtBagEncryptor = crtBagEncryptorBuilder.build(passwordInput.toCharArray());

        pkcs12Builder.addEncryptedData(crtBagEncryptor, crtBags);
    } else {
        for (PKCS12SafeBag crtBag : crtBags) {
            pkcs12Builder.addData(crtBag);
        }
    }
    if (keyBagBuilder != null) {
        pkcs12Builder.addData(keyBagBuilder.build());
    }

    PKCS12PfxPdu pkcs12;

    try {
        if (passwordInput != null) {
            pkcs12 = pkcs12Builder.build(new BcPKCS12MacCalculatorBuilder(), passwordInput.toCharArray());
        } else {
            pkcs12 = pkcs12Builder.build(null, null);
        }
    } catch (PKCSException e) {
        throw new StoreProviderException(e);
    }
    return pkcs12.getEncoded();
}

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

/**
 * Dekodiert einen Block mit DES/*from  w w  w  .  j a  va2  s  .  c  o m*/
 * 
 * @param key
 *            Byte-Array enthlt den 3DES-Schlssel
 * @param z
 *            verschlsselter Block
 * @return entschlsselter block
 */
@Override
public byte[] decryptBlock(byte[] key, byte[] z) {
    byte[] s = new byte[16];
    KeyParameter encKey = new KeyParameter(key);
    BlockCipher cipher = new DESedeEngine();
    cipher.init(false, encKey);
    cipher.processBlock(z, 0, s, 0);
    return s;
}

From source file:edu.biu.scapi.primitives.prf.bc.BcTripleDES.java

License:Open Source License

/**
 * Passes the DesedeEngine of BC to the abstract super class
 */
public BcTripleDES() {

    super(new DESedeEngine());
}

From source file:edu.biu.scapi.primitives.prf.bc.BcTripleDES.java

License:Open Source License

/**
 * Receives random object to use./*w  w w. j a  v  a 2  s .  c  om*/
 * Passes it and the DesedeEngine of BC to the abstract super class.
 * @param random SecureRandom to use
 */
public BcTripleDES(SecureRandom random) {

    super(new DESedeEngine(), random);
}