@Override public byte[] decrypt(final byte[] encrypted) throws EncryptionError { // mini-pooling of keys for parallel processing: if (inUse.getAndSet(true)) { // our copy is in use - create a copy for later use synchronized (copyMutex) { // we lock only to create a copy if (copy == null) copy = new PrivateKey(pack()); } // now the copy will do the same: encrypt or create a copy... return copy.decrypt(encrypted); } else { try { return privateKey.decrypt(encrypted); } finally { inUse.set(false); } } }
private void processHelloAnswer(Binder result) throws EncryptionError { byte[] data = result.getBinaryOrThrow("data"); byte[] signature = result.getBinaryOrThrow("signature"); setRemoteKey(result.getBinaryOrThrow("public_key")); if (!remoteKey.verify(data, signature, HashType.SHA256)) throw new EncryptionError("bad signature in hello answer"); Binder answer = Boss.unpack(myKey.decrypt(data)); if (!Arrays.equals(answer.getBinaryOrThrow("nonce"), myNonce)) throw new EncryptionError("nonce mismatch"); remoteSessionKey = new SymmetricKey(answer.getBinary("session_key")); }
@Test public void concurrencyTest() throws Exception { PrivateKey privateKey = new PrivateKey(2048); PublicKey publicKey = privateKey.getPublicKey(); ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(128); AtomicInteger errorsCount = new AtomicInteger(0); for (int i = 0; i < 10000; ++i) { executorService.submit(() -> { try { byte[] data = Bytes.random(128).getData(); byte[] encrypted = publicKey.encrypt(data); byte[] decrypted = privateKey.decrypt(encrypted); assertArrayEquals(data, decrypted); } catch (Exception e) { e.printStackTrace(); errorsCount.incrementAndGet(); } }); } executorService.shutdown(); executorService.awaitTermination(120, TimeUnit.SECONDS); assertEquals(0, errorsCount.get()); }
@Test public void testKeys() throws Exception { // Test vectors are for key #1 PrivateKey key = (PrivateKey) TestKeys.privateKey(1); byte[] encrypted = Bytes.fromBase64(encrypted64).toArray(); byte[] decrypted = key.decrypt(encrypted); assertEquals(plainText, new String(decrypted)); PublicKey publicKey = key.getPublicKey(); byte[] encrypted2 = publicKey.encrypt(plainText); assertEquals(plainText, new String(key.decrypt(encrypted2))); publicKey = new PublicKey(Do.decodeBase64(publicKey64)); encrypted2 = publicKey.encrypt(plainText); assertEquals(plainText, new String(key.decrypt(encrypted2))); }
byte[] sign = session.handshake_sessionPart2; if (new PublicKey(session.remoteNodeInfo.getPublicKey().pack()).verify(encrypted, sign, HashType.SHA512)) { byte[] decryptedData = new PrivateKey(ownPrivateKey.pack()).decrypt(encrypted); List data = Boss.load(decryptedData); byte[] sessionKey = ((Bytes) data.get(0)).toArray();
report(logLabel, ()->"received both parts of key_req from " + sessionReader.remoteNodeInfo.getNumber(), VerboseLevel.BASE); byte[] encrypted = sessionReader.handshake_keyReqPart1; byte[] packed = new PrivateKey(ownPrivateKey.pack()).decrypt(encrypted); byte[] sign = sessionReader.handshake_keyReqPart2; List nonceList = Boss.load(packed);
privateKey.decrypt( params.getBinaryOrThrow("encrypted_token")