List of usage examples for org.bouncycastle.crypto.paddings PKCS7Padding PKCS7Padding
PKCS7Padding
From source file:org.ourfilesystem.security.SecurityTools.java
License:Open Source License
public static BBytes encodePublicPost(String raw, PublicKeySet pubkey) { try {//from w ww . j a v a 2 s . c o m byte strb[] = raw.getBytes(Charset.forName("UTF-16BE")); byte[] key = new byte[32]; Random.nextBytes(key); KeyParameter kp = new KeyParameter(key); CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(aes, new PKCS7Padding()); cipher.init(true, kp); byte[] output = new byte[cipher.getOutputSize(strb.length)]; int len = cipher.processBytes(strb, 0, strb.length, output, 0); cipher.doFinal(output, len); RSAEngine eng = new RSAEngine(); byte headblk[] = new byte[(2 * Long.SIZE / Byte.SIZE) + key.length]; ByteBuffer bbuf = ByteBuffer.wrap(headblk); bbuf.putLong(0x01234567); //magic number bbuf.putLong(0x76543210); //magic number System.arraycopy(key, 0, headblk, (2 * Long.SIZE / Byte.SIZE), key.length); PKCS1Encoding enc = new PKCS1Encoding(eng); enc.init(true, (CipherParameters) pubkey.getPublicEncryptionKey()); byte enchead[] = enc.processBlock(headblk, 0, headblk.length); byte rslt[] = new byte[(Integer.SIZE / Byte.SIZE) + enchead.length + output.length]; ByteBuffer rbuf = ByteBuffer.wrap(rslt); rbuf.putInt(enchead.length); System.arraycopy(enchead, 0, rslt, Integer.SIZE / Byte.SIZE, enchead.length); System.arraycopy(output, 0, rslt, (Integer.SIZE / Byte.SIZE) + enchead.length, output.length); BBytes orslt = new BBytes(rslt); return orslt; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.sandrob.KeyStoreUtils.ExportImport.java
License:Apache License
private static byte[] process(byte[] input, String password, boolean forEncryption) throws Exception { try {/*from w w w .j a v a 2 s . co m*/ // generate key with iteration from password, salt SecretKeyFactory f = SecretKeyFactory.getInstance(KeyFactoryType, CryptoProvider); KeySpec ks = new PBEKeySpec(password.toCharArray(), getSaltByteArray(), KeyGenIterations, KeyLength); SecretKey s = f.generateSecret(ks); Key k = new SecretKeySpec(s.getEncoded(), KeyAlgorithm); CipherParameters cipherParameters = new KeyParameter(k.getEncoded()); BlockCipher blockCipher = new AESEngine(); BlockCipherPadding blockCipherPadding = new PKCS7Padding(); BufferedBlockCipher bufferedBlockCipher = new PaddedBufferedBlockCipher(blockCipher, blockCipherPadding); // initialize the chiper bufferedBlockCipher.init(forEncryption, cipherParameters); int inputOffset = 0; int inputLength = input.length; int maximumOutputLength = bufferedBlockCipher.getOutputSize(inputLength); byte[] output = new byte[maximumOutputLength]; int outputOffset = 0; int outputLength = 0; int bytesProcessed; // process input buffer bytesProcessed = bufferedBlockCipher.processBytes(input, inputOffset, inputLength, output, outputOffset); outputOffset += bytesProcessed; outputLength += bytesProcessed; // process the last block bytesProcessed = bufferedBlockCipher.doFinal(output, outputOffset); outputOffset += bytesProcessed; outputLength += bytesProcessed; if (outputLength == output.length) { return output; } else { byte[] truncatedOutput = new byte[outputLength]; System.arraycopy(output, 0, truncatedOutput, 0, outputLength); return truncatedOutput; } } catch (Exception ex) { Log.e(TAG, ex.getMessage()); throw new Exception(ex); } }
From source file:org.springframework.security.crypto.encrypt.BouncyCastleAesCbcBytesEncryptor.java
License:Apache License
@Override public byte[] encrypt(byte[] bytes) { byte[] iv = this.ivGenerator.generateKey(); PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding()); blockCipher.init(true, new ParametersWithIV(secretKey, iv)); byte[] encrypted = process(blockCipher, bytes); return iv != null ? concatenate(iv, encrypted) : encrypted; }
From source file:org.springframework.security.crypto.encrypt.BouncyCastleAesCbcBytesEncryptor.java
License:Apache License
@Override public byte[] decrypt(byte[] encryptedBytes) { byte[] iv = subArray(encryptedBytes, 0, this.ivGenerator.getKeyLength()); encryptedBytes = subArray(encryptedBytes, this.ivGenerator.getKeyLength(), encryptedBytes.length); PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding()); blockCipher.init(false, new ParametersWithIV(secretKey, iv)); return process(blockCipher, encryptedBytes); }
From source file:piecework.security.concrete.ExampleBouncyCastleEncryptionService.java
License:Educational Community License
private BufferedBlockCipher cipher() { BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); cipher.reset();/* w w w. j a v a2 s. c o m*/ return cipher; }
From source file:snodes.net.Packet.java
License:Open Source License
/** * Encrypts the packet using the given key. * * @param bytes//from w ww .j a va 2 s .c o m * The packet data. * @param key * The packet key. * @return * The encrypted packet data. * @throws IllegalArgumentException * If <tt>key</tt> is too short. */ private static byte[] encrypt(byte[] bytes, Key key) throws IllegalArgumentException { logger.fine("Encrypting packet with key " + key); KeyParameter fishkey = new KeyParameter(key.toByteArray()); TwofishEngine twofish = new TwofishEngine(); int blocksize = twofish.getBlockSize(); int blocks = (int) Math.ceil((double) bytes.length / (double) blocksize); twofish.init(true, fishkey); // true = encrypt byte[] encrypted = new byte[blocks * blocksize]; // Leave room for padding for (int i = 0; i < blocks; i++) { byte[] block = new byte[blocksize]; int offset = i * blocksize; int written = 0; // Pad if not enough bytes -- otherwise, exception is thrown if (bytes.length - offset < blocksize) { BlockCipherPadding pad = new PKCS7Padding(); int padLen = bytes.length - offset; byte[] write = new byte[blocksize]; int padding = 0; System.arraycopy(bytes, offset, write, 0, padLen); pad.init(new SecureRandom()); padding = pad.addPadding(write, padLen); written = twofish.processBlock(write, 0, block, 0); } else { written = twofish.processBlock(bytes, offset, block, 0); } System.arraycopy(block, 0, encrypted, offset, written); } return encrypted; }
From source file:us.exultant.ahs.crypto.bc.AesCtrPkcs7Sha1.java
License:Open Source License
public AesCtrPkcs7Sha1() { // build the system $cipher = new PaddedBufferedBlockCipher(new SICBlockCipher(new AESEngineMod()), // i've decided to frown upon CBC because of the bug i noticed with IVs in that code. new PKCS7Padding()); $hmac = new HMac(new SHA1Digest()); }