From faaa11b3b5827b56b741244fb539c32160f6ab8f Mon Sep 17 00:00:00 2001 From: samsonjaw <114964564+samsonjaw@users.noreply.github.com> Date: Sun, 17 Mar 2024 15:12:46 +0800 Subject: [PATCH] Add files via upload add pkcs7 and ECB --- AES_HPP.cpp | 33 +++++++++++++++++++++++++ AES_HPP.hpp | 6 +++++ Base64_HPP.cpp | 2 ++ Base64_HPP.hpp | 2 +- main.cpp | 67 +++++++++++++++++++++++++++++++++----------------- pkcs7_HPP.cpp | 20 +++++++++++++++ pkcs7_HPP.hpp | 16 ++++++++++++ 7 files changed, 123 insertions(+), 23 deletions(-) create mode 100644 pkcs7_HPP.cpp create mode 100644 pkcs7_HPP.hpp diff --git a/AES_HPP.cpp b/AES_HPP.cpp index 21f7bd2..37a7470 100644 --- a/AES_HPP.cpp +++ b/AES_HPP.cpp @@ -1,5 +1,8 @@ #include"AES_HPP.hpp" +using namespace std; + + unsigned char S_BOX[256] = { 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, @@ -301,4 +304,34 @@ void decrypt(unsigned char* ciphertext, unsigned char* key, unsigned char* outpu output[j * 4 + i] = state[i][j]; } } +} + +void encryptECB(int textLength,unsigned char* plaintext, unsigned char* key, unsigned char* output) { + unsigned char block[16]; + for (int i = 0; i < textLength; i += 16) { + memcpy(block, plaintext + i, 16); + + unsigned char tmp[16]; + encrypt(block, key, tmp); + + for (int j = i; j < i + 16; j++) { + output[j] = tmp[j - i]; + } + } + return; +} + +void decryptECB(int textLength, unsigned char* ciphertext, unsigned char* key, unsigned char* output) { + unsigned char block[16]; + for (int i = 0; i < textLength; i += 16) { + memcpy(block, ciphertext + i, 16); + + unsigned char tmp[16]; + decrypt(block, key, tmp); + + for (int j = i; j < i + 16; j++) { + output[j] = tmp[j - i]; + } + } + return; } \ No newline at end of file diff --git a/AES_HPP.hpp b/AES_HPP.hpp index c876eb5..43a7f86 100644 --- a/AES_HPP.hpp +++ b/AES_HPP.hpp @@ -6,6 +6,8 @@ #include #include +using namespace std; + extern unsigned char S_BOX[256]; extern unsigned char INV_S_BOX[256]; @@ -40,4 +42,8 @@ void encrypt(unsigned char* plaintext, unsigned char* key, unsigned char* output void decrypt(unsigned char* ciphertext, unsigned char* key, unsigned char* output); +void encryptECB(int textLength, unsigned char* plaintext, unsigned char* key, unsigned char* output); + +void decryptECB(int textLength, unsigned char* plaintext, unsigned char* key, unsigned char* output); + #endif // AES_HPP \ No newline at end of file diff --git a/Base64_HPP.cpp b/Base64_HPP.cpp index 2384281..d96703a 100644 --- a/Base64_HPP.cpp +++ b/Base64_HPP.cpp @@ -1,5 +1,7 @@ #include "Base64_HPP.hpp" + using namespace std; + int sBase64[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', diff --git a/Base64_HPP.hpp b/Base64_HPP.hpp index 8a93a56..a24e288 100644 --- a/Base64_HPP.hpp +++ b/Base64_HPP.hpp @@ -14,4 +14,4 @@ unsigned char BaseValue(char c); string Base64_decode(string str); -#endif // !Base64_HPP +#endif // !Base64_HPP \ No newline at end of file diff --git a/main.cpp b/main.cpp index 46dbd88..edbd9cd 100644 --- a/main.cpp +++ b/main.cpp @@ -1,65 +1,81 @@ #include"AES_HPP.hpp" #include"Base64_HPP.hpp" -using namespace std; +#include"pkcs7_HPP.hpp" int main() {//g++ AES.cpp main.cpp -o program - + + ios_base::fmtflags original_flags = cout.flags(); + int BlockSize = 16; //shiftrow & invshiftrow & sub & Invsub 完成測試 + cout << "此程式支援Base64編碼與pkcs7填充\n"; while (1) { cout << "此AES加密與解密程式只支援128位的密鑰\n要使用加密功能請輸入0,要使用解密功能請輸入1\n"; int en_or_de; cin >> en_or_de; if (!en_or_de) {//en + int textLength; + cout << "輸入的明文以字節儲存(就是常見的1byte儲存一個)的請輸入0" << '\n'; cout << "輸入的明文以Base64編碼的請輸入1(會先轉為字節儲存再加密)" << '\n'; int ch_or_ba; cin >> ch_or_ba; cout << "請輸入明文\n"; - unsigned char plaintext[16]; + string tmp; cin.ignore(); getline(cin, tmp); - if (en_or_de) { + if (ch_or_ba) { tmp = Base64_decode(tmp); } - cout << "明文的16進位 "; + pkcs7_padding(tmp, BlockSize); + textLength = tmp.size(); + + unsigned char *plaintext = new unsigned char[textLength]; + for (int i = 0; i < textLength; i++) { plaintext[i] = tmp[i]; } + + cout << "填充後的16進位 "; for (const auto& item : tmp) { cout << hex << setw(2) << setfill('0') << (0xFF & static_cast(item)) << ' '; - }cout << '\n'; - - for (int i = 0; i < 16; i++) { plaintext[i] = tmp[i]; } + }cout << '\n'; cout.flags(original_flags); cout << "請輸入密鑰(字節儲存128bit)\n"; unsigned char key[16]; getline(cin, tmp); for (int i = 0; i < 16; i++) { key[i] = tmp[i]; } - unsigned char ciphertext[16]; - encrypt(plaintext, key, ciphertext); + cout << "textLength " << textLength << '\n'; + unsigned char *ciphertext = new unsigned char [textLength]; + encryptECB(textLength,plaintext, key, ciphertext); string tt; - tt.resize(16); - for (int i = 0; i < 16; i++) { tt[i] = ciphertext[i]; } + tt.resize(textLength); + for (int i = 0; i < textLength; i++) { tt[i] = ciphertext[i]; } cout << "密文(16進位) "; for (const auto& item : tt) { cout << hex << setw(2) << setfill('0') << (0xFF & static_cast(item)) << ' '; - }cout << '\n'; + }cout << '\n'; cout.flags(original_flags); tt = Base64_encode(tt); cout << tt << "\n\n"; + + delete[] plaintext; + delete[] ciphertext; } else if (en_or_de) {//de + int textLength; cout << "請輸入Base64編碼的密文\n"; - unsigned char ciphertext[16]; string tmp; cin.ignore(); getline(cin, tmp); tmp = Base64_decode(tmp); - for (int i = 0; i < 16; i++) { + textLength = tmp.size(); + unsigned char *ciphertext = new unsigned char[textLength]; + + for (int i = 0; i < textLength; i++) { ciphertext[i] = tmp[i]; } cout << "請輸入密鑰(字節儲存128bit)\n"; @@ -69,22 +85,29 @@ int main() {//g++ AES.cpp main.cpp -o program key[i] = tmp[i]; } - unsigned char plaintext[16]; - decrypt(ciphertext, key, plaintext); - + unsigned char *plaintext = new unsigned char[textLength]; + decryptECB(textLength,ciphertext, key, plaintext); + string tt; - tt.resize(16); - for (int i = 0; i < 16; i++) { + tt.resize(textLength); + for (int i = 0; i < textLength; i++) { tt[i] = plaintext[i]; } + + pkcs7_unpadding(tt); + textLength = tt.size(); + for (const auto& item : tt) { cout << hex << setw(2) << setfill('0') << (0xFF & static_cast(item)) << ' '; - }cout << '\n'; + }cout << '\n'; cout.flags(original_flags); - for (int i = 0; i < 16; i++) { + for (int i = 0; i < textLength; i++) { cout << plaintext[i]; }cout << "\n\n"; + + delete[] plaintext; + delete[] ciphertext; } } } \ No newline at end of file diff --git a/pkcs7_HPP.cpp b/pkcs7_HPP.cpp new file mode 100644 index 0000000..48991fc --- /dev/null +++ b/pkcs7_HPP.cpp @@ -0,0 +1,20 @@ +#include"pkcs7_HPP.hpp" +using namespace std; +void pkcs7_padding(string& str, int BlockSize) { + cout << BlockSize << ' ' << str.size() << '\n'; + int PaddingSize = BlockSize - (str.size() % BlockSize); + unsigned char PaddingChar = (unsigned char)PaddingSize; + for (int i = 0; i < PaddingSize; i++) { + str.push_back(PaddingChar); + } + + return; +} + +void pkcs7_unpadding(string& str) { + cout << "ttttt " << (unsigned char)str[str.size() - 1] << '\n'; + int PaddingSize = (int)((unsigned char)str[str.size() - 1]); + cout << "paddingsize " << PaddingSize << '\n'; + str.resize(str.size() - PaddingSize); + return; +} \ No newline at end of file diff --git a/pkcs7_HPP.hpp b/pkcs7_HPP.hpp new file mode 100644 index 0000000..a85e247 --- /dev/null +++ b/pkcs7_HPP.hpp @@ -0,0 +1,16 @@ +#ifndef pkcs7_HPP +#define pkcs7_HPP + +#include +#include +#include + + +using namespace std; + +void pkcs7_padding(string& str, int BlockSize); + +void pkcs7_unpadding(string& str); + + +#endif // !pkcs7_HPP