List of usage examples for javax.crypto.spec IvParameterSpec getIV
public byte[] getIV()
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 . j a v a 2 s . c o 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!"); } }
From source file:com.cws.esolutions.security.utils.PasswordUtils.java
/** * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility * is required but encryption (obfuscation, technically) is required. * * @param value - The plain text data to encrypt * @param salt - The salt value to utilize for the request * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory * @param iterations - The number of times to loop through the keyspec * @param keyBits - The size of the key, in bits * @param algorithm - The algorithm to encrypt the data with * @param cipherInstance - The cipher instance to utilize * @param encoding - The text encoding/*from w w w .j ava2 s . c o m*/ * @return The encrypted string in a reversible format * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing */ public static final String encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException { final String methodName = PasswordUtils.CNAME + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", secretInstance); DEBUGGER.debug("Value: {}", iterations); DEBUGGER.debug("Value: {}", keyBits); DEBUGGER.debug("Value: {}", algorithm); DEBUGGER.debug("Value: {}", cipherInstance); DEBUGGER.debug("Value: {}", encoding); } String encPass = null; try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance); PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits); SecretKey keyTmp = keyFactory.generateSecret(keySpec); SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm); Cipher pbeCipher = Cipher.getInstance(cipherInstance); pbeCipher.init(Cipher.ENCRYPT_MODE, sks); AlgorithmParameters parameters = pbeCipher.getParameters(); IvParameterSpec ivParameterSpec = parameters.getParameterSpec(IvParameterSpec.class); byte[] cryptoText = pbeCipher.doFinal(value.getBytes(encoding)); byte[] iv = ivParameterSpec.getIV(); String combined = Base64.getEncoder().encodeToString(iv) + ":" + Base64.getEncoder().encodeToString(cryptoText); encPass = Base64.getEncoder().encodeToString(combined.getBytes()); } catch (InvalidKeyException ikx) { throw new SecurityException(ikx.getMessage(), ikx); } catch (NoSuchAlgorithmException nsx) { throw new SecurityException(nsx.getMessage(), nsx); } catch (NoSuchPaddingException npx) { throw new SecurityException(npx.getMessage(), npx); } catch (IllegalBlockSizeException ibx) { throw new SecurityException(ibx.getMessage(), ibx); } catch (BadPaddingException bpx) { throw new SecurityException(bpx.getMessage(), bpx); } catch (UnsupportedEncodingException uex) { throw new SecurityException(uex.getMessage(), uex); } catch (InvalidKeySpecException iksx) { throw new SecurityException(iksx.getMessage(), iksx); } catch (InvalidParameterSpecException ipsx) { throw new SecurityException(ipsx.getMessage(), ipsx); } return encPass; }
From source file:com.keepassdroid.fingerprint.FingerPrintHelper.java
public void encryptData(final String value) { if (!isFingerprintInitialized()) { if (fingerPrintCallback != null && hasEnrolledFingerprints()) { fingerPrintCallback.onException(); }//from w w w . ja v a2 s. c o m return; } try { // actual do encryption here byte[] encrypted = cipher.doFinal(value.getBytes()); final String encryptedValue = new String(Base64Coder.encode(encrypted)); // passes updated iv spec on to callback so this can be stored for decryption final IvParameterSpec spec = cipher.getParameters().getParameterSpec(IvParameterSpec.class); final String ivSpecValue = new String(Base64Coder.encode(spec.getIV())); fingerPrintCallback.handleEncryptedResult(encryptedValue, ivSpecValue); } catch (final Exception e) { fingerPrintCallback.onException(); } }
From source file:test.PBEncryptLink.java
/** * Retrieve the Initialization Vector from the stored IvParameterSpec. * //www . j av a2 s . c o m * @return the iV */ public byte[] getIV() { IvParameterSpec ivs = getIvParmSpec(); byte[] iv = null; if (ivs == null) { iv = new byte[0]; } else { iv = ivs.getIV(); } return iv; }
From source file:Networking.Server.java
public byte[] encryptMessage() { byte[] cipherText = null; byte[] text = null; try {/* w ww .j a v a 2s .c o m*/ byte[] plainText = message.getBytes(); SecretKeySpec myKey; myKey = new SecretKeySpec(this.d.getSessionKey(), "AES"); SecureRandom random = new SecureRandom(); byte randombytes[] = new byte[16]; random.nextBytes(randombytes); this.d.setIv(randombytes); IvParameterSpec iv = new IvParameterSpec(this.d.getIv()); Cipher c = Cipher.getInstance("AES/CTR/NoPadding"); c.init(Cipher.ENCRYPT_MODE, myKey, iv); cipherText = new byte[c.getOutputSize(plainText.length)]; c.doFinal(plainText, 0, plainText.length, cipherText); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(iv.getIV()); outputStream.write((cipherText)); text = outputStream.toByteArray(); File ivMsgFile = new File("./write_iv.txt"); if (ivMsgFile.createNewFile()) { System.out.println("File is created!"); } FileOutputStream sigfos = new FileOutputStream(ivMsgFile); sigfos.write(text); sigfos.flush(); sigfos.close(); byte[] sig_bytes = new byte[(int) ivMsgFile.length()]; BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(ivMsgFile)); bis1.read(sig_bytes, 0, sig_bytes.length); sendMesLen((int) ivMsgFile.length()); Timestamp timestamp = new Timestamp(System.currentTimeMillis()); System.out.println("sent time: " + timestamp); Socket writeSocket = new Socket(Ip, port); writeSocket.getOutputStream().write(sig_bytes, 0, sig_bytes.length); writeSocket.getOutputStream().flush(); writeSocket.close(); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException | IOException ex) { Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); } return Base64.encodeBase64(text); }
From source file:com.kactech.otj.Utils.java
public static ByteBuffer seal(String msg, String nymID, PublicKey nymKey, SecretKeySpec aesSecret, IvParameterSpec vector) throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { ByteBuffer buff = ByteBuffer.allocate(msg.length() + 500);//donno? buff.order(ByteOrder.BIG_ENDIAN); buff.putShort((short) 1);//asymmetric buff.putInt(1);//array size buff.putInt(nymID.length() + 1);// w w w .ja v a2s . c om buff.put(bytes(nymID + '\0', US_ASCII)); // create encoded key and message Cipher cipher; try { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); } catch (Exception e) { throw new RuntimeException(e); } cipher.init(Cipher.ENCRYPT_MODE, aesSecret, vector); byte[] encrypted = cipher.doFinal(bytes(msg + '\0', UTF8)); try { cipher = Cipher.getInstance(WRAP_ALGO); } catch (Exception e) { throw new RuntimeException(e); } cipher.init(Cipher.WRAP_MODE, nymKey); byte[] encKeyBytes = cipher.wrap(aesSecret); buff.putInt(encKeyBytes.length); buff.put(encKeyBytes); buff.putInt(vector.getIV().length); buff.put(vector.getIV()); buff.put(encrypted); buff.flip(); return buff; }