fork download
  1. // MD5 Padding
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <bitset>
  6. using namespace std;
  7.  
  8. // Converts a decimal number to an 8-bit binary string
  9. string decToBinary8(int n) {
  10. return bitset<8>(n).to_string();
  11. }
  12.  
  13. // Converts a 64-bit integer to little-endian byte-wise binary strings
  14. vector<string> lengthTo64BitLittleEndian(uint64_t bitLength) {
  15. vector<string> res;
  16. for (int i = 0; i < 8; i++) {
  17. res.push_back(decToBinary8(bitLength & 0xFF)); // Get the least significant byte
  18. bitLength >>= 8; // Shift right by 8 bits
  19. }
  20. return res;
  21. }
  22.  
  23. int main() {
  24. string inp;
  25. cout << "Give the original message to check: ";
  26. getline(cin, inp);
  27.  
  28. vector<string> inputChain;
  29. cout << "\n1. Original message in binary:\n";
  30. for (char c : inp) {
  31. string binChar = decToBinary8((int)c);
  32. inputChain.push_back(binChar);
  33. cout << binChar << " ";
  34. }
  35.  
  36. int originalBits = inp.size() * 8;
  37. cout << "\n\n2. Total message length in bits: " << originalBits << endl;
  38.  
  39. // Padding starts with '1' bit followed by '0's
  40. vector<string> paddingChain;
  41. paddingChain.push_back("10000000"); // First '1' bit (0x80)
  42.  
  43. // Calculate padding length: make total (message + padding) ≡ 448 mod 512
  44. int totalBitsBeforeLength = originalBits + 8; // +8 for the initial 0x80 byte
  45. int paddingBitsNeeded = (448 - totalBitsBeforeLength % 512 + 512) % 512;
  46. int paddingBytes = paddingBitsNeeded / 8;
  47.  
  48. for (int i = 0; i < paddingBytes; i++) {
  49. paddingChain.push_back("00000000");
  50. }
  51.  
  52. cout << "\n3. Padding bytes (including 0x80): " << paddingChain.size() << endl;
  53. cout << "Padding in binary:\n";
  54. for (string p : paddingChain) {
  55. cout << p << " ";
  56. }
  57.  
  58. // Message length in 64-bit little-endian format
  59. vector<string> lengthChain = lengthTo64BitLittleEndian(originalBits);
  60. cout << "\n\n4. 64-bit message length in little-endian binary:\n";
  61. for (string l : lengthChain) {
  62. cout << l << " ";
  63. }
  64.  
  65. // Combine everything
  66. cout << "\n\n5. Final message in binary (message + padding + length):\n";
  67. for (string s : inputChain) cout << s << " ";
  68. for (string s : paddingChain) cout << s << " ";
  69. for (string s : lengthChain) cout << s << " ";
  70.  
  71. cout << "\n\nTotal final size in bits: " << inputChain.size()*8 + paddingChain.size()*8 + 64 << endl;
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Give the original message to check: 
1. Original message in binary:


2. Total message length in bits: 0

3. Padding bytes (including 0x80): 56
Padding in binary:
10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 

4. 64-bit message length in little-endian binary:
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 

5. Final message in binary (message + padding + length):
10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 

Total final size in bits: 512