List of usage examples for org.bouncycastle.util Arrays copyOfRange
public static BigInteger[] copyOfRange(BigInteger[] original, int from, int to)
From source file:Algorithms.Asymmetric.blockElGamal.java
@Override public CrypticObject encrypt(byte[] message) { context = new ClassPathXmlApplicationContext("spring.xml"); algo = (CrypticAlgo) context.getBean("basicElGamal"); CrypticObject tmp;//from w w w .j av a 2 s . c o m crypt = new CrypticObject(); crypt.data = msgBytes; int sTime, eTime; beg = 0; length = message.length; sTime = eTime = 0; while (beg < length) { end = (beg + 19 >= length) ? (length) : (beg + 19); msgBytes = Arrays.copyOfRange(message, beg, end); tmp = algo.encrypt(msgBytes); crypt.time += tmp.time; crypt.data = Arrays.concatenate(crypt.data, tmp.data); beg += 19; } return crypt; }
From source file:Algorithms.Asymmetric.blockElGamal.java
@Override public CrypticObject decrypt(byte[] message) { CrypticObject tmp;//from www . j a v a 2s. c o m crypt = new CrypticObject(); int sTime, eTime; beg = 0; length = message.length; sTime = eTime = 0; while (beg < length) { end = (beg + 40 >= length) ? (length) : (beg + 40); msgBytes = Arrays.copyOfRange(message, beg, end); tmp = algo.decrypt(msgBytes); crypt.time += tmp.time; crypt.data = Arrays.concatenate(crypt.data, tmp.data); beg += 40; } return crypt; }
From source file:Algorithms.Asymmetric.blockRSA.java
@Override public CrypticObject encrypt(byte[] message) { context = new ClassPathXmlApplicationContext("spring.xml"); algo = (CrypticAlgo) context.getBean("basicRSA"); CrypticObject tmp;/* ww w.j a v a2 s. c om*/ crypt = new CrypticObject(); crypt.data = msgBytes; int sTime, eTime; beg = 0; length = message.length; sTime = eTime = 0; while (beg < length) { end = (beg + 245 >= length) ? (length) : (beg + 245); msgBytes = Arrays.copyOfRange(message, beg, end); tmp = algo.encrypt(msgBytes); crypt.time += tmp.time; crypt.data = Arrays.concatenate(crypt.data, tmp.data); beg += 245; } return crypt; }
From source file:Algorithms.Asymmetric.blockRSA.java
@Override public CrypticObject decrypt(byte[] message) { CrypticObject tmp;/* w w w. ja va2s .c o m*/ crypt = new CrypticObject(); int sTime, eTime; beg = 0; length = message.length; sTime = eTime = 0; while (beg < length) { end = (beg + 256 >= length) ? (length) : (beg + 256); msgBytes = Arrays.copyOfRange(message, beg, end); tmp = algo.decrypt(msgBytes); crypt.time += tmp.time; crypt.data = Arrays.concatenate(crypt.data, tmp.data); beg += 256; } return crypt; }
From source file:cc.agentx.wrapper.FakedHttpWrapper.java
License:Apache License
public byte[] unwrapFromResponse(final byte[] bytes) { // caution: placeholder bytes' end-pos must less than 200 String fuzzyHeader = new String(Arrays.copyOfRange(bytes, 0, 200)); if (!fuzzyHeader.startsWith(Http.VERSION_1_1)) { throw new RuntimeException("unknown format"); }/* www . ja va 2 s . c o m*/ fuzzyHeader = fuzzyHeader.substring(fuzzyHeader.indexOf("Content-Length: ") + "Content-Length: ".length()); fuzzyHeader = fuzzyHeader.substring(0, fuzzyHeader.indexOf(Http.CRLF)); int rawLen = Integer.parseInt(fuzzyHeader); return Arrays.copyOfRange(bytes, bytes.length - rawLen, bytes.length); }
From source file:cc.telepath.phage.util.Crypto.java
License:GNU General Public License
/** * Encrypt and encode a message with our AES key. * @param message//w ww.j av a 2 s. c om * @param AESKey * @return */ public byte[] AESEncrypt(byte[] message, String AESKey) { Base64 base64 = new Base64(); try { MessageDigest md = MessageDigest.getInstance("SHA512"); md.update(base64.decode(AESKey)); byte[] IV = Arrays.copyOfRange(md.digest(), 0, 16); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); AlgorithmParameterSpec IVSpec = new IvParameterSpec(IV); c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(base64.decode(AESKey), "AES"), IVSpec); byte[] encryptData = c.doFinal(message); return encryptData; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:cc.telepath.phage.util.Crypto.java
License:GNU General Public License
/** * Decrypt a message with a given AES key * @param message/*w ww.j av a 2 s. c o m*/ * @param AESKey * @return */ public byte[] AESDecrypt(byte[] message, String AESKey) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { Base64 base64 = new Base64(); MessageDigest md = MessageDigest.getInstance("SHA512"); md.update(base64.decode(AESKey)); byte[] IV = Arrays.copyOfRange(md.digest(), 0, 16); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); AlgorithmParameterSpec IVSpec = new IvParameterSpec(IV); c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(base64.decode(AESKey), "AES"), IVSpec); byte[] decryptData = c.doFinal(message); return decryptData; }
From source file:cc.telepath.phage.util.Crypto.java
License:GNU General Public License
/** * Convert an arbitrary length password to a base64 encoded 256-bit AES key. * @param password/* w w w . jav a 2s .c o m*/ * @return * @throws NoSuchAlgorithmException * @throws NoSuchProviderException */ public String passwordToAESKey(String password) throws NoSuchAlgorithmException, NoSuchProviderException { Base64 base64 = new Base64(); MessageDigest md = MessageDigest.getInstance("SHA512", "BC"); md.update(password.getBytes()); byte[] passwordBytes = Arrays.copyOfRange(md.digest(), 0, 32); return new String(base64.encode(passwordBytes)); }
From source file:ch.lamacrypt.internal.crypto.CPCipher.java
License:Open Source License
/** * Decrypts a given file with ChaCha20 w/ Poly1305 as MAC in * encrypt-then-MAC scheme./*from www. j a va 2 s . c o m*/ * <p> * Reads data from the InputStream and writes the decrypted data to the * OutputStream * * @param key * @param nonce * @param input * @param output * @throws IOException */ protected void decrypt(byte[] key, byte[] nonce, InputStream input, OutputStream output) throws IOException { this.cipher.init(false, new ParametersWithIV(new KeyParameter(key), nonce)); byte[] computedMac = new byte[16], receivedMac = new byte[16], readBuf = new byte[BUFFER_SIZE], chachaBuf = new byte[BUFFER_SIZE]; initMAC(cipher); int r = 0; while ((r = input.read(readBuf)) != -1) { if (r == BUFFER_SIZE) { // use C in whole to update the MAC and decrypt updateMAC(readBuf, 0, r); cipher.processBytes(readBuf, 0, r, chachaBuf, 0); output.write(chachaBuf, 0, r); } else { // use all but the last 16 bytes from C to update the MAC and decrypt updateMAC(Arrays.copyOfRange(readBuf, 0, r - 16), 0, r - 16); cipher.processBytes(Arrays.copyOfRange(readBuf, 0, r - 16), 0, r - 16, chachaBuf, 0); output.write(chachaBuf, 0, r - 16); // copy the last 16 bytes as the original MAC receivedMac = Arrays.copyOfRange(readBuf, r - 16, r); } } // check if the two MACs match mac.doFinal(computedMac, 0); if (!Arrays.constantTimeAreEqual(computedMac, receivedMac)) { throw new TlsFatalAlert(AlertDescription.bad_record_mac); } }
From source file:com.amazonawsencryptionsdk.CryptoInputStreamTest.java
License:Open Source License
@Test public void encryptAPIComptability() throws IOException { final int ptSize = 1000000; // 1MB final byte[] plaintextBytes = TestIOUtils.generateRandomPlaintext(ptSize); final String inputFileName = sandboxPath_ + "plaintext_1MB.txt"; final String encryptedFileName = new String(inputFileName + ".enc"); Map<String, String> encryptionContext = new HashMap<String, String>(1); encryptionContext.put("ENC", "Test with %s" + inputFileName); encryptionClient_.setEncryptionFrameSize(AwsCrypto.getDefaultFrameSize()); // encryption final CryptoResult<byte[], JceMasterKey> cipherText = encryptionClient_.encryptData(customerMasterKey, plaintextBytes, encryptionContext); final OutputStream outStream = new FileOutputStream(encryptedFileName); outStream.write(cipherText.getResult()); outStream.close();//from w ww . ja v a2s. com // decryption final FileInputStream inStream = new FileInputStream(encryptedFileName); final InputStream decryptionInStream = encryptionClient_.createDecryptingStream(customerMasterKey, inStream); final byte[] decryptedBytes = new byte[cipherText.getResult().length]; int readLen = 0; int totalReadBytes = 0; while (readLen >= 0) { totalReadBytes += readLen; try { readLen = decryptionInStream.read(decryptedBytes, totalReadBytes, 1024); } catch (IOException e) { e.printStackTrace(); } } decryptionInStream.close(); final byte[] outBytes = Arrays.copyOfRange(decryptedBytes, 0, totalReadBytes); assertArrayEquals(plaintextBytes, outBytes); }