List of usage examples for org.bouncycastle.crypto BlockCipher reset
public void reset();
From source file:jcrypter.JCrypterFrame.java
License:Apache License
/** * Test if the unlimited strength policy files are installed *//* w w w .j a v a 2 s . c o m*/ private void testUnlimitedPolicy() { try { byte[] data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; // create a 64 bit secret key from raw bytes byte[] key = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; //NOI18N byte[] out = new byte[256]; // create a cipher and attempt to encrypt the data block with our key BlockCipher c = new BlowfishEngine(); c.init(true, new KeyParameter(key)); c.processBlock(data, 0, out, 0); c.reset(); // create a 192 bit secret key from raw bytes SecretKey key192 = new SecretKeySpec(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }, "Blowfish"); //NOI18N // now try encrypting with the larger key c.init(true, new KeyParameter(out)); c.processBlock(data, 0, out, 0); //If no exception is thrown before System.out.println(i18n.getString("Unrestricted_policy_test:_passed")); } /*catch (InvalidKeyException ex) { JOptionPane.showMessageDialog(this, i18n.getString("The_Unrestricted_Policy_Files_are_not_installed_in_your_JRE.") + i18n.getString("Please_install_them_to_enable_strong_cryptography!"), i18n.getString("Restricted_policy_files"), JOptionPane.ERROR_MESSAGE); }*/ catch (Exception ex) { Logger.getLogger(JCrypterFrame.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.cryptomator.crypto.aes256.AesSivCipherUtil.java
License:Open Source License
static byte[] sivEncrypt(byte[] aesKey, byte[] macKey, byte[] plaintext, byte[]... additionalData) throws InvalidKeyException { if (aesKey.length != 16 && aesKey.length != 24 && aesKey.length != 32) { throw new InvalidKeyException("Invalid aesKey length " + aesKey.length); }/*from w w w. j a v a 2 s . co m*/ final byte[] iv = s2v(macKey, plaintext, additionalData); final int numBlocks = (plaintext.length + 15) / 16; // clear out the 31st and 63rd (rightmost) bit: final byte[] ctr = Arrays.copyOf(iv, 16); ctr[8] = (byte) (ctr[8] & 0x7F); ctr[12] = (byte) (ctr[12] & 0x7F); final ByteBuffer ctrBuf = ByteBuffer.wrap(ctr); final long initialCtrVal = ctrBuf.getLong(8); final byte[] x = new byte[numBlocks * 16]; final BlockCipher aes = new AESFastEngine(); aes.init(true, new KeyParameter(aesKey)); for (int i = 0; i < numBlocks; i++) { final long ctrVal = initialCtrVal + i; ctrBuf.putLong(8, ctrVal); aes.processBlock(ctrBuf.array(), 0, x, i * 16); aes.reset(); } final byte[] ciphertext = xor(plaintext, x); return ArrayUtils.addAll(iv, ciphertext); }
From source file:org.cryptomator.crypto.aes256.AesSivCipherUtil.java
License:Open Source License
static byte[] sivDecrypt(byte[] aesKey, byte[] macKey, byte[] ciphertext, byte[]... additionalData) throws DecryptFailedException, InvalidKeyException { if (aesKey.length != 16 && aesKey.length != 24 && aesKey.length != 32) { throw new InvalidKeyException("Invalid aesKey length " + aesKey.length); }//w ww .j a v a2 s . com final byte[] iv = Arrays.copyOf(ciphertext, 16); final byte[] actualCiphertext = Arrays.copyOfRange(ciphertext, 16, ciphertext.length); final int numBlocks = (actualCiphertext.length + 15) / 16; // clear out the 31st and 63rd (rightmost) bit: final byte[] ctr = Arrays.copyOf(iv, 16); ctr[8] = (byte) (ctr[8] & 0x7F); ctr[12] = (byte) (ctr[12] & 0x7F); final ByteBuffer ctrBuf = ByteBuffer.wrap(ctr); final long initialCtrVal = ctrBuf.getLong(8); final byte[] x = new byte[numBlocks * 16]; final BlockCipher aes = new AESFastEngine(); aes.init(true, new KeyParameter(aesKey)); for (int i = 0; i < numBlocks; i++) { final long ctrVal = initialCtrVal + i; ctrBuf.putLong(8, ctrVal); aes.processBlock(ctrBuf.array(), 0, x, i * 16); aes.reset(); } final byte[] plaintext = xor(actualCiphertext, x); final byte[] control = s2v(macKey, plaintext, additionalData); if (MessageDigest.isEqual(control, iv)) { return plaintext; } else { throw new DecryptFailedException("Authentication failed"); } }
From source file:org.cryptomator.siv.SivMode.java
License:Open Source License
/** * Encrypts plaintext using SIV mode. A block cipher defined by the constructor is being used.<br> * /*from w w w .j ava2 s.c om*/ * @param ctrKey SIV mode requires two separate keys. You can use one long key, which is splitted in half. See https://tools.ietf.org/html/rfc5297#section-2.2 * @param macKey SIV mode requires two separate keys. You can use one long key, which is splitted in half. See https://tools.ietf.org/html/rfc5297#section-2.2 * @param plaintext Your plaintext, which shall be encrypted. * @param associatedData Optional associated data, which gets authenticated but not encrypted. * @return IV + Ciphertext as a concatenated byte array. * @throws IllegalArgumentException if the either of the two keys is of invalid length for the used {@link BlockCipher}. */ public byte[] encrypt(byte[] ctrKey, byte[] macKey, byte[] plaintext, byte[]... associatedData) { final byte[] iv = s2v(macKey, plaintext, associatedData); // Check if plaintext length will cause overflows if (plaintext.length > (Integer.MAX_VALUE - 16)) { throw new IllegalArgumentException("Plaintext is too long"); } final int numBlocks = (plaintext.length + 15) / 16; // clear out the 31st and 63rd (rightmost) bit: final byte[] ctr = Arrays.copyOf(iv, 16); ctr[8] = (byte) (ctr[8] & 0x7F); ctr[12] = (byte) (ctr[12] & 0x7F); final ByteBuffer ctrBuf = ByteBuffer.wrap(ctr); final long initialCtrVal = ctrBuf.getLong(8); final byte[] x = new byte[numBlocks * 16]; final BlockCipher cipher = threadLocalCipher.get(); cipher.init(true, new KeyParameter(ctrKey)); for (int i = 0; i < numBlocks; i++) { final long ctrVal = initialCtrVal + i; ctrBuf.putLong(8, ctrVal); cipher.processBlock(ctrBuf.array(), 0, x, i * 16); cipher.reset(); } final byte[] ciphertext = xor(plaintext, x); // concat IV + ciphertext: final byte[] result = new byte[iv.length + ciphertext.length]; System.arraycopy(iv, 0, result, 0, iv.length); System.arraycopy(ciphertext, 0, result, iv.length, ciphertext.length); return result; }
From source file:org.cryptomator.siv.SivMode.java
License:Open Source License
/** * Decrypts ciphertext using SIV mode. A block cipher defined by the constructor is being used.<br> * //from ww w . j a v a 2 s .c om * @param ctrKey SIV mode requires two separate keys. You can use one long key, which is splitted in half. See https://tools.ietf.org/html/rfc5297#section-2.2 * @param macKey SIV mode requires two separate keys. You can use one long key, which is splitted in half. See https://tools.ietf.org/html/rfc5297#section-2.2 * @param ciphertext Your ciphertext, which shall be encrypted. * @param associatedData Optional associated data, which needs to be authenticated during decryption. * @return Plaintext byte array. * @throws IllegalArgumentException If the either of the two keys is of invalid length for the used {@link BlockCipher}. * @throws UnauthenticCiphertextException If the authentication failed, e.g. because ciphertext and/or associatedData are corrupted. * @throws IllegalBlockSizeException If the provided ciphertext is of invalid length. */ public byte[] decrypt(byte[] ctrKey, byte[] macKey, byte[] ciphertext, byte[]... associatedData) throws UnauthenticCiphertextException, IllegalBlockSizeException { if (ciphertext.length < 16) { throw new IllegalBlockSizeException("Input length must be greater than or equal 16."); } final byte[] iv = Arrays.copyOf(ciphertext, 16); final byte[] actualCiphertext = Arrays.copyOfRange(ciphertext, 16, ciphertext.length); // will not overflow because actualCiphertext.length == (ciphertext.length - 16) final int numBlocks = (actualCiphertext.length + 15) / 16; // clear out the 31st and 63rd (rightmost) bit: final byte[] ctr = Arrays.copyOf(iv, 16); ctr[8] = (byte) (ctr[8] & 0x7F); ctr[12] = (byte) (ctr[12] & 0x7F); final ByteBuffer ctrBuf = ByteBuffer.wrap(ctr); final long initialCtrVal = ctrBuf.getLong(8); final byte[] x = new byte[numBlocks * 16]; final BlockCipher cipher = threadLocalCipher.get(); cipher.init(true, new KeyParameter(ctrKey)); for (int i = 0; i < numBlocks; i++) { final long ctrVal = initialCtrVal + i; ctrBuf.putLong(8, ctrVal); cipher.processBlock(ctrBuf.array(), 0, x, i * 16); cipher.reset(); } final byte[] plaintext = xor(actualCiphertext, x); final byte[] control = s2v(macKey, plaintext, associatedData); // time-constant comparison (taken from MessageDigest.isEqual in JDK8) assert iv.length == control.length; int diff = 0; for (int i = 0; i < iv.length; i++) { diff |= iv[i] ^ control[i]; } if (diff == 0) { return plaintext; } else { throw new UnauthenticCiphertextException("authentication in SIV decryption failed"); } }