Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sstream>
- using namespace std;
- unsigned char hexVal(unsigned char a) {
- if (a >= 48 && a <= 57) {
- return (a - 48);
- }
- else {
- if (a >= 65 && a <= 70) {
- return (a - 55);
- }
- else {
- if (a >= 97 && a <= 102) {
- return (a - 87);
- }
- else {
- return 0;
- }
- }
- }
- }
- unsigned char byteVal(unsigned char a) {
- a = a & 0x0f;
- if (a >= 0x00 && a <= 0x09) {
- return (a + 48);
- }
- else {
- return (a + 87);
- }
- }
- int main(int argc, char** argv)
- {
- if (argc == 1) {
- cout << "**********************************************************************" << endl;
- cout << "* ******************* *" << endl;
- cout << "* * CRYPTO WKR 2018 * *" << endl;
- cout << "* ******************* *" << endl;
- cout << "**********************************************************************" << endl;
- }
- else if (argc == 2) {
- if (string(argv[1]) == "--help") {
- cout << "**********************************************************************" << endl;
- cout << "* ******************* *" << endl;
- cout << "* * CRYPTO WKR 2018 * *" << endl;
- cout << "* ******************* *" << endl;
- cout << "**********************************************************************" << endl;
- cout << "* Prosze uzupelnic pole pomocy w miare rozwoju aplikacji. *" << endl;
- cout << "* W chwili obecnej program zbudowany jest jako szablon, *" << endl;
- cout << "* ktory powinien byc uzupelniany o kolejne algorytmy *" << endl;
- cout << "* kryptograficzne. Powodzenia. *" << endl;
- cout << "**********************************************************************" << endl;
- cout << "* Dolecowy program powinien dzialac w nastepujacy sposob: *" << endl;
- cout << "* W wierszu polecen wywolujemy nazwe programu i parametry *" << endl;
- cout << "* programu, gdzie: *" << endl;
- cout << "* -bc szyfr blokowy w trybie c/d (szyfrowanie/deszyfrowanie); *" << endl;
- cout << "* -sc szyfr strumieniowy w trybie c/d (szyfrowanie/deszyfrowanie); *" << endl;
- cout << "* -hf funkcja skrotu bez trybow; *" << endl;
- cout << "* -ac szyfr asymetryczny w trybie c/d (szyfrowanie/deszyfrowanie); *" << endl;
- cout << "* -i sciezka pliku wejsciowego (tekstu jawnego, wiadomosci); *" << endl;
- cout << "* -o sciezka pliku wyjsciowego (tekstu zaszyfrowanego, skrotu); *" << endl;
- cout << "* -k sciezka klucza; *" << endl;
- cout << "* -h, --help to pomoc (wystepuje bez innych parametrow); *" << endl;
- cout << "**********************************************************************" << endl;
- cout << "* Przyklad: *" << endl;
- cout << "(* crypto_2018.exe -bc d -i C:\in.txt -o D:\out.txt -k C:\key.txt *)" << endl;
- cout << "**********************************************************************" << endl;
- cout << "* Program operuje na plikach tekstowych. *" << endl;
- cout << "**********************************************************************" << endl;
- }
- else {
- cout << argc << endl;
- cout << "Blednie wprowadzone parametr/parametry. (Blad: E001)" << endl;
- }
- }
- else {
- int flaga_szyfru = 0;
- int tryb_szyfrowania = 0;
- int flaga_poprawnosci_parametrow = 1;
- int wybor_algorytmu = 0;
- fstream input;
- fstream output;
- fstream key;
- for (int i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- if (argc == i + 1 || argv[i + 1][0] == '-') {
- if (string(argv[i]) == "-hf") {
- wybor_algorytmu = i;
- cout << argv[i] << ":";
- cout << "Ok, wyznaczenie skrotu do dzialania nie potrzebuje parametru ani pliku klucza;" << endl;
- }
- else if (string(argv[i]) == "-bc") {
- cout << argv[i] << ": ";
- cout << "Nie podano parametru pracy szyfru blokowego;" << endl;
- }
- else if (string(argv[i]) == "-sc") {
- cout << argv[i] << ": ";
- cout << "Nie podano parametru pracy szyfru strumieniowego;" << endl;
- }
- else if (string(argv[i]) == "-ac") {
- cout << argv[i] << ": ";
- cout << "Nie podano parametru pracy szyfru asymetrycznego;" << endl;
- }
- else if (string(argv[i]) == "-i") {
- cout << argv[i] << ": ";
- cout << "Nie podano sciezki pliku wejsciowego;" << endl;
- }
- else if (string(argv[i]) == "-o") {
- cout << argv[i] << ": ";
- cout << "Nie podano sciezki pliku wyjsciowego;" << endl;
- }
- else if (string(argv[i]) == "-k") {
- cout << argv[i] << ": ";
- cout << "Nie podano pliku klucza;" << endl;
- }
- else {
- cout << argv[i] << ": ";
- cout << "Niepoprawny parametr pracy programu;" << endl;
- }
- }
- else {
- if (string(argv[i]) == "-i") {
- input.open(argv[i + 1], ios::in);
- if (input.good()) {
- flaga_poprawnosci_parametrow *= 2;
- cout << "Dane wejsciowe zostana pobrany z pliku: " << argv[++i] << endl;
- }
- else {
- cout << argv[i] << ": ";
- cout << "Niepoprawna sciezka pliku wejsciowego!" << endl;
- }
- }
- else if (string(argv[i]) == "-o") {
- output.open(argv[i + 1], ios::out);
- if (output.good()) {
- flaga_poprawnosci_parametrow *= 3;
- cout << "Dane wyjsciowe zostana zapisane do pliku: " << argv[++i] << endl;
- }
- else {
- cout << argv[i] << ": ";
- cout << "Niepoprawna sciezka pliku wejsciowego!" << endl;
- }
- }
- else if (string(argv[i]) == "-k") {
- key.open(argv[i + 1], ios::in);
- if (key.good()) {
- flaga_poprawnosci_parametrow *= 5;
- cout << "Klucz zostanie pobrany z pliku: " << argv[++i] << endl;
- }
- else {
- cout << argv[i] << ": ";
- cout << "Niepoprawna sciezka klucza!" << endl;
- }
- }
- else if (string(argv[i]) == "-bc") {
- flaga_szyfru = 1;
- if (string(argv[i + 1]) == "c") {
- tryb_szyfrowania = 1;
- cout << "Szyfr blokowy bedzie pracowal w trybie szyfrowania: " << argv[++i] << endl;
- }
- else if (string(argv[i + 1]) == "d")
- {
- tryb_szyfrowania = 2;
- cout << "Szyfr blokowy bedzie pracowal w trybie deszyfrowania: " << argv[++i] << endl;
- }
- else {
- cout << argv[i] << ": ";
- cout << "Niepoprawny tryb pracy szyfru;" << endl;
- }
- }
- else if (string(argv[i]) == "-sc") {
- if (flaga_szyfru) {
- cout << "Podano 2 rodzaje szyfrow/trybow pracy. Bledna konfiguracja programu" << endl;
- return 1;
- }
- flaga_szyfru = 2;
- if (string(argv[i + 1]) == "c") {
- tryb_szyfrowania = 1;
- cout << "Szyfr strumieniowy bedzie pracowal w trybie szyfrowania: " << argv[++i] << endl;
- }
- else if (string(argv[i + 1]) == "d")
- {
- tryb_szyfrowania = 2;
- cout << "Szyfr strumieniowy bedzie pracowal w trybie deszyfrowania: " << argv[++i] << endl;
- }
- else {
- cout << argv[i] << ": ";
- cout << "Niepoprawny tryb pracy szyfru;" << endl;
- }
- }
- else if (string(argv[i]) == "-ac") {
- if (flaga_szyfru) {
- cout << "Podano 2 rodzaje szyfrow/trybow pracy. Bledna konfiguracja programu" << endl;
- return 1;
- }
- flaga_szyfru = 3;
- if (string(argv[i + 1]) == "c") {
- tryb_szyfrowania = 1;
- cout << "Szyfr asymetryczny bedzie pracowal w trybie szyfrowania: " << argv[++i] << endl;
- }
- else if (string(argv[i + 1]) == "d")
- {
- tryb_szyfrowania = 2;
- cout << "Szyfr asymetryczny bedzie pracowal w trybie deszyfrowania: " << argv[++i] << endl;
- }
- else {
- cout << argv[i] << ": ";
- cout << "Niepoprawny tryb pracy szyfru;" << endl;
- }
- }
- else {
- cout << argv[i] << ": ";
- cout << "Niepoprawny parametr pracy programu;" << endl;
- }
- }
- }
- }
- /*
- if (flaga_szyfru == 0 || tryb_szyfrowania == 0) {
- cout << "Brak poprawnego wybrania trybu pracy(szyfruj/deszyfruj)" << endl;
- return 0;
- }
- if (!input.is_open() || !output.is_open() || !key.is_open()) {
- cout << "Brak poprawnej konfiguracji plikow" << endl;
- return 0;
- }
- */
- if (flaga_szyfru == 1)
- {
- string i_str;
- string k_str;
- stringstream ss_out;
- input >> i_str;
- key >> k_str;
- input.close();
- key.close();
- if (tryb_szyfrowania == 1) {
- int len = i_str.length() / 16;
- for (int i = 0; i < len; i++) {
- string v_s = i_str.substr(i * 16, 16);
- string k_s = k_str.substr(i * 32, 32);
- uint32_t v[2];
- uint32_t k[4];
- for (int j = 0; j < 2; j++) {
- v[j] = strtoul(v_s.substr(j * 8, 8).c_str(), NULL, 16);
- }
- for (int j = 0; j < 4; j++) {
- k[j] = strtoul(k_s.substr(j * 8, 8).c_str(), NULL, 16);
- }
- uint32_t sum = 0;
- uint32_t delta = 0x9E3779B9;
- for (int j = 0; j < 32; j++) {
- sum += delta;
- v[0] += ((v[1] << 4) + k[0]) ^ (v[1] + sum) ^ ((v[1] >> 5) + k[1]);
- v[1] += ((v[0] << 4) + k[2]) ^ (v[0] + sum) ^ ((v[0] >> 5) + k[3]);
- }
- stringstream ss_temp;
- ss_temp << hex << v[0] << v[1];
- string s_temp = ss_temp.str();
- while (s_temp.length() < 16) {
- s_temp = "0" + s_temp;
- }
- ss_out << s_temp;
- cout << "Zapisano!"<<endl;
- }
- }
- if (tryb_szyfrowania == 2) {
- int len = i_str.size() / 16;
- for (int i = 0; i < len; i++) {
- string v_s = i_str.substr(i * 16, 16);
- string k_s = k_str.substr(i * 32, 32);
- uint32_t delta = 0x9E3779B9;
- uint32_t sum = 32 * delta;
- uint32_t v[2];
- uint32_t k[4];
- for (int j = 0; j < 2; j++) {
- v[j] = strtoul(v_s.substr(j * 8, 8).c_str(), NULL, 16);
- }
- for (int j = 0; j < 4; j++) {
- k[j] = strtoul(k_s.substr(j * 8, 8).c_str(), NULL, 16);
- }
- for (int j = 0; j < 32; j++) {
- v[1] -= ((v[0] << 4) + k[2]) ^ (v[0] + sum) ^ ((v[0] >> 5) + k[3]);
- v[0] -= ((v[1] << 4) + k[0]) ^ (v[1] + sum) ^ ((v[1] >> 5) + k[1]);
- sum -= delta;
- }
- stringstream ss_temp;
- ss_temp << hex << v[0] << v[1];
- string s_temp = ss_temp.str();
- while (s_temp.length() < 16) {
- s_temp = "0" + s_temp;
- }
- ss_out << s_temp;
- cout << "Zapisano!"<<endl;
- }
- }
- output << ss_out.str();
- output << endl;
- output.close();
- }
- if (flaga_szyfru == 2) {
- string i_str;
- string k_str;
- stringstream ss_out;
- input >> i_str;
- key >> k_str;
- input.close();
- key.close();
- if (tryb_szyfrowania == 1 || tryb_szyfrowania == 2) {
- int dlugosc = i_str.length() / 8;
- uint32_t lfsr = strtoul(k_str.c_str(), NULL, 16);
- for (int i = 0; i < dlugosc; i++) {
- uint32_t i_frag = strtoul(i_str.substr(i * 8, 8).c_str(), NULL, 16);
- stringstream ss_temp;
- uint32_t blok = 0;
- for (int j = 0; j < 32; j++) {
- uint32_t bit = ((lfsr >> 31) ^ (lfsr >> 1) ^ (lfsr >> 0)) & 1;
- blok = (blok << 1) | (lfsr & 1);
- lfsr = (lfsr >> 1) | (bit << 31);
- }
- i_frag = i_frag ^ blok;
- ss_temp << hex << i_frag;
- string s_temp = ss_temp.str();
- while (s_temp.length() < 8) {
- s_temp = "0" + s_temp;
- }
- ss_out << s_temp;
- cout << "Zapisano!"<<endl;
- }
- }
- output << ss_out.str();
- output << endl;
- output.close();
- }
- if (flaga_szyfru == 3) {
- if (tryb_szyfrowania == 1 || tryb_szyfrowania == 2) {
- const unsigned char nSize = 2;
- const unsigned char modSize = 2;
- const unsigned char blockSize = 1;
- char* tempN = new char[nSize];
- char* tempMod = new char[modSize];
- char v = 0;
- char c = 0;
- unsigned int M = 0;
- unsigned int E = 0;
- key.read(tempN, nSize);
- unsigned int N = ((hexVal((unsigned char)tempN[0]) << 4) + hexVal((unsigned char)tempN[1]));
- key.read(&v, 1);
- key.read(tempMod, modSize);
- unsigned int MOD = ((hexVal((unsigned char)tempMod[0]) << 4) + hexVal((unsigned char)tempMod[1]));
- input.read(&v, blockSize);
- cout << "..." << endl;
- while (input) {
- M = v;
- E = v;
- for (unsigned int i = 1; i < N; i++) {
- E *= M;
- E = E % MOD;
- }
- c = (char)E;
- output.write(&c, blockSize);
- input.read(&v, blockSize);
- }
- cout << "Zapisano!" << endl;
- }
- }
- if (std::string(argv[wybor_algorytmu]) == "-hf") {
- if (output.good() == true && input.good() == true && flaga_poprawnosci_parametrow == 6) {
- const unsigned char blockSize = 1;
- const unsigned char hashSize = 8;//2*sizeof(unsigned long);
- char v;
- char* finalHash = new char[hashSize];
- unsigned long hash = 5381;
- input.read(&v, blockSize);
- cout << "..." << endl;
- while (input) {
- hash = ((hash << 5) + hash) + (int)v;
- input.read(&v, blockSize);
- }
- cout<< "Hash: " << hash << endl;
- for (int i = 0; i < hashSize; i++) {
- finalHash[hashSize - 1 - i] = byteVal(hash >> (i * 4));
- }
- output.write(finalHash, hashSize);
- cout << "Zapisano!" << endl;
- delete[] finalHash;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement