Add files via upload
add pkcs7 and ECB
This commit is contained in:
33
AES_HPP.cpp
33
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,
|
||||
@@ -302,3 +305,33 @@ void decrypt(unsigned char* ciphertext, unsigned char* key, unsigned char* outpu
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
#include<string>
|
||||
#include<iomanip>
|
||||
|
||||
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
|
||||
@@ -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',
|
||||
|
||||
63
main.cpp
63
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<unsigned int>(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<unsigned int>(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<unsigned int>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
20
pkcs7_HPP.cpp
Normal file
20
pkcs7_HPP.cpp
Normal file
@@ -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;
|
||||
}
|
||||
16
pkcs7_HPP.hpp
Normal file
16
pkcs7_HPP.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef pkcs7_HPP
|
||||
#define pkcs7_HPP
|
||||
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<iomanip>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
void pkcs7_padding(string& str, int BlockSize);
|
||||
|
||||
void pkcs7_unpadding(string& str);
|
||||
|
||||
|
||||
#endif // !pkcs7_HPP
|
||||
Reference in New Issue
Block a user