Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. SHA3_Sym_Enc(key,message)
  2. {
  3. 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
  4.  
  5. keystream="";
  6. int counter=0;
  7.  
  8. for(read_message=0;read_message<message_length;read_message+=base_key.length()) //work in blocks of SHA3 outputs
  9. {
  10. hash_block=SHA3(base_key+string(counter)); //H(base_key+#) -> block#
  11. keystream+=hash_block; //add blocks into one big pseudorandom output
  12. counter++;
  13. }
  14.  
  15. keystream=keystream.Left(message.length()); //our keystream works in blocks, and might output bigger than the message, so trim any excess
  16.  
  17. return message^keystream; //XOR the keystream with the message, output
  18. }
  19.  
  20. void main()
  21. {
  22. scanf(key); //input user key
  23.  
  24. nonce=generate_random_bytes(sizeof(SHA3)); //generate a nonce, SHA3-length should be enough
  25.  
  26. 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
  27.  
  28. ciphertext=SHA3_Sym_Enc(plaintext,message_key); //output the encrypted message via the function described earlier
  29.  
  30. 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
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement