public static PrivateKey unpackWithPassword(byte[] packedBinary, String password) throws EncryptionError { List params = Boss.load(packedBinary); if ((Integer) params.get(0) == 0) { return new PrivateKey(packedBinary); } else if ((Integer) params.get(0) == 1) { throw new EncryptionError("the key is public, not private"); } else if ((Integer) params.get(0) == 2) { try { int rounds = (int) params.get(1); Bytes salt = (Bytes) params.get(2); String function = (String) params.get(3); Bytes packedEncryptedKey = (Bytes) params.get(4); Bytes digest = (Bytes) params.get(5); SymmetricKey key = new KeyInfo(KeyInfo.PRF.valueOf(function), rounds, salt.getData(), null) .derivePassword(password); byte[] packedKey = key.decrypt(packedEncryptedKey.getData()); byte[] resDigest = new Crc32().update(packedKey).digest(); if (!digest.equals(new Bytes(resDigest))) { throw new PasswordProtectedException("wrong password"); } return new PrivateKey(packedKey); } catch (Exception e) { if (e instanceof PasswordProtectedException) throw e; throw new EncryptionError("failed to parse password protected private key", e); } } else { throw new EncryptionError("Bad or unknown private key type"); } }
@Test public void bytesRange() throws Exception { SymmetricKey k = new SymmetricKey(); byte[] src = new byte[4]; src[0] = (byte) 255; src[1] = (byte) 254; src[2] = (byte) 253; src[3] = (byte) 252; assertThat(k.decrypt(k.encrypt(src)), equalTo(src)); }
@Test public void bigVolume() throws Exception { SymmetricKey k = new SymmetricKey(); byte[] src = CTRTransformer.randomBytes(0x23456); assertThat(k.decrypt(k.encrypt(src)), equalTo(src)); }
); Binder data = Boss.unpack( session.getSessionKey().decrypt(a.data.getBinaryOrThrow("result")) ); Binder result = data.getBinder("result", null);
public Binder command(Binder params) throws ClientError, EncryptionError { // decrypt params and execute command Binder result = null; try { result = Binder.fromKeysValues( "result", executeAuthenticatedCommand( Boss.unpack( sessionKey.decrypt(params.getBinaryOrThrow("params")) ) ) ); } catch (Exception e) { ErrorRecord r = (e instanceof ClientError) ? ((ClientError) e).getErrorRecord() : new ErrorRecord(Errors.COMMAND_FAILED, "", e.getMessage()); result = Binder.fromKeysValues( "error", r ); } // encrypt and return result return Binder.fromKeysValues( "result", sessionKey.encrypt(Boss.pack(result)) ); }