Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SHA3_Sym_Enc(key,message)
- {
- base_key=SHA3(key); //not really needed, but in case we will ever allow the key to be "anything", we'll standardize the initial value like this
- keystream="";
- int counter=0;
- for(read_message=0;read_message<message_length;read_message+=base_key.length()) //work in blocks of SHA3 outputs
- {
- hash_block=SHA3(base_key+string(counter)); //H(base_key+#) -> block#
- keystream+=hash_block; //add blocks into one big pseudorandom output
- counter++;
- }
- keystream=keystream.Left(message.length()); //our keystream works in blocks, and might output bigger than the message, so trim any excess
- return message^keystream; //XOR the keystream with the message, output
- }
- void main()
- {
- scanf(key); //input user key
- nonce=generate_random_bytes(sizeof(SHA3)); //generate a nonce, SHA3-length should be enough
- message_key=SHA3(key+nonce); //generate a random nonce, then build the actual key that is going to encrypt the message via concat and hash
- ciphertext=SHA3_Sym_Enc(plaintext,message_key); //output the encrypted message via the function described earlier
- final_message=ciphertext+SHA3(ciphertext+message_key)+nonce; //append a MAC and the nonce to the message, note that we are not doing HMAC because SHA3 is supposedly immune to length extension attacks
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement