/* this function encrypts a message*/ void EncryptMessage(HC128_State state, Byte[] message, Byte[] ciphertext, UInt64 msglength) { UInt64 i; UInt32 j; int messageIndex = 0, ciphertextIndex = 0; /*encrypt a message, each time 4 bytes are encrypted*/ for (i = 0; (i + 4) <= msglength; i += 4, messageIndex += 4, ciphertextIndex += 4) { /*generate 32-bit keystream and store it in state.keystreamword*/ OneStep(state); { /*encrypt 32 bits of the message*/ Byte[] ciphertextBytes = ConvertUtil.ConvertUInt32ToBytes(BitConverter.ToUInt32(message, messageIndex) ^ state.keystreamword); for (int byteIndex = 0; byteIndex < 4; byteIndex++) { ciphertext[ciphertextIndex + byteIndex] = ciphertextBytes[byteIndex]; } } } /*encrypt the last message block if the message length is not multiple of 4 bytes*/ if ((msglength & 3) != 0) { OneStep(state); for (j = 0; j < (msglength & 3); j++) { ciphertext[j] = (byte)(message[j] ^ BitConverter.GetBytes(state.counter1024)[j]); } } }