public byte[] packWithPassword(String password) throws EncryptionError { byte[] packedKey = pack(); byte[] salt = getClass().getCanonicalName().getBytes(); int rounds = getKDFRounds(); KeyInfo.PRF function = KeyInfo.PRF.HMAC_SHA256; SymmetricKey key = new KeyInfo(function, rounds, salt, null) .derivePassword(password); byte[] packedEncryptedKey = key.encrypt(packedKey); return Boss.dumpToArray(new Object[]{ 2, rounds, salt, function.name(), packedEncryptedKey, new Crc32().update(packedKey).digest() }); }
@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)); }
Answer a = requestOrThrow("command", "command", "command", "params", session.getSessionKey().encrypt(Boss.pack(call)), "session_id", session.getSessionId() );
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)) ); }