/** * Decrypt the message with TripleDES * * @param message * @return * @throws Exception */ public static String decrypt(byte[] message) throws Exception { byte[] values = Base64decodingByte(message, 0); final MessageDigest md = MessageDigest.getInstance("SHA-1"); final byte[] digestOfPassword = md.digest(token .getBytes("utf-8")); final byte[] keyBytes = copyOf(digestOfPassword, 24); for (int j = 0, k = 16; j < 8; ) { keyBytes[k++] = keyBytes[j++]; } final SecretKey key = new SecretKeySpec(keyBytes, "DESede"); String s1 = "12345678"; byte[] bytes = s1.getBytes(); final IvParameterSpec iv = new IvParameterSpec(bytes); final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); decipher.init(Cipher.DECRYPT_MODE, key, iv); final byte[] plainText = decipher.doFinal(values); return new String(plainText, "UTF-8"); }
@Override public void subscribe(ObservableEmitter<EncryptionResult> emitter) throws Exception { if (initException==null) { createKey(keyName, timeout); SecretKey secretKey=(SecretKey)ks.getKey(keyName, null); Cipher cipher=Cipher.getInstance("AES/CBC/PKCS7Padding"); SecureRandom rand=new SecureRandom(); byte[] iv=new byte[BLOCK_SIZE]; rand.nextBytes(iv); IvParameterSpec ivParams=new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParams); emitter.onNext(new EncryptionResult(ivParams.getIV(), cipher.doFinal(toEncrypt))); } else { throw initException; } }
public PBKDF2Encryptor(final String passPhrase, final byte[] salt, final int iterationCount, final int i1) { this.iterationCount = iterationCount; try { // create the key KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount, i1); SecretKey tmp = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keySpec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); // encryptor ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); ecipher.init(Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = ecipher.getParameters(); byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV(); // decryptor dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); dcipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv)); } catch (Exception ex) { throw new IllegalArgumentException(ex); } }
byte[] iv = new byte[cipher.getBlockSize()]; new SecureRandom().nextBytes(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv); MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(keyString.getBytes()); byte[] key = new byte[16]; System.arraycopy(digest.digest(), 0, key, 0, key.length); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8")); System.out.println("encrypted: " + new String(encrypted));
public static String encryptAES(String data, String secretKey) { try { byte[] secretKeys = Arrays.copyOfRange(Hashing.sha256().hashString(secretKey, Charsets.UTF_8) .asBytes(), 0, 16); final SecretKey secret = new SecretKeySpec(secretKeys, "AES"); final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); final AlgorithmParameters params = cipher.getParameters(); final byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV(); final byte[] cipherText = cipher.doFinal(data.getBytes(Charsets.UTF_8)); return DatatypeConverter.printHexBinary(iv) + DatatypeConverter.printHexBinary(cipherText); } catch (Exception e) { throw Throwables.propagate(e); } }
md.reset(); if (addmd++ > 0) { md.update(md_buf); md.update(data); if (null != salt) { md.update(salt, 0, 8); aesCBC.getBlockSize(), md5, salt, args[ARG_INDEX_PASSWORD].getBytes(ASCII), ITERATIONS); SecretKeySpec key = new SecretKeySpec(keyAndIV[INDEX_KEY], "AES"); IvParameterSpec iv = new IvParameterSpec(keyAndIV[INDEX_IV]); aesCBC.init(Cipher.DECRYPT_MODE, key, iv); byte[] decrypted = aesCBC.doFinal(encrypted);
private Cipher initCipher(byte[] keyData) throws GeneralSecurityException { MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); sha256.reset(); byte[] digest = sha256.digest(keyData); Cipher result = Cipher.getInstance("AES/CFB8/NoPadding"); result.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(digest, 0, 16, "AES"), new IvParameterSpec(digest, 16, 16), rnd); return result; }
private Cipher createCipher(byte[] salt, final int mode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { digest.reset(); digest.update(pwdAsBytes); digest.update(salt, 0, 8); result = digest.digest(); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(mode, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
secretKey = new SecretKeySpec(passwordKey, CIPHER_ALGORITHM); ivParameterSpec = new IvParameterSpec(rawSecretKey); MessageDigest digest; try { digest = MessageDigest.getInstance(MESSAGEDIGEST_ALGORITHM); return digest.digest(text.getBytes()); } catch (NoSuchAlgorithmException e) { Log.e(TAG, "No such algorithm " + MESSAGEDIGEST_ALGORITHM, e);
MessageDigest messageDigest = MessageDigest.getInstance(mBuilder.getDigestAlgorithm()); messageDigest.update(key.getBytes(mBuilder.getCharsetName())); return Base64.encodeToString(messageDigest.digest(), Base64.NO_PADDING).toCharArray(); setSecureRandom(SecureRandom.getInstance(getSecureRandomAlgorithm())); setIvParameterSpec(new IvParameterSpec(getIv())); return new Encryption(this);
new SecureRandom().nextBytes(iv); jnlpMac = JnlpSlaveAgentProtocol.SLAVE_SECRET.mac(slaveName.getBytes("UTF-8")); SecretKey key = new SecretKeySpec(jnlpMac, 0, /* export restrictions */ 128 / 8, "AES"); byte[] encrypted; try { Cipher c = Secret.getCipher("AES/CFB8/NoPadding"); c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); encrypted = c.doFinal(csos.getBytes()); } catch (GeneralSecurityException x) { throw new IOException(x);
hmacKey = digest.digest(key.toArray()); } else { key.append(format == FORMAT.CLIENT ? SERVER_MAGIC_CONFIDENTIALITY : CLIENT_MAGIC_CONFIDENTIALITY); hmacKey = digest.digest(key.toArray()); throw saslDigest.mechUnknownCipher(cipher).toSaslException(); ciph = Cipher.getInstance(transformationSpec.getTransformation()); int slash = ciph.getAlgorithm().indexOf('/'); String alg = (slash > -1 ? ciph.getAlgorithm().substring(0, slash) : ciph.getAlgorithm()); cipherKey = new SecretKeySpec(cipherKeyBytes, alg); } else if (cipher.equals("des")) { cipherKeyBytes = Arrays.copyOf(hmacKey, 7); // first 7 bytes ciph.init((wrap ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE), cipherKey, new IvParameterSpec(IV), secureRandomGenerator); } else { ciph.init((wrap ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE), cipherKey, secureRandomGenerator);
SecureRandom rnd = new SecureRandom (); rnd.nextBytes (mSalt); Db ("generated salt :" + Hex.encodeHexString (mSalt)); mEcipher.init (Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = mEcipher.getParameters (); mInitVec = params.getParameterSpec (IvParameterSpec.class).getIV(); mDecipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(mInitVec));
} else if (rfile.length() == 0L) { new SecureRandom().nextBytes(iv); rfile.write(iv); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key,"AES"), new IvParameterSpec(iv)); lastBlock = cipher.doFinal(lastBlockEnc); rfile.seek(rfile.length()-16); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key,"AES"), new IvParameterSpec(iv)); byte[] out; if (lastBlock != null) { // lastBlock is null if we're starting a new file }; Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key,"AES"), new IvParameterSpec(iv)); byte[] buff = new byte[1<<13]; //8kiB while (true) {
public AesFlushingCipher(int mode, byte[] secretKey, long nonce, long offset) { try { cipher = Cipher.getInstance("AES/CTR/NoPadding"); blockSize = cipher.getBlockSize(); zerosBlock = new byte[blockSize]; flushedBlock = new byte[blockSize]; long counter = offset / blockSize; int startPadding = (int) (offset % blockSize); cipher.init( mode, new SecretKeySpec(secretKey, Util.splitAtFirst(cipher.getAlgorithm(), "/")[0]), new IvParameterSpec(getInitializationVector(nonce, counter))); if (startPadding != 0) { updateInPlace(new byte[startPadding], 0, startPadding); } } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) { // Should never happen. throw new RuntimeException(e); } }
SecureRandom rng = new SecureRandom(); byte[] aesKeyData = new byte[128 / Byte.SIZE]; rng.nextBytes(aesKeyData); SecretKey aesKey = new SecretKeySpec(aesKeyData, "AES"); // just to show it works Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[aesCBC.getBlockSize()]; rng.nextBytes(iv); aesCBC.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(iv));
private void loadSecretKey(ObjectInputStream ois) throws IOException, GeneralSecurityException { byte[] encryptedData = readBytes(ois); byte[] iv = readBytes(ois); decrypt.init(Cipher.DECRYPT_MODE, storageSecretKey, new IvParameterSpec(iv)); Assert.checkMaximumParameter("cipher block size", 256, decrypt.getBlockSize()); byte[] unPadded = pkcs7UnPad(decrypt.doFinal(encryptedData)); ObjectInputStream entryOis = new ObjectInputStream(new ByteArrayInputStream(unPadded)); String ksAlias = entryOis.readUTF(); byte[] encodedSecretKey = readBytes(entryOis); KeyStore.Entry entry = new KeyStore.SecretKeyEntry(new SecretKeySpec(encodedSecretKey, DATA_OID)); dataKeyStore.setEntry(ksAlias, entry, convertParameter(protectionParameter)); }
public static byte[] encrypt(byte[] data, IvParameterSpec zeroIv, SecretKeySpec keySpec) { try { Profiler.enter("time cost on [aes encrypt]: data length=" + data.length); Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.ENCRYPT_MODE, keySpec, zeroIv); return cipher.doFinal(data); } catch (Exception e) { LOGGER.error("AES encrypt ex, iv={}, key={}", Arrays.toString(zeroIv.getIV()), Arrays.toString(keySpec.getEncoded()), e); throw new CryptoException("AES encrypt ex", e); } finally { Profiler.release(); } }
@Override public void init(byte[] key, byte[] iv) throws IOException { Preconditions.checkNotNull(key); Preconditions.checkNotNull(iv); contextReset = false; try { cipher.init(mode, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); } catch (Exception e) { throw new IOException(e); } }
SecretKeySpec skeySpec = new SecretKeySpec(getCryptoKeyByteArray(length=16)); Cipher encryptor = Cipher.getInstance("AES/CTR/NoPadding"); // Initialisation vector: byte[] iv = new byte[encryptor.getBlockSize()]; SecureRandom.getInstance("SHA1PRNG").nextBytes(iv); // If storing separately IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); encryptor.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); byte[] encrypted = encryptor.doFinal(plain);