List of usage examples for javax.crypto Cipher getBlockSize
public final int getBlockSize()
From source file:Main.java
private static byte[] decrypt(byte[] key, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); return cipher.doFinal(encrypted); }
From source file:Main.java
private static byte[] encrypt(byte[] key, byte[] clear) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); return cipher.doFinal(clear); }
From source file:Main.java
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; }
From source file:Main.java
public static String encryptData(String password, String plaintextData) throws Exception { // Thank you Mr. Nelenkov String maybeThisHelps = "http://nelenkov.blogspot.com/2012/04/using-password-based-encryption-on.html"; Log.v(TAG, maybeThisHelps);/* w ww . j a v a 2 s. c o m*/ int iterationCount = 100; //because Polaroid int keyLength = 256; int saltLength = keyLength; SecureRandom random = new SecureRandom(); byte[] salt = new byte[saltLength]; random.nextBytes(salt); KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded(); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[cipher.getBlockSize()]; random.nextBytes(iv); IvParameterSpec ivParams = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivParams); byte[] ciphertext = cipher.doFinal(plaintextData.getBytes("UTF-8")); String ivToString = new String(Base64.encode(iv, 0)); String saltToString = new String(Base64.encode(salt, 0)); String ciphertextToString = new String(Base64.encode(ciphertext, 0)); Log.d(TAG, ivToString + "]" + saltToString + "]" + ciphertextToString); return (ivToString + "]" + saltToString + "]" + ciphertextToString).replace("\n", ""); }
From source file:AESTest.java
/** * Uses a cipher to transform the bytes in an input stream and sends the transformed bytes to an * output stream./*from w w w. j av a 2 s .c o m*/ * @param in the input stream * @param out the output stream * @param cipher the cipher that transforms the bytes */ public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, GeneralSecurityException { int blockSize = cipher.getBlockSize(); int outputSize = cipher.getOutputSize(blockSize); byte[] inBytes = new byte[blockSize]; byte[] outBytes = new byte[outputSize]; int inLength = 0; boolean more = true; while (more) { inLength = in.read(inBytes); if (inLength == blockSize) { int outLength = cipher.update(inBytes, 0, blockSize, outBytes); out.write(outBytes, 0, outLength); } else more = false; } if (inLength > 0) outBytes = cipher.doFinal(inBytes, 0, inLength); else outBytes = cipher.doFinal(); out.write(outBytes); }
From source file:RSATest.java
/** * Uses a cipher to transform the bytes in an input stream and sends the transformed bytes to an * output stream./* w w w . j ava 2 s .c o m*/ * @param in the input stream * @param out the output stream * @param cipher the cipher that transforms the bytes */ public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, GeneralSecurityException { int blockSize = cipher.getBlockSize(); int outputSize = cipher.getOutputSize(blockSize); byte[] inBytes = new byte[blockSize]; byte[] outBytes = new byte[outputSize]; int inLength = 0; ; boolean more = true; while (more) { inLength = in.read(inBytes); if (inLength == blockSize) { int outLength = cipher.update(inBytes, 0, blockSize, outBytes); out.write(outBytes, 0, outLength); } else more = false; } if (inLength > 0) outBytes = cipher.doFinal(inBytes, 0, inLength); else outBytes = cipher.doFinal(); out.write(outBytes); }
From source file:com.confighub.core.security.Encryption.java
private static String encryptShared(CipherTransformation ct, String decrypted, String secret) throws ConfigException { if (null == decrypted) return null; try {// w w w. ja va 2s. c om Cipher cipher = Cipher.getInstance(ct.getName()); final int blockSize = cipher.getBlockSize(); SecretKeySpec sharedKey = new SecretKeySpec(getKeyAsBytes(secret, ct.getKeyLen()), ct.getAlgo()); if ("CBC".equals(ct.getMode())) cipher.init(Cipher.ENCRYPT_MODE, sharedKey, new IvParameterSpec(getIV(blockSize))); else cipher.init(Cipher.ENCRYPT_MODE, sharedKey); byte[] encrypted = cipher.doFinal(Utils.isBlank(decrypted) ? new byte[0] : decrypted.getBytes("UTF8")); return Base64.encodeBase64String(encrypted); } catch (Exception e) { throw new ConfigException(Error.Code.ENCRYPTION_ERROR); } }
From source file:com.confighub.core.security.Encryption.java
private static String decryptShared(CipherTransformation ct, String encrypted, String secret) throws ConfigException { if (null == encrypted) return null; try {/* www . j av a2 s . com*/ Cipher cipher = Cipher.getInstance(ct.getName()); final int blockSize = cipher.getBlockSize(); SecretKeySpec sharedKey = new SecretKeySpec(getKeyAsBytes(secret, ct.getKeyLen()), ct.getAlgo()); if ("CBC".equals(ct.getMode())) cipher.init(Cipher.DECRYPT_MODE, sharedKey, new IvParameterSpec(getIV(blockSize))); else cipher.init(Cipher.DECRYPT_MODE, sharedKey); byte[] decrypted = cipher.doFinal(Base64.decodeBase64(encrypted)); return new String(decrypted, "UTF8"); } catch (Exception e) { throw new ConfigException(Error.Code.DECRYPTION_ERROR); } }
From source file:org.sakuli.services.cipher.AesCbcCipher.java
public static byte[] decryptBytes(SecretKey aesKey, byte[] ciphertext) throws SakuliCipherException { checkCipherParameters(aesKey, ciphertext); final byte[] decrypted; try {//ww w .j av a 2s. c o m final ByteArrayInputStream bais = new ByteArrayInputStream(ciphertext); final Cipher aesCBC = Cipher.getInstance(CBC_ALGORITHM); final IvParameterSpec ivForCBC = readIV(aesCBC.getBlockSize(), bais); aesCBC.init(Cipher.DECRYPT_MODE, aesKey, ivForCBC); final byte[] buf = new byte[1_024]; try (final CipherInputStream cis = new CipherInputStream(bais, aesCBC); final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { int read; while ((read = cis.read(buf)) != -1) { baos.write(buf, 0, read); } decrypted = baos.toByteArray(); } return decrypted; } catch (Exception e) { throw new SakuliCipherException(e, "Error during decrypting secret!"); } }
From source file:org.sakuli.services.cipher.AesCbcCipher.java
public static byte[] encryptBytes(SecureRandom rng, SecretKey aesKey, byte[] plaintext) throws SakuliCipherException { checkCipherParameters(aesKey, plaintext); final byte[] ciphertext; try {/*from w w w . ja v a2s .co m*/ final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Cipher aesCBC = Cipher.getInstance(CBC_ALGORITHM); final IvParameterSpec ivForCBC = createIV(aesCBC.getBlockSize(), Optional.of(rng)); aesCBC.init(Cipher.ENCRYPT_MODE, aesKey, ivForCBC); baos.write(ivForCBC.getIV()); try (final CipherOutputStream cos = new CipherOutputStream(baos, aesCBC)) { cos.write(plaintext); } ciphertext = baos.toByteArray(); return ciphertext; } catch (Exception e) { throw new SakuliCipherException(e, "Error during encrypting secret!"); } }