List of usage examples for org.bouncycastle.crypto.generators SCrypt generate
public static byte[] generate(byte[] P, byte[] S, int N, int r, int p, int dkLen)
From source file:org.bunkr.core.descriptor.ScryptDescriptor.java
License:Open Source License
@Override public byte[] writeInventoryToBytes(Inventory source, UserSecurityProvider usp) throws BaseBunkrException { try {// w w w . j ava 2 s. c om byte[] inventoryJsonBytes = InventoryJSON.encode(source).getBytes(); // first refresh the salt RandomMaker.fill(this.scryptSalt); if (this.encryptionAlgorithm == Encryption.NONE) throw new IllegalArgumentException("ScryptDescriptor requires an active encryption mode"); // generate the encryption key and iv using scrypt byte[] data = SCrypt.generate(usp.getHashedPassword(), this.scryptSalt, this.scryptN, this.scryptR, this.scryptP, this.encryptionAlgorithm.keyByteLength + this.encryptionAlgorithm.ivByteLength); // pull key and iv out of the data byte[] key = Arrays.copyOfRange(data, 0, this.encryptionAlgorithm.keyByteLength); byte[] iv = Arrays.copyOfRange(data, this.encryptionAlgorithm.keyByteLength, data.length); byte[] encryptedInv = SimpleAES.encrypt(this.encryptionAlgorithm, inventoryJsonBytes, key, iv); Arrays.fill(inventoryJsonBytes, (byte) 0); Arrays.fill(data, (byte) 0); Arrays.fill(key, (byte) 0); Arrays.fill(iv, (byte) 0); return encryptedInv; } catch (IllegalPasswordException | CryptoException e) { throw new BaseBunkrException(e); } }
From source file:org.cryptomator.crypto.aes256.Aes256Cryptor.java
License:Open Source License
private SecretKey scrypt(CharSequence password, byte[] salt, int costParam, int blockSize, int keyLengthInBits) { // use sb, as password.toString's implementation is unknown final StringBuilder sb = new StringBuilder(password); final byte[] pw = sb.toString().getBytes(); try {/* w w w . j a v a 2 s . c om*/ final byte[] key = SCrypt.generate(pw, salt, costParam, blockSize, 1, keyLengthInBits / Byte.SIZE); return new SecretKeySpec(key, AES_KEY_ALGORITHM); } finally { // destroy copied bytes of the plaintext password: Arrays.fill(pw, (byte) 0); for (int i = 0; i < password.length(); i++) { sb.setCharAt(i, (char) 0); } } }
From source file:org.gity.internal.crypto.GCMCipher.java
License:Open Source License
/** * Encrypts a given file with AES-256 in GCM mode of operation * <p>//from www .j a v a 2s . c om * Reads data from the InputStream and writes the encrypted data to the * OutputStream * * @param inputFile * @return * @throws java.io.IOException * @throws java.security.InvalidKeyException * @throws java.security.InvalidAlgorithmParameterException * @throws javax.crypto.BadPaddingException * @throws javax.crypto.IllegalBlockSizeException */ protected int encrypt_V00(File inputFile) throws IOException, InvalidKeyException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException { DefaultFrame.setFileQueueItemStatus("Generating header"); // defining input stream InputStream input = new FileInputStream(inputFile); // getting the encryption password char[] pass = DefaultCipher.getEncryptionPassword(); // generating Sx, Nx, R, Kx final byte[] S1 = GPCrypto.randomGen(S_BYTES), S2 = GPCrypto.randomGen(S_BYTES), epoch = DatatypeConverter.parseHexBinary(Long.toHexString(NTP.getTime() / 1000)), N1 = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, epoch[0], epoch[1], epoch[2], epoch[3] }, N2 = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, epoch[0], epoch[1], epoch[2], (byte) (epoch[3] + 0x01) }, R = GPCrypto.randomGen(R_BYTES); final SecretKey K1 = new SecretKeySpec(SCrypt.generate(GPCrypto.charToByte(pass), S1, (int) Math.pow(2, K1_KDF_N), KDF_r, KDF_p, CIPHER_KEY_BITS / 8), 0, CIPHER_KEY_BITS / 8, "AES"), K2 = new SecretKeySpec( SCrypt.generate(R, S2, (int) Math.pow(2, K2_KDF_N), KDF_r, KDF_p, CIPHER_KEY_BITS / 8), 0, CIPHER_KEY_BITS / 8, "AES"); // writing header this.cipher.init(Cipher.ENCRYPT_MODE, K1, new GCMParameterSpec(GCM_TAG_BITS, N1, 0, GCM_NONCE_BYTES)); dos.write((byte) 0x00); dos.write(S1); dos.write(N1); dos.write(DatatypeConverter.parseHexBinary(Integer.toHexString(K1_KDF_N))); dos.write(cipher.doFinal(R)); dos.write(S2); dos.write(N2); dos.write(DatatypeConverter.parseHexBinary(Integer.toHexString(K2_KDF_N))); // encrypting file DefaultFrame.setFileQueueItemStatus("Uploading (0%)"); this.cipher.init(Cipher.ENCRYPT_MODE, K2, new GCMParameterSpec(GCM_TAG_BITS, N2, 0, GCM_NONCE_BYTES)); Long percentage = inputFile.length() / 100L, bytesRead = 0L; int r = 0; while ((r = input.read(buf)) != -1) { if (r == BUFFER_SIZE) { dos.write(this.cipher.update(buf)); } else { dos.write(this.cipher.doFinal(Arrays.copyOfRange(buf, 0, r))); } bytesRead += r; if (bytesRead % percentage > 1) { DefaultFrame.setFileQueueItemStatus("Uploading (" + bytesRead / percentage + "%)"); } } DefaultFrame.setFileQueueItemStatus("Finalizing"); // erasing cryptographic parameters and closing streams GPCrypto.eraseByteArrays(S1, S2, epoch, N1, N2, R); GPCrypto.eraseKeys(K1, K2); GPCrypto.sanitize(pass); input.close(); return dis.readInt(); }
From source file:org.gity.internal.crypto.GCMCipher.java
License:Open Source License
/** * Decrypts a given file with AES-256 in GCM mode of operation * * @param outputFile/* w w w . j a v a2 s . c o m*/ * @return * @throws java.io.IOException * @throws java.security.InvalidKeyException * @throws java.security.InvalidAlgorithmParameterException * @throws javax.crypto.BadPaddingException * @throws javax.crypto.IllegalBlockSizeException */ protected int decrypt_V00(File outputFile) throws IOException, InvalidKeyException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException { DefaultFrame.setFileQueueItemStatus("Reading header"); // getting file size and calculating iterCnt long fileSize = dis.readLong(), iterCnt = fileSize / BUFFER_SIZE, percentage = fileSize / 100L, bytesRead = 0L; // defining output stream OutputStream output = new FileOutputStream(outputFile); // getting the encryption password char[] pass = DefaultCipher.getEncryptionPassword(); // reading header byte[] header = new byte[234]; dis.read(header, 0, 234); // reading Sx, Nx, scrypt factors and generating K1 final byte[] S1 = Arrays.copyOfRange(header, 0, VS1), N1 = Arrays.copyOfRange(header, VS1, S1N1), K1_N = Arrays.copyOfRange(header, S1N1, N1K1N), S2 = Arrays.copyOfRange(header, K1NR, RS2), N2 = Arrays.copyOfRange(header, RS2, S2N2), K2_N = Arrays.copyOfRange(header, S2N2, N2K2N); final int K1_N_bak = (int) Math.pow(2, Integer.valueOf(Hex.toHexString(K1_N), 16)), K2_N_bak = (int) Math.pow(2, Integer.valueOf(Hex.toHexString(K2_N), 16)); final SecretKey K1 = new SecretKeySpec( SCrypt.generate(GPCrypto.charToByte(pass), S1, K1_N_bak, KDF_r, KDF_p, CIPHER_KEY_BITS / 8), 0, CIPHER_KEY_BITS / 8, "AES"); // reading E(K1, N1, R) this.cipher.init(Cipher.DECRYPT_MODE, K1, new GCMParameterSpec(GCM_TAG_BITS, N1, 0, GCM_NONCE_BYTES)); final byte[] R = cipher.doFinal(Arrays.copyOfRange(header, N1K1N, K1NR)); // generating K2 final SecretKey K2 = new SecretKeySpec(SCrypt.generate(R, S2, K2_N_bak, KDF_r, KDF_p, CIPHER_KEY_BITS / 8), 0, CIPHER_KEY_BITS / 8, "AES"); // decrypting file DefaultFrame.setFileQueueItemStatus("Downloading"); this.cipher.init(Cipher.DECRYPT_MODE, K2, new GCMParameterSpec(GCM_TAG_BITS, N2, 0, GCM_NONCE_BYTES)); for (int i = 0; i < iterCnt; i++) { dis.readFully(buf); if (i != iterCnt) { output.write(this.cipher.update(buf)); } else if (fileSize % BUFFER_SIZE == 0) { output.write(this.cipher.doFinal(buf)); } bytesRead += BUFFER_SIZE; if (bytesRead % percentage > 1) { DefaultFrame.setFileQueueItemStatus("Uploading (" + bytesRead / percentage + "%)"); } } if (fileSize % BUFFER_SIZE != 0) { int r = dis.read(buf, 0, ((int) fileSize % BUFFER_SIZE) + 16); output.write(this.cipher.update(Arrays.copyOfRange(buf, 0, r - GCM_TAG_BITS / 8))); output.write(this.cipher.doFinal(Arrays.copyOfRange(buf, r - GCM_TAG_BITS / 8, r))); bytesRead += r; if (bytesRead % percentage > 1) { DefaultFrame.setFileQueueItemStatus("Uploading (" + bytesRead / percentage + "%)"); } } DefaultFrame.setFileQueueItemStatus("Finalizing"); // erasing cryptographic parameters and closing streams GPCrypto.eraseByteArrays(header, S1, S2, N1, N2, R); GPCrypto.eraseKeys(K1, K2); GPCrypto.sanitize(pass); output.close(); return dis.readInt(); }
From source file:org.gity.internal.crypto.GCMCopy.java
License:Open Source License
/** * Encrypts a given file with AES-256 in GCM mode of operation * <p>/*from ww w. j a va2s.c om*/ * Reads data from the InputStream and writes the encrypted data to the * OutputStream * * @param inputFile * @param outputFile * @throws java.io.IOException * @throws java.security.InvalidKeyException * @throws java.security.InvalidAlgorithmParameterException * @throws javax.crypto.BadPaddingException * @throws javax.crypto.IllegalBlockSizeException */ public void encrypt_V00(File inputFile, File outputFile) throws IOException, InvalidKeyException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException { // defining I/O streams InputStream input = new FileInputStream(inputFile); OutputStream output = new FileOutputStream(outputFile); // getting the encryption password char[] pass = DefaultCipher.getEncryptionPassword(); long fileSize = inputFile.length(); // generating Sx, Nx, R and Kx final byte[] S1 = GPCrypto.randomGen(S_BYTES), S2 = GPCrypto.randomGen(S_BYTES), epoch = DatatypeConverter.parseHexBinary(Long.toHexString(NTP.getTime() / 1000)), N1 = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, epoch[0], epoch[1], epoch[2], epoch[3] }, N2 = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, epoch[0], epoch[1], epoch[2], (byte) (epoch[3] + 0x01) }, R = GPCrypto.randomGen(R_BYTES); final SecretKey K1 = new SecretKeySpec(SCrypt.generate(GPCrypto.charToByte(pass), S1, (int) Math.pow(2, K1_KDF_N), KDF_r, KDF_p, CIPHER_KEY_BITS / 8), 0, CIPHER_KEY_BITS / 8, "AES"), K2 = new SecretKeySpec( SCrypt.generate(R, S2, (int) Math.pow(2, K2_KDF_N), KDF_r, KDF_p, CIPHER_KEY_BITS / 8), 0, CIPHER_KEY_BITS / 8, "AES"); // writing header this.cipher.init(Cipher.ENCRYPT_MODE, K1, new GCMParameterSpec(GCM_TAG_BITS, N1, 0, GCM_NONCE_BYTES)); output.write((byte) 0x00); output.write(S1); output.write(N1); output.write(DatatypeConverter.parseHexBinary(Integer.toHexString(K1_KDF_N))); output.write(cipher.doFinal(R)); output.write(S2); output.write(N2); output.write(DatatypeConverter.parseHexBinary(Integer.toHexString(K2_KDF_N))); // encrypting file this.cipher.init(Cipher.ENCRYPT_MODE, K2, new GCMParameterSpec(GCM_TAG_BITS, N2, 0, GCM_NONCE_BYTES)); int r = 0, counter = 0; while ((r = input.read(buf)) != -1) { counter++; if (r == BUFFER_SIZE) { if (counter * BUFFER_SIZE != fileSize) { output.write(this.cipher.update(buf)); } else { output.write(this.cipher.doFinal(buf)); } } else { output.write(this.cipher.doFinal(Arrays.copyOfRange(buf, 0, r))); } } // erasing cryptographic parameters and closing streams GPCrypto.eraseByteArrays(S1, S2, epoch, N1, N2, R); GPCrypto.sanitize(buf, 1000); GPCrypto.eraseKeys(K1, K2); GPCrypto.sanitize(pass); input.close(); output.close(); }
From source file:org.gity.internal.crypto.GCMCopy.java
License:Open Source License
/** * Decrypts a given file with AES-256 in GCM mode of operation * * @param inputFile// ww w .j a v a 2 s .c om * @param outputFile * @throws java.io.IOException * @throws java.security.InvalidKeyException * @throws java.security.InvalidAlgorithmParameterException * @throws javax.crypto.BadPaddingException * @throws javax.crypto.IllegalBlockSizeException */ public void decrypt_V00(File inputFile, File outputFile) throws IOException, InvalidKeyException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException { // defining I/O streams OutputStream output = new FileOutputStream(outputFile); InputStream input = new FileInputStream(inputFile); // getting the encryption password char[] pass = DefaultCipher.getEncryptionPassword(); long fileSize = inputFile.length(), iterCnt = fileSize / BUFFER_SIZE, percentage = fileSize / 100L, bytesRead = 0L; // skipping version byte and reading header input.skip(1); byte[] header = new byte[234]; input.read(header, 0, 234); // reading Sx, Nx, scrypt factors and generating K1 final byte[] S1 = Arrays.copyOfRange(header, 0, VS1), N1 = Arrays.copyOfRange(header, VS1, S1N1), K1_N = Arrays.copyOfRange(header, S1N1, N1K1N), S2 = Arrays.copyOfRange(header, K1NR, RS2), N2 = Arrays.copyOfRange(header, RS2, S2N2), K2_N = Arrays.copyOfRange(header, S2N2, N2K2N); final int K1_N_bak = (int) Math.pow(2, Integer.valueOf(Hex.toHexString(K1_N), 16)), K2_N_bak = (int) Math.pow(2, Integer.valueOf(Hex.toHexString(K2_N), 16)); final SecretKey K1 = new SecretKeySpec( SCrypt.generate(GPCrypto.charToByte(pass), S1, K1_N_bak, KDF_r, KDF_p, CIPHER_KEY_BITS / 8), 0, CIPHER_KEY_BITS / 8, "AES"); // reading E(K1, N1, R) this.cipher.init(Cipher.DECRYPT_MODE, K1, new GCMParameterSpec(GCM_TAG_BITS, N1, 0, GCM_NONCE_BYTES)); final byte[] R = cipher.doFinal(Arrays.copyOfRange(header, N1K1N, K1NR)); // generating K2 final SecretKey K2 = new SecretKeySpec(SCrypt.generate(R, S2, K2_N_bak, KDF_r, KDF_p, CIPHER_KEY_BITS / 8), 0, CIPHER_KEY_BITS / 8, "AES"); // decrypting file this.cipher.init(Cipher.DECRYPT_MODE, K2, new GCMParameterSpec(GCM_TAG_BITS, N2, 0, GCM_NONCE_BYTES)); int r = 0, counter = 0; while ((r = input.read(buf)) != -1) { counter++; if (r == BUFFER_SIZE) { if (counter * BUFFER_SIZE != fileSize) { output.write(this.cipher.update(buf)); } else { output.write(this.cipher.doFinal(buf)); } } else { output.write(this.cipher.doFinal(Arrays.copyOfRange(buf, 0, r))); } } // erasing cryptographic parameters and closing streams GPCrypto.eraseByteArrays(header, S1, S2, N1, N2, R); GPCrypto.sanitize(buf, 1000); GPCrypto.eraseKeys(K1, K2); GPCrypto.sanitize(pass); output.close(); }
From source file:org.hyperledger.common.MasterPrivateKey.java
License:Apache License
/** * Create a MasterPrivateKey from a seed, that is assumed to be encrypted. In practice often simply random. * * @param passphrase - passphrase for decryption * @param encryptedSeed the seed/*ww w . ja v a 2s. c om*/ * @return (re-)created MasterPrivateKey * @throws HyperLedgerException for any error in used libraries */ public static MasterPrivateKey createFromEncryptedSeed(String passphrase, byte[] encryptedSeed) throws HyperLedgerException { try { byte[] key = SCrypt.generate(passphrase.getBytes("UTF-8"), BITCOIN_SEED, 16384, 8, 8, 32); SecretKeySpec keyspec = new SecretKeySpec(key, "AES"); if (encryptedSeed.length != 32) { throw new HyperLedgerException("Incorrect encrypted seed length"); } Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC"); cipher.init(Cipher.DECRYPT_MODE, keyspec); return create(cipher.doFinal(encryptedSeed)); } catch (UnsupportedEncodingException | NoSuchPaddingException | NoSuchProviderException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) { throw new HyperLedgerException(e); } }
From source file:org.hyperledger.common.MasterPrivateKey.java
License:Apache License
/** * Re-create a MasterPrivateKey from encrypted serialization * * @param passphrase passphrase/*from ww w . ja v a 2 s . c om*/ * @param encrypted cipher text from encrypt * @return * @throws HyperLedgerException error in used libraries or wrong format */ public static MasterPrivateKey decrypt(String passphrase, byte[] encrypted) throws HyperLedgerException { try { byte[] key = SCrypt.generate(passphrase.getBytes("UTF-8"), BITCOIN_SEED, 16384, 8, 8, 32); SecretKeySpec keyspec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); byte[] iv = Arrays.copyOfRange(encrypted, 0, 16); byte[] data = Arrays.copyOfRange(encrypted, 16, encrypted.length); cipher.init(Cipher.DECRYPT_MODE, keyspec, new IvParameterSpec(iv)); return MasterPrivateKey.parse(new String(cipher.doFinal(data))); } catch (UnsupportedEncodingException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchProviderException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) { throw new HyperLedgerException(e); } }
From source file:org.hyperledger.common.MasterPrivateKey.java
License:Apache License
/** * Encrypt this key with AES/CBC/PKCS5Padding. Useful if you decide to store it. * * @param passphrase - passphrase//from www . jav a2 s . c o m * @param production - determines the Base58 serialization that will then be encrypted. * @return ciphertext * @throws HyperLedgerException for any error in the used libraries */ public byte[] encrypt(String passphrase, boolean production) throws HyperLedgerException { try { byte[] key = SCrypt.generate(passphrase.getBytes("UTF-8"), BITCOIN_SEED, 16384, 8, 8, 32); SecretKeySpec keyspec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, keyspec); byte[] iv = cipher.getIV(); byte[] c = cipher.doFinal(serialize(production).getBytes()); byte[] result = new byte[iv.length + c.length]; System.arraycopy(iv, 0, result, 0, iv.length); System.arraycopy(c, 0, result, iv.length, c.length); return result; } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { throw new HyperLedgerException(e); } }
From source file:org.hyperledger.common.MasterPublicKey.java
License:Apache License
/** * Re-create a MasterPublicKey from encrypted serialization. * * @param passphrase - passphrase//from w w w .j a va2 s.c o m * @param encrypted - the cipher text returned by encrypt * @return * @throws HyperLedgerException error in used libraries or wrong format */ public static MasterPublicKey decrypt(String passphrase, byte[] encrypted) throws HyperLedgerException { try { byte[] key = SCrypt.generate(passphrase.getBytes("UTF-8"), BITCOIN_SEED, 16384, 8, 8, 32); SecretKeySpec keyspec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); byte[] iv = Arrays.copyOfRange(encrypted, 0, 16); byte[] data = Arrays.copyOfRange(encrypted, 16, encrypted.length); cipher.init(Cipher.DECRYPT_MODE, keyspec, new IvParameterSpec(iv)); return MasterPublicKey.parse(new String(cipher.doFinal(data))); } catch (UnsupportedEncodingException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchProviderException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) { throw new HyperLedgerException(e); } }