/** * Encrypt a message. * * @param paddedPlaintext The plaintext message bytes, optionally padded. * @return Ciphertext. * @throws NoSessionException */ public byte[] encrypt(byte[] paddedPlaintext) { lock (LOCK) { try { SenderKeyRecord record = senderKeyStore.loadSenderKey(senderKeyId); SenderKeyState senderKeyState = record.getSenderKeyState(); SenderMessageKey senderKey = senderKeyState.getSenderChainKey().getSenderMessageKey(); byte[] ciphertext = getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext); SenderKeyMessage senderKeyMessage = new SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate()); senderKeyState.setSenderChainKey(senderKeyState.getSenderChainKey().getNext()); senderKeyStore.storeSenderKey(senderKeyId, record); return(senderKeyMessage.serialize()); } catch (InvalidKeyIdException e) { throw new NoSessionException(e); } } }
public void addSenderMessageKey(SenderMessageKey senderMessageKey) { SenderKeyStateStructure.Types.SenderMessageKey senderMessageKeyStructure = SenderKeyStateStructure.Types.SenderMessageKey.CreateBuilder() .SetIteration(senderMessageKey.getIteration()) .SetSeed(ByteString.CopyFrom(senderMessageKey.getSeed())) .Build(); this.senderKeyStateStructure = this.senderKeyStateStructure.ToBuilder() .AddSenderMessageKeys(senderMessageKeyStructure) .Build(); }
public void addSenderMessageKey(SenderMessageKey senderMessageKey) { SenderKeyStateStructure.Types.SenderMessageKey senderMessageKeyStructure = new SenderKeyStateStructure.Types.SenderMessageKey { Iteration = senderMessageKey.getIteration(), Seed = ByteString.CopyFrom(senderMessageKey.getSeed()) }; this.senderKeyStateStructure.SenderMessageKeys.Add(senderMessageKeyStructure); if (senderKeyStateStructure.SenderMessageKeys.Count > MAX_MESSAGE_KEYS) { senderKeyStateStructure.SenderMessageKeys.RemoveAt(0); } }
public void addSenderMessageKey(SenderMessageKey senderMessageKey) { SenderKeyStateStructure.Types.SenderMessageKey senderMessageKeyStructure = SenderKeyStateStructure.Types.SenderMessageKey.CreateBuilder() .SetIteration(senderMessageKey.getIteration()) .SetSeed(ByteString.CopyFrom(senderMessageKey.getSeed())) .Build(); SenderKeyStateStructure.Builder builder = this.senderKeyStateStructure.ToBuilder(); builder.AddSenderMessageKeys(senderMessageKeyStructure); if (builder.SenderMessageKeysList.Count > MAX_MESSAGE_KEYS) { builder.SenderMessageKeysList.RemoveAt(0); } this.senderKeyStateStructure = builder.Build(); }