List of usage examples for javax.crypto.spec IvParameterSpec IvParameterSpec
public IvParameterSpec(byte[] iv)
iv
as the IV. From source file:com.baran.crypto.CryptoDES.java
public byte[] decrypt(byte[] message, String trivia) throws Exception { final MessageDigest md = MessageDigest.getInstance("md5"); final byte[] digestTrivia = md.digest(trivia.getBytes("utf-8")); final byte[] keyBytes = Arrays.copyOf(digestTrivia, 24); for (int j = 0, k = 16; j < 8;) { keyBytes[k++] = keyBytes[j++];// w w w .j a v a 2 s . c o m } final SecretKey key = new SecretKeySpec(keyBytes, "DESede"); final IvParameterSpec iv = new IvParameterSpec(new byte[8]); final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); decipher.init(Cipher.DECRYPT_MODE, key, iv); final byte[] plainText = decipher.doFinal(message); return plainText; }
From source file:dualcontrol.CryptoHandler.java
private void cipher(String mode, String alias, String transformation, String ivString, String dataString) throws Exception { logger.debug(join("cipher", alias, transformation, mode)); SecretKey key = dualControl.loadKey(alias); logger.debug("keyalg " + key.getAlgorithm()); Cipher cipher = Cipher.getInstance(transformation); logger.debug("mode " + mode); if (mode.equals("DECRYPT")) { this.ivBytes = Base64.decodeBase64(ivString); logger.debug("iv " + Base64.encodeBase64String(ivBytes)); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); this.dataBytes = cipher.doFinal(Base64.decodeBase64(dataString)); write(ivBytes, dataBytes);// w w w. ja v a 2s. c om } else if (mode.equals("ENCRYPT")) { this.ivBytes = getIvBytes(ivString); logger.debug("iv " + Base64.encodeBase64String(ivBytes)); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); long startTime = System.nanoTime(); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); this.dataBytes = cipher.doFinal(dataString.getBytes()); logger.info("encrypt time nanos " + Nanos.elapsed(startTime)); write(ivBytes, dataBytes); } }
From source file:com.glaf.core.security.SecurityUtils.java
/** * DES//from ww w . j av a 2s .co m * * @param data * * @param key * ???8? * @return ? */ public static String encode(String key, String data) { if (data == null) { return null; } try { DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // key??8? Key secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM_DES); IvParameterSpec iv = new IvParameterSpec("12345678".getBytes()); AlgorithmParameterSpec paramSpec = iv; cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); byte[] bytes = cipher.doFinal(data.getBytes()); return byte2hex(bytes); } catch (Exception ex) { throw new SecurityException(ex); } }
From source file:com.thoughtworks.go.security.AESEncrypter.java
@Override public String decrypt(String cipherText) throws CryptoException { try {//w ww. j a v a2 s . c o m Assert.isTrue(canDecrypt(cipherText), "bad cipher text"); String[] splits = cipherText.split(":"); String encodedIV = splits[1]; String encodedCipherText = splits[2]; byte[] initializationVector = DECODER.decode(encodedIV); Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); decryptCipher.init(Cipher.DECRYPT_MODE, createSecretKeySpec(), new IvParameterSpec(initializationVector)); byte[] decryptedBytes = decryptCipher.doFinal(DECODER.decode(encodedCipherText)); return new String(decryptedBytes, StandardCharsets.UTF_8); } catch (Exception e) { throw new CryptoException(e); } }
From source file:org.craftercms.commons.crypto.SimpleCipher.java
public byte[] encrypt(byte[] clear) throws CryptoException { if (key == null) { key = CryptoUtils.generateAesKey(); logger.debug(LOG_KEY_KEY_GEN);/*ww w . ja va 2 s. c o m*/ } if (iv == null) { iv = CryptoUtils.generateAesIv(); logger.debug(LOG_KEY_IV_GEN); } if (cipher == null) { cipher = createDefaultCipher(); } try { cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); return cipher.doFinal(clear); } catch (GeneralSecurityException e) { throw new CryptoException(ERROR_KEY_ENC_ERROR, e); } finally { logger.debug(LOG_KEY_ENC_SUCCESSFUL); } }
From source file:net.thewaffleshop.nimbus.api.EncryptionAPI.java
/** * Encrypt//ww w. ja va 2s . co m * * @param secretKey * @param iv * @param secret * @return */ public byte[] encrypt(SecretKey secretKey, byte[] iv, byte[] secret) { try { IvParameterSpec ivSpec = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec); byte[] ret = cipher.doFinal(secret); return ret; } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
From source file:com.waveerp.desEncryption.java
public void Encrypter(String keyString, String ivString) { try {// w w w.ja va 2s . c o m keyString = "J3SuSChRiSt"; ivString = "Pr0V3rBs"; final MessageDigest msgDigest = MessageDigest.getInstance("md5"); final byte[] digestOfPassword = msgDigest.digest(Base64.decodeBase64(keyString.getBytes("utf-8"))); final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); for (int j = 0, k = 16; j < 8;) { keyBytes[k++] = keyBytes[j++]; } kSpec = new DESedeKeySpec(keyBytes); sKey = SecretKeyFactory.getInstance("DESede").generateSecret(kSpec); ivParSpec = new IvParameterSpec(ivString.getBytes()); } catch (Exception e) { e.printStackTrace(); } }
From source file:edu.utdallas.bigsecret.cipher.AesCtr.java
/** * Encrypts input data with AES CTR mode. * @param data Input byte array./*from w w w.ja v a2s .c o m*/ * @return Encryption result. * @throws Exception Throws exception if there is no data to encrypt.<br> * May throw exception based on Javax.Crypto.Cipher class */ public byte[] encrypt(byte[] data) throws Exception { //check if there is data to encrypt if (data.length == 0) { throw new Exception("No data to encrypt"); } //create iv byte[] iv = new byte[BLOCK_SIZE_BYTES]; byte[] randomNumber = (new BigInteger(BLOCK_SIZE_BITS, m_secureRandom)).toByteArray(); int a; for (a = 0; a < randomNumber.length && a < BLOCK_SIZE_BYTES; a++) iv[a] = randomNumber[a]; for (; a < BLOCK_SIZE_BYTES; a++) iv[a] = 0; //init cipher instance m_cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, m_keySpec, new IvParameterSpec(iv)); //return concatenation of iv + encrypted data return ArrayUtils.addAll(iv, m_cipher.doFinal(data)); }
From source file:com.bamboocloud.im.provisioner.json.crypto.simple.SimpleDecryptor.java
@Override public JsonValue decrypt(JsonValue value) throws JsonCryptoException { try {/*from ww w. j a v a2 s.c o m*/ JsonValue key = value.get("key").required(); String cipher = value.get("cipher").required().asString(); Key symmetricKey; if (key.isString()) { symmetricKey = select(key.asString()); } else { Key privateKey = select(key.get("key").required().asString()); Cipher asymmetric = Cipher.getInstance(key.get("cipher").required().asString()); asymmetric.init(Cipher.DECRYPT_MODE, privateKey); byte[] ciphertext = Base64.decodeBase64(key.get("data").required().asString()); symmetricKey = new SecretKeySpec(asymmetric.doFinal(ciphertext), cipher.split("/", 2)[0]); } Cipher symmetric = Cipher.getInstance(cipher); String iv = value.get("iv").asString(); IvParameterSpec ivps = (iv == null ? null : new IvParameterSpec(Base64.decodeBase64(iv))); symmetric.init(Cipher.DECRYPT_MODE, symmetricKey, ivps); byte[] plaintext = symmetric.doFinal(Base64.decodeBase64(value.get("data").required().asString())); return new JsonValue(mapper.readValue(plaintext, Object.class)); } catch (GeneralSecurityException gse) { // Java Cryptography Extension throw new JsonCryptoException(gse); } catch (IOException ioe) { // Jackson throw new JsonCryptoException(ioe); } catch (JsonValueException jne) { // JSON Fluent throw new JsonCryptoException(jne); } }
From source file:com.gvmax.common.util.Enc.java
public byte[] decryptByte(String encryptedValue) { if (encryptedValue == null) { return null; }/*from ww w. j av a2 s . c om*/ if (!enabled) { return StringUtil.getBytes(encryptedValue); } try { c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); byte[] decordedValue = Base64.decodeBase64(encryptedValue); return c.doFinal(decordedValue); } catch (Exception e) { logger.warn("Unable to decrypt: " + encryptedValue, e); return null; } }