List of usage examples for org.bouncycastle.crypto.params ParametersWithIV ParametersWithIV
public ParametersWithIV(CipherParameters parameters, byte[] iv, int ivOff, int ivLen)
From source file:com.licel.jcardsim.crypto.SymmetricSignatureImpl.java
License:Apache License
public void init(Key theKey, byte theMode, byte[] bArray, short bOff, short bLen) throws CryptoException { if (theKey == null) { CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY); }/*from www .ja v a2 s .c o m*/ if (!theKey.isInitialized()) { CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY); } if (!(theKey instanceof SymmetricKeyImpl)) { CryptoException.throwIt(CryptoException.ILLEGAL_VALUE); } CipherParameters cipherParams = null; BlockCipher cipher = ((SymmetricKeyImpl) theKey).getCipher(); if (bArray == null) { cipherParams = ((SymmetricKeyImpl) theKey).getParameters(); } else { if (bLen != cipher.getBlockSize()) { CryptoException.throwIt(CryptoException.ILLEGAL_VALUE); } cipherParams = new ParametersWithIV(((SymmetricKeyImpl) theKey).getParameters(), bArray, bOff, bLen); } switch (algorithm) { case ALG_DES_MAC4_NOPAD: engine = new CBCBlockCipherMac(cipher, 32, null); break; case ALG_DES_MAC8_NOPAD: engine = new CBCBlockCipherMac(cipher, 64, null); break; case ALG_DES_MAC4_ISO9797_M1: engine = new CBCBlockCipherMac(cipher, 32, new ZeroBytePadding()); break; case ALG_DES_MAC8_ISO9797_M1: engine = new CBCBlockCipherMac(cipher, 64, new ZeroBytePadding()); break; case ALG_DES_MAC4_ISO9797_M2: engine = new CBCBlockCipherMac(cipher, 32, new ISO7816d4Padding()); break; case ALG_DES_MAC8_ISO9797_M2: engine = new CBCBlockCipherMac(cipher, 64, new ISO7816d4Padding()); break; case ALG_DES_MAC8_ISO9797_1_M2_ALG3: engine = new ISO9797Alg3Mac(new DESEngine(), 64, new ISO7816d4Padding()); break; case ALG_DES_MAC4_PKCS5: engine = new CBCBlockCipherMac(cipher, 32, new PKCS7Padding()); break; case ALG_DES_MAC8_PKCS5: engine = new CBCBlockCipherMac(cipher, 64, new PKCS7Padding()); break; case ALG_AES_MAC_128_NOPAD: engine = new CBCBlockCipherMac(cipher, 128, null); break; case ALG_HMAC_SHA1: engine = new HMac(new SHA1Digest()); break; case ALG_HMAC_SHA_256: engine = new HMac(new SHA256Digest()); break; case ALG_HMAC_SHA_384: engine = new HMac(new SHA384Digest()); break; case ALG_HMAC_SHA_512: engine = new HMac(new SHA512Digest()); break; case ALG_HMAC_MD5: engine = new HMac(new MD5Digest()); break; case ALG_HMAC_RIPEMD160: engine = new HMac(new RIPEMD160Digest()); break; default: CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM); break; } engine.init(cipherParams); isInitialized = true; }
From source file:com.raphfrk.craftproxyliter.LocalSocket.java
License:Open Source License
public void setAES() { BufferedBlockCipher in = new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 8)); in.init(false, new ParametersWithIV(new KeyParameter(this.ptc.getSecretKey().getEncoded()), this.ptc.getSecretKey().getEncoded(), 0, 16)); BufferedBlockCipher out = new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 8)); out.init(true, new ParametersWithIV(new KeyParameter(this.ptc.getSecretKey().getEncoded()), this.ptc.getSecretKey().getEncoded(), 0, 16)); this.in = new DataInputStream(new CipherInputStream(this.in, in)); this.out = new DataOutputStream(new CipherOutputStream(this.out, out)); pin = new ProtocolInputStream(this.in, 255 * 16 * 1024); pout = new ProtocolOutputStream(this.out); }
From source file:com.vmware.admiral.common.security.EncryptorService.java
License:Open Source License
private BufferedBlockCipher getCipher(boolean forEncryption) { BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); cipher.init(forEncryption, new ParametersWithIV( new KeyParameter(keyBytes, IV_LENGTH, keyBytes.length - IV_LENGTH), keyBytes, 0, IV_LENGTH)); return cipher; }
From source file:eg.nileu.cis.nilestore.cryptography.AESCipher.java
License:Open Source License
/** * Instantiates a new aES cipher.//from w w w . ja va2s . co m * * @param encryptionKey * the encryption key * @param forEncryption * the for encryption */ public AESCipher(byte[] encryptionKey, boolean forEncryption) { byte[] ivBytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; AESFastEngine baseengine = new AESFastEngine(); cipher = new SICBlockCipher(baseengine); cipher.init(forEncryption, new ParametersWithIV(new KeyParameter(encryptionKey), ivBytes, 0, ivBytes.length)); prev_padding = 0; blockSize = cipher.getBlockSize(); }
From source file:eg.nileu.cis.nilestore.cryptography.AESCipher.java
License:Open Source License
/** * Instantiates a new aES cipher./*from w w w .jav a 2 s .com*/ * * @param encryptionKey * the encryption key * @param IV * the iV * @param forEncryption * the for encryption */ public AESCipher(byte[] encryptionKey, byte[] IV, boolean forEncryption) { AESFastEngine baseengine = new AESFastEngine(); cipher = new SICBlockCipher(baseengine); cipher.init(forEncryption, new ParametersWithIV(new KeyParameter(encryptionKey), IV, 0, IV.length)); prev_padding = 0; blockSize = cipher.getBlockSize(); }
From source file:freemail.OutboundContact.java
License:Open Source License
/** * Set up an outbound contact. Fetch the mailsite, generate a new SSK keypair and post an RTS message to the appropriate KSK. * Will block for mailsite retrieval and RTS insertion * * @return true for success/* w ww. j a v a 2s .com*/ */ private boolean init() throws ConnectionTerminatedException, InterruptedException { Logger.normal(this, "Initialising Outbound Contact " + address.toString()); // try to fetch get all necessary info. will fetch mailsite / generate new keys if necessary String initialslot = this.getCurrentLowestSlot(); SSKKeyPair commssk = this.getCommKeyPair(); if (commssk == null) return false; SSKKeyPair ackssk = this.getAckKeyPair(); RSAKeyParameters their_pub_key = this.getPubKey(); if (their_pub_key == null) return false; String rtsksk = this.getRtsKsk(); if (rtsksk == null) return false; StringBuffer rtsmessage = new StringBuffer(); // the public part of the SSK keypair we generated rtsmessage.append("commssk=" + commssk.pubkey + "\r\n"); rtsmessage.append("ackssk=" + ackssk.privkey + "\r\n"); rtsmessage.append("initialslot=" + initialslot + "\r\n"); rtsmessage.append("messagetype=rts\r\n"); // must include who this RTS is to, otherwise we're vulnerable to surreptitious forwarding rtsmessage.append("to=" + this.address.getSubDomain() + "\r\n"); // get our mailsite URI String our_mailsite_uri = account.getProps().get("mailsite.pubkey"); rtsmessage.append("mailsite=" + our_mailsite_uri + "\r\n"); rtsmessage.append("\r\n"); //FreemailLogger.normal(this,rtsmessage.toString()); // sign the message SHA256Digest sha256 = new SHA256Digest(); sha256.update(rtsmessage.toString().getBytes(), 0, rtsmessage.toString().getBytes().length); byte[] hash = new byte[sha256.getDigestSize()]; sha256.doFinal(hash, 0); RSAKeyParameters our_priv_key = AccountManager.getPrivateKey(account.getProps()); AsymmetricBlockCipher sigcipher = new RSAEngine(); sigcipher.init(true, our_priv_key); byte[] sig = null; try { sig = sigcipher.processBlock(hash, 0, hash.length); } catch (InvalidCipherTextException icte) { Logger.error(this, "Failed to RSA encrypt hash: " + icte.getMessage()); icte.printStackTrace(); return false; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { bos.write(rtsmessage.toString().getBytes()); bos.write(sig); } catch (IOException ioe) { ioe.printStackTrace(); return false; } // make up a symmetric key PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); // quick paranoia check! if (aescipher.getBlockSize() != AES_BLOCK_LENGTH) { // bouncycastle must have changed their implementation, so // we're in trouble Logger.normal(this, "Incompatible block size change detected in cryptography API! Are you using a newer version of the bouncycastle libraries? If so, we suggest you downgrade for now, or check for a newer version of Freemail."); return false; } byte[] aes_iv_and_key = this.getAESParams(); // now encrypt that with our recipient's public key AsymmetricBlockCipher enccipher = new RSAEngine(); enccipher.init(true, their_pub_key); byte[] encrypted_aes_params = null; try { encrypted_aes_params = enccipher.processBlock(aes_iv_and_key, 0, aes_iv_and_key.length); } catch (InvalidCipherTextException icte) { Logger.error(this, "Failed to perform asymmertic encryption on RTS symmetric key: " + icte.getMessage()); icte.printStackTrace(); return false; } // now encrypt the message with the symmetric key KeyParameter kp = new KeyParameter(aes_iv_and_key, aescipher.getBlockSize(), AES_KEY_LENGTH); ParametersWithIV kpiv = new ParametersWithIV(kp, aes_iv_and_key, 0, aescipher.getBlockSize()); aescipher.init(true, kpiv); byte[] encmsg = new byte[aescipher.getOutputSize(bos.toByteArray().length) + encrypted_aes_params.length]; System.arraycopy(encrypted_aes_params, 0, encmsg, 0, encrypted_aes_params.length); int offset = encrypted_aes_params.length; offset += aescipher.processBytes(bos.toByteArray(), 0, bos.toByteArray().length, encmsg, offset); try { aescipher.doFinal(encmsg, offset); } catch (InvalidCipherTextException icte) { Logger.error(this, "Failed to perform symmertic encryption on RTS data: " + icte.getMessage()); icte.printStackTrace(); return false; } // insert it! HighLevelFCPClient cli = new HighLevelFCPClient(); if (cli.slotInsert(encmsg, "KSK@" + rtsksk + "-" + DateStringFactory.getKeyString(), 1, "") < 0) { // safe to copy the message into the contact outbox though return false; } // remember the fact that we have successfully inserted the rts this.contactfile.put("status", "rts-sent"); // and remember when we sent it! this.contactfile.put("rts-sent-at", Long.toString(System.currentTimeMillis())); // and since that's been successfully inserted to that key, we can // throw away the symmetric key this.contactfile.remove("aesparams"); Logger.normal(this, "Succesfully initialised Outbound Contact"); return true; }
From source file:freemail.RTSFetcher.java
License:Open Source License
private byte[] decrypt_rts(File rtsmessage) throws IOException, InvalidCipherTextException { // initialise our ciphers RSAKeyParameters ourprivkey = AccountManager.getPrivateKey(account.getProps()); AsymmetricBlockCipher deccipher = new RSAEngine(); deccipher.init(false, ourprivkey);//w w w . ja v a2 s . c o m PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); // first n bytes will be an encrypted RSA block containting the // AES IV and Key. Read that. byte[] encrypted_params = new byte[deccipher.getInputBlockSize()]; FileInputStream fis = new FileInputStream(rtsmessage); int read = 0; while (read < encrypted_params.length) { read += fis.read(encrypted_params, read, encrypted_params.length - read); if (read < 0) break; } if (read < 0) { throw new InvalidCipherTextException("RTS Message too short"); } byte[] aes_iv_and_key = deccipher.processBlock(encrypted_params, 0, encrypted_params.length); KeyParameter kp = new KeyParameter(aes_iv_and_key, aescipher.getBlockSize(), aes_iv_and_key.length - aescipher.getBlockSize()); ParametersWithIV kpiv = new ParametersWithIV(kp, aes_iv_and_key, 0, aescipher.getBlockSize()); try { aescipher.init(false, kpiv); } catch (IllegalArgumentException iae) { throw new InvalidCipherTextException(iae.getMessage()); } byte[] plaintext = new byte[aescipher.getOutputSize((int) rtsmessage.length() - read)]; int ptbytes = 0; while (read < rtsmessage.length()) { byte[] buf = new byte[(int) rtsmessage.length() - read]; int thisread = fis.read(buf, 0, (int) rtsmessage.length() - read); ptbytes += aescipher.processBytes(buf, 0, thisread, plaintext, ptbytes); read += thisread; } fis.close(); try { aescipher.doFinal(plaintext, ptbytes); } catch (DataLengthException dle) { throw new InvalidCipherTextException(dle.getMessage()); } return plaintext; }
From source file:nl.owlstead.jscl.bouncy.PKCS5S2_SHA256_ParametersGenerator.java
License:Open Source License
/** * Generate a key with initialisation vector parameter derived from the * password, salt, and iteration count we are currently initialised with. * /*from w ww . ja v a 2s. c o m*/ * @param keySize * the size of the key we want (in bits) * @param ivSize * the size of the iv we want (in bits) * @return a ParametersWithIV object. */ public CipherParameters generateDerivedParameters(int keySize, int ivSize) { keySize = keySize / 8; ivSize = ivSize / 8; byte[] dKey = generateDerivedKey(keySize + ivSize); return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), dKey, keySize, ivSize); }
From source file:org.freenetproject.freemail.RTSFetcher.java
License:Open Source License
private byte[] decrypt_rts(File rtsmessage) throws IOException, InvalidCipherTextException { // initialise our ciphers RSAKeyParameters ourprivkey = AccountManager.getPrivateKey(account.getProps()); AsymmetricBlockCipher deccipher = new RSAEngine(); deccipher.init(false, ourprivkey);/*from www.jav a2 s . c om*/ PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); // first n bytes will be an encrypted RSA block containting the // AES IV and Key. Read that. byte[] encrypted_params = new byte[deccipher.getInputBlockSize()]; int read = 0; FileInputStream fis = new FileInputStream(rtsmessage); try { while (read < encrypted_params.length) { read += fis.read(encrypted_params, read, encrypted_params.length - read); if (read < 0) break; } if (read < 0) { fis.close(); throw new InvalidCipherTextException("RTS Message too short"); } byte[] aes_iv_and_key = deccipher.processBlock(encrypted_params, 0, encrypted_params.length); KeyParameter kp = new KeyParameter(aes_iv_and_key, aescipher.getBlockSize(), aes_iv_and_key.length - aescipher.getBlockSize()); ParametersWithIV kpiv = new ParametersWithIV(kp, aes_iv_and_key, 0, aescipher.getBlockSize()); try { aescipher.init(false, kpiv); } catch (IllegalArgumentException iae) { fis.close(); throw new InvalidCipherTextException(iae.getMessage()); } byte[] plaintext = new byte[aescipher.getOutputSize((int) rtsmessage.length() - read)]; int ptbytes = 0; while (read < rtsmessage.length()) { byte[] buf = new byte[(int) rtsmessage.length() - read]; int thisread = fis.read(buf, 0, (int) rtsmessage.length() - read); ptbytes += aescipher.processBytes(buf, 0, thisread, plaintext, ptbytes); read += thisread; } try { aescipher.doFinal(plaintext, ptbytes); } catch (DataLengthException dle) { throw new InvalidCipherTextException(dle.getMessage()); } return plaintext; } finally { fis.close(); } }
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; }