public void SetMessageKeys(IEcPublicKey senderEphemeral, MessageKeys messageKeys) { Pair <SessionStructure.Types.Chain, uint> chainAndIndex = GetReceiverChain(senderEphemeral); SessionStructure.Types.Chain chain = chainAndIndex.First(); SessionStructure.Types.Chain.Types.MessageKey messageKeyStructure = new SessionStructure.Types.Chain.Types.MessageKey { CipherKey = ByteString.CopyFrom(messageKeys.GetCipherKey()), MacKey = ByteString.CopyFrom(messageKeys.GetMacKey()), Index = messageKeys.GetCounter(), Iv = ByteString.CopyFrom(messageKeys.GetIv()) }; chain.MessageKeys.Add(messageKeyStructure); if (chain.MessageKeys.Count > MaxMessageKeys) { chain.MessageKeys.RemoveAt(0); } _sessionStructure.ReceiverChains[(int)chainAndIndex.Second()] = chain; }
public void SetMessageKeys(ECPublicKey senderEphemeral, MessageKeys messageKeys) { Pair <Chain, uint> chainAndIndex = GetReceiverChain(senderEphemeral); Chain chain = chainAndIndex.First(); Chain.Types.MessageKey messageKeyStructure = Chain.Types.MessageKey.CreateBuilder() .SetCipherKey(ByteString.CopyFrom(messageKeys.GetCipherKey() /*.getEncoded()*/)) .SetMacKey(ByteString.CopyFrom(messageKeys.GetMacKey() /*.getEncoded()*/)) .SetIndex(messageKeys.GetCounter()) .SetIv(ByteString.CopyFrom(messageKeys.GetIv() /*.getIV()*/)) .Build(); Chain.Builder updatedChain = chain.ToBuilder().AddMessageKeys(messageKeyStructure); if (updatedChain.MessageKeysList.Count > MAX_MESSAGE_KEYS) { updatedChain.MessageKeysList.RemoveAt(0); } this.sessionStructure = this.sessionStructure.ToBuilder() .SetReceiverChains((int)chainAndIndex.Second(), updatedChain.Build()) // TODO: conv .Build(); }
private byte[] GetPlaintext(uint version, MessageKeys messageKeys, byte[] cipherText) { try { //Cipher cipher; if (version >= 3) { //cipher = getCipher(Cipher.DECRYPT_MODE, messageKeys.getCipherKey(), messageKeys.getIv()); return(Util.Decrypt.AesCbcPkcs5(cipherText, messageKeys.GetCipherKey(), messageKeys.GetIv())); } else { //cipher = getCipher(Cipher.DECRYPT_MODE, messageKeys.getCipherKey(), messageKeys.getCounter()) return(Util.Decrypt.AesCtr(cipherText, messageKeys.GetCipherKey(), messageKeys.GetCounter())); } } catch (/*IllegalBlockSizeException | BadPadding*/ Exception e) { throw new InvalidMessageException(e); } }
private byte[] GetCiphertext(uint version, MessageKeys messageKeys, byte[] plaintext) { try { if (version >= 3) { //cipher = getCipher(Cipher.ENCRYPT_MODE, messageKeys.getCipherKey(), messageKeys.getIv()); return(Util.Encrypt.AesCbcPkcs5(plaintext, messageKeys.GetCipherKey(), messageKeys.GetIv())); } else { //cipher = getCipher(Cipher.ENCRYPT_MODE, messageKeys.getCipherKey(), messageKeys.getCounter()); return(Util.Encrypt.AesCtr(plaintext, messageKeys.GetCipherKey(), messageKeys.GetCounter())); } } catch (/*IllegalBlockSizeException | BadPadding*/ Exception e) { throw new Exception(e.Message); } }