2024-10-15 15:37:22 +05:30
|
|
|
#include <cstdint>
|
2024-09-05 21:08:04 +05:30
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
#include <blowfish/blowfish2.h>
|
2024-10-15 15:37:22 +05:30
|
|
|
#include <ostream>
|
2024-09-05 21:08:04 +05:30
|
|
|
|
|
|
|
|
std::string from_uint(uint64_t sh) {
|
|
|
|
|
std::string re("");
|
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
|
re += (unsigned char)(sh >> i * 8);
|
|
|
|
|
}
|
|
|
|
|
return re;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-15 15:37:22 +05:30
|
|
|
uint64_t to_uint(std::string &s, size_t index, size_t size) {
|
|
|
|
|
return *reinterpret_cast<uint64_t *>(
|
|
|
|
|
const_cast<char *>(s.substr(index, size).c_str()));
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 21:08:04 +05:30
|
|
|
int main(int argc, char const *argv[]) {
|
|
|
|
|
std::string key("test@pass47");
|
|
|
|
|
std::string message("My name is Avinal and I am cute and smart");
|
|
|
|
|
std::string cipher("");
|
|
|
|
|
int len = message.length();
|
2024-10-15 15:37:22 +05:30
|
|
|
int J = sizeof(uint64_t);
|
2024-09-05 21:08:04 +05:30
|
|
|
int rem =
|
2024-10-15 15:37:22 +05:30
|
|
|
((len > J * 2) ? (((len / J * 2) + 1) * J * 2 - len) : (J * 2 - len));
|
2024-09-05 21:08:04 +05:30
|
|
|
message.append(rem, '\0');
|
|
|
|
|
len = message.length();
|
2024-10-15 15:37:22 +05:30
|
|
|
|
2024-09-05 21:08:04 +05:30
|
|
|
Blowfish2 blowfish(key);
|
2024-10-15 15:37:22 +05:30
|
|
|
|
|
|
|
|
std::cout << "My message is: " << message << J << std::endl;
|
|
|
|
|
uint64_t L = 0, R = 0;
|
2024-09-05 21:08:04 +05:30
|
|
|
for (size_t i = 0; i < len; i += 16) {
|
2024-10-15 15:37:22 +05:30
|
|
|
L = to_uint(message, i, J);
|
|
|
|
|
R = to_uint(message, i + J, J);
|
|
|
|
|
blowfish.encrypt(L, R);
|
|
|
|
|
cipher += from_uint(L) + from_uint(R);
|
2024-09-05 21:08:04 +05:30
|
|
|
}
|
2024-10-15 15:37:22 +05:30
|
|
|
std::cout << "Cipher: " << cipher << std::endl;
|
|
|
|
|
|
2024-09-05 21:08:04 +05:30
|
|
|
std::string decipher("");
|
|
|
|
|
len = cipher.length();
|
|
|
|
|
std::cout << "length: " << len << std::endl;
|
|
|
|
|
for (size_t i = 0; i < len; i += 16) {
|
2024-10-15 15:37:22 +05:30
|
|
|
L = to_uint(cipher, i, J);
|
|
|
|
|
R = to_uint(cipher, i + J, J);
|
|
|
|
|
blowfish.decrypt(L, R);
|
|
|
|
|
decipher += from_uint(L) + from_uint(R);
|
2024-09-05 21:08:04 +05:30
|
|
|
}
|
|
|
|
|
if (message == decipher) {
|
2024-10-15 15:37:22 +05:30
|
|
|
std::cout << "Test OK." << std::endl;
|
2024-09-05 21:08:04 +05:30
|
|
|
} else {
|
2024-10-15 15:37:22 +05:30
|
|
|
std::cout << "Test failed." << std::endl;
|
2024-09-05 21:08:04 +05:30
|
|
|
}
|
2024-10-15 15:37:22 +05:30
|
|
|
// C Blowfish 2 tests
|
|
|
|
|
|
|
|
|
|
L = 0x0000000000000001, R = 0x0000000000000002;
|
|
|
|
|
|
|
|
|
|
blowfish.initialize("TESTKEY");
|
|
|
|
|
blowfish.encrypt(L, R);
|
|
|
|
|
if (L == 0x7B2B9DE71D1B1C62 && R == 0x91C230351177BEE8)
|
|
|
|
|
std::cout << "Test encryption OK." << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "Test encryption failed." << std::endl;
|
|
|
|
|
|
|
|
|
|
blowfish.decrypt(L, R);
|
|
|
|
|
if (L == 1 && R == 2)
|
|
|
|
|
std::cout << "Test decryption OK." << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "Test decryption failed." << std::endl;
|
|
|
|
|
|
|
|
|
|
L = 0x0102030405060708;
|
|
|
|
|
R = 0x0910111213141516;
|
|
|
|
|
|
|
|
|
|
blowfish.initialize("A");
|
|
|
|
|
blowfish.encrypt(L, R);
|
|
|
|
|
if (L == 0xCA38165603F9915C && R == 0x61F0776A0F55E807)
|
|
|
|
|
std::cout << "Test encryption OK." << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "Test encryption failed." << std::endl;
|
|
|
|
|
|
|
|
|
|
blowfish.decrypt(L, R);
|
|
|
|
|
if (L == 0x0102030405060708 && R == 0x0910111213141516)
|
|
|
|
|
std::cout << "Test decryption OK." << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "Test decryption failed." << std::endl;
|
|
|
|
|
|
|
|
|
|
L = 0x0102030405060708;
|
|
|
|
|
R = 0x0910111213141516;
|
|
|
|
|
|
|
|
|
|
blowfish.initialize("B");
|
|
|
|
|
blowfish.encrypt(L, R);
|
|
|
|
|
if (L == 0xD07690A78B109983 && R == 0x8DDF85826F2366C2)
|
|
|
|
|
std::cout << "Test encryption OK." << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "Test encryption failed." << std::endl;
|
|
|
|
|
|
|
|
|
|
blowfish.decrypt(L, R);
|
|
|
|
|
if (L == 0x0102030405060708 && R == 0x0910111213141516)
|
|
|
|
|
std::cout << "Test decryption OK." << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "Test decryption failed." << std::endl;
|
2024-09-05 21:08:04 +05:30
|
|
|
}
|