List of usage examples for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher
public CBCBlockCipher(BlockCipher cipher)
From source file:com.oneops.cms.crypto.CmsCryptoDES.java
License:Apache License
/** * Encrypt.//w w w . j a va 2 s .c o m * * @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 ww .jav a 2s . com 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.smedia.sqzserver.common.util.SerialNumber.java
public byte[] decrypt(String cookieValue, byte[] keyBytes) throws InvalidKeyException, UnsupportedEncodingException { byte[] encryptedData; byte[] decryptedData; TwofishEngine t = new TwofishEngine(); // /////////////////// BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine())); cipher.init(false, new KeyParameter(keyBytes)); // create the key // byte[] keyBytes = key.getBytes(); // Object keyObject = twofish.makeKey(keyBytes, 16); // make the length of the string a multiple of // the block size while (cookieValue.length() < (32 * 8 / 5)) { cookieValue += "A"; }//from ww w . j av a 2 s .c om // initialize byte arrays that will hold encrypted/decrypted // text encryptedData = sorensonBase32Decode(cookieValue); decryptedData = new byte[cookieValue.length() * 2]; /* * int oLen = cipher.processBytes(encryptedData, 0, * encryptedData.length, decryptedData, 0); try { * cipher.doFinal(decryptedData, oLen); } catch (CryptoException ce) { * ce.printStackTrace(); } */ // String test = new String(rv).trim(); // /// encryptedData = sorensonBase32Decode(cookieValue); decryptedData = new byte[cookieValue.length() * 2]; // Iterate over the byte arrays by 16-byte blocks and decrypt. for (int i = 0; i < Array.getLength(encryptedData); i += 16) { // twofish.decrypt(encryptedData, i, decryptedData, i, keyObject, // 16); cipher.processBytes(encryptedData, i, 16, decryptedData, 0); } return decryptedData; }
From source file:com.sos.CredentialStore.KeePass.pl.sind.keepass.crypto.BcAESCipher.java
License:Apache License
@Override public byte[] decrypt(final byte[] key, final byte[] data, final byte[] iv) throws CipherException { BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); if (iv != null) { cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv)); } else {//from w w w .j a v a 2 s.c o m cipher.init(false, new KeyParameter(key)); } byte[] decoded = new byte[cipher.getOutputSize(data.length)]; int out = cipher.processBytes(data, 0, data.length, decoded, 0); try { out += cipher.doFinal(decoded, out); if (out < decoded.length) { decoded = Arrays.copyOf(decoded, out); } } catch (DataLengthException e) { // we are padding so shouldn happen throw new CipherException("Invalid data lenght", e); } catch (IllegalStateException e) { throw new CipherException("Decrypting error", e); } catch (InvalidCipherTextException e) { throw new CipherException("Unable to decrypt data", e); } return decoded; }
From source file:com.sos.CredentialStore.KeePass.pl.sind.keepass.crypto.BcAESCipher.java
License:Apache License
@Override public byte[] encrypt(final byte[] key, final byte[] data, final byte[] iv, final int rounds, final boolean padding) throws CipherException { BufferedBlockCipher cipher = null;/*from w w w .ja va 2 s . c om*/ if (padding) { cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); } else { cipher = new BufferedBlockCipher(new AESEngine()); } if (iv != null) { cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv)); } else { cipher.init(true, new KeyParameter(key)); } byte[] encoded = null; if (padding) { encoded = new byte[cipher.getOutputSize(data.length)]; } else { encoded = new byte[data.length]; } int out = cipher.processBytes(data, 0, data.length, encoded, 0); if (rounds > 1) { for (int i = 1; i < rounds; i++) { out = cipher.processBytes(encoded, 0, encoded.length, encoded, 0); } } try { if (padding && out < encoded.length) { cipher.doFinal(encoded, out); } } catch (DataLengthException e) { // we are padding so shouldn happen throw new CipherException("Invalid data lenght", e); } catch (IllegalStateException e) { throw new CipherException("Decrypting error", e); } catch (InvalidCipherTextException e) { throw new CipherException("Unable to decrypt data", e); } return encoded; }
From source file:com.thecorpora.qbo.androidapk.AESCipher.java
License:Open Source License
public String encrypt(byte configData[], String key) throws Exception { byte[] encryptedConfigData = null; AESEngine blockCipher = new AESEngine(); blockCipher.reset();//from w w w . j a v a2s.c o m CBCBlockCipher cbcCipher = new CBCBlockCipher(blockCipher); BufferedBlockCipher bbc = new PaddedBufferedBlockCipher(cbcCipher); byte[] salt = new byte[8]; SecureRandom secure = new SecureRandom(); secure.nextBytes(salt); //intialising in the encryption mode with Key and IV bbc.init(true, getKeyParamWithIv(key, salt)); byte[] encryptedData = new byte[bbc.getOutputSize(configData.length)]; //process array of bytes int noOfBytes = bbc.processBytes(configData, 0, configData.length, encryptedData, 0); //process the last block in the buffer bbc.doFinal(encryptedData, noOfBytes); ByteArrayOutputStream bos = new ByteArrayOutputStream(); //writing encrypted data along with the salt in the format readable by open ssl api bos.write("Salted__".getBytes()); bos.write(salt); bos.write(encryptedData); encryptedConfigData = bos.toByteArray(); bos.close(); // return encryptedConfigData; return Base64.encodeToString(encryptedConfigData, Base64.DEFAULT); }
From source file:com.thoughtworks.go.security.DESEncrypter.java
License:Apache License
private static String decrypt(byte[] key, String cipherText) throws CryptoException { try {//from www. ja v a 2 s . c om PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine())); cipher.init(false, new KeyParameter(key)); byte[] cipherTextBytes = DECODER.decode(cipherText); byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)]; int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0); cipher.doFinal(plainTextBytes, outputLength); int paddingStarts = plainTextBytes.length - 1; for (; paddingStarts >= 0; paddingStarts--) { if (plainTextBytes[paddingStarts] != 0) { break; } } return new String(plainTextBytes, 0, paddingStarts + 1); } catch (Exception e) { throw new CryptoException(e); } }
From source file:com.thoughtworks.go.security.DESEncrypter.java
License:Apache License
private static String encrypt(byte[] key, String plainText) throws CryptoException { try {//from ww w . ja v a 2s . c om PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine())); KeyParameter keyParameter = new KeyParameter(key); cipher.init(true, keyParameter); byte[] plainTextBytes = plainText.getBytes(); byte[] cipherTextBytes = new byte[cipher.getOutputSize(plainTextBytes.length)]; int outputLength = cipher.processBytes(plainTextBytes, 0, plainTextBytes.length, cipherTextBytes, 0); cipher.doFinal(cipherTextBytes, outputLength); return ENCODER.encodeToString(cipherTextBytes).trim(); } catch (Exception e) { throw new CryptoException(e); } }
From source file:com.thoughtworks.go.security.GoCipher.java
License:Apache License
String cipher(byte[] key, String plainText) throws InvalidCipherTextException { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine())); KeyParameter keyParameter = new KeyParameter(Hex.decode(key)); cipher.init(true, keyParameter);//w w w . j av a2s .co m byte[] plainTextBytes = plainText.getBytes(); byte[] cipherTextBytes = new byte[cipher.getOutputSize(plainTextBytes.length)]; int outputLength = cipher.processBytes(plainTextBytes, 0, plainTextBytes.length, cipherTextBytes, 0); cipher.doFinal(cipherTextBytes, outputLength); return Base64.encodeBase64String(cipherTextBytes).trim(); }
From source file:com.thoughtworks.go.security.GoCipher.java
License:Apache License
String decipher(byte[] key, String cipherText) throws InvalidCipherTextException { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine())); cipher.init(false, new KeyParameter(Hex.decode(key))); byte[] cipherTextBytes = Base64.decodeBase64(cipherText); byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)]; int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0); cipher.doFinal(plainTextBytes, outputLength); int paddingStarts = plainTextBytes.length - 1; for (; paddingStarts >= 0; paddingStarts--) { if (plainTextBytes[paddingStarts] != 0) { break; }//from w w w .j a va 2s.c o m } return new String(plainTextBytes, 0, paddingStarts + 1); }