Ganav Posted June 25, 2014 Report Posted June 25, 2014 In urma cu cateva zile am creat un challenge in care se dorea aflarea textului in clar al unui sir de caractere codate:https://rstforums.com/forum/85978-encode-decode.rstPana acum nu am primit niciun raspuns in privinta unei rezolvari sau chiar si a vreunei idei de rezolvare. Drept urmare m-am gandit sa fac publica sursa encoder-ului. A fost scris in Visual C++ Express Edition 2010 care poate fi gasit aici:Download OverviewDeschideti Visual Studio si creati un proiect nou de tip consola. La sfarsit inainte de a da click pe Finish asigurati-va ca proiectul nu va folosi antete precompilate(Precompiled headers). Acum creati urmatoarele fisiere antet(Acestea trebuiesc situate in folder-ul "Header Files" vizibil in Project Explorer):includes.h#ifndef INCLUDES_H#define INCLUDES_H#include <iostream>#include <string>#include <algorithm>#include <utility>#include <vector>#include <fstream>#endifencoder.h#ifndef ENCODER_H#define ENCODER_H#include "includes.h"class Char4EncoderDecoder {private: char getEncodedChar(int c); int getDecodedChar(char c); void reverseStr(std::vector<unsigned char>& input) { std::reverse(input.begin(), input.end()); } void bitReverse(std::vector<unsigned char>& input); void oddSwap(std::vector<unsigned char>& input); void evenSwap(std::vector<unsigned char>& input); void formatInput(std::vector<unsigned char>& input); void Char4EncoderDecoder::XORConsecutiveBytePairsInv(std::vector<unsigned char>& input); void write2dst(std::vector<unsigned char>& dst, std::string& fin_dst);public: void encode(std::string& src, std::string& dst); void decode(std::string& src, std::string& dst);};#endifAcum ne trebuiesc fisierele sursa(unde sunt implementate metodele claselor din fisierele antet). Adaugam in folderul "Source Files" din Project Explorer urmatoarele fisiere:encoder.cpp#include "includes.h"#include "encoder.h"void Char4EncoderDecoder::bitReverse(std::vector<unsigned char>& input) { for(size_t i = 0; i < input.size(); i++) input[i] = (input[i] * 0x0202020202ULL & 0x010884422010ULL) % 1023;}void Char4EncoderDecoder::oddSwap(std::vector<unsigned char>& input) { for(size_t i = 0; i < input.size() / 2; i++) if(input[i] % 2) { input[i] ^= input[input.size()/2+i]; input[input.size()/2+i] ^= input[i]; input[i] ^= input[input.size()/2+i]; }}void Char4EncoderDecoder::evenSwap(std::vector<unsigned char>& input) { for(size_t i = 0; i < input.size() / 2; i++) if(input[i] % 2 == 0) { input[input.size()/2+i] ^= input[i]; input[i] ^= input[input.size()/2+i]; input[input.size()/2+i] ^= input[i]; }}void Char4EncoderDecoder::encode(std::string& src, std::string& dst) { std::vector<unsigned char> byte_dst(src.begin(), src.end()); dst.clear(); // Reverse the source string this->reverseStr(byte_dst); // Bit reverse the source string this->bitReverse(byte_dst); // Swap even this->evenSwap(byte_dst); // Odd swap this->oddSwap(byte_dst); write2dst(byte_dst, dst);}void Char4EncoderDecoder::decode(std::string& src, std::string& dst) { std::vector<unsigned char> byte_dst(src.begin(), src.end()); dst.clear(); // Format input into bytes formatInput(byte_dst); // Odd swap this->oddSwap(byte_dst); // Swap even this->evenSwap(byte_dst); // Bit reverse the source string this->bitReverse(byte_dst); // Reverse the source string this->reverseStr(byte_dst); dst.resize(byte_dst.size()); for(size_t i = 0; i < byte_dst.size(); i++) dst[i] = byte_dst[i];}void Char4EncoderDecoder::formatInput(std::vector<unsigned char>& input) { // Format input if(input.size() % 4 != 0) { size_t aux = input.size() % 4; for(size_t i = 0; i < aux; i++) input.push_back('['); } // Extract bits and create results vector std::vector<unsigned char> dst; dst.resize(input.size() / 4); for(size_t i = 0; i < input.size(); i += 4) { dst[i/4] |= getDecodedChar(input[i]); dst[i/4] |= getDecodedChar(input[i+1]) << 2; dst[i/4] |= getDecodedChar(input[i+2]) << 4; dst[i/4] |= getDecodedChar(input[i+3]) << 6; } input = dst;}int Char4EncoderDecoder::getDecodedChar(char c) { if(c == '[') return 0; else if(c == '<') return 1; else if(c == '>') return 2; else if(c == ']') return 3; else return -1;}char Char4EncoderDecoder::getEncodedChar(int c) { if(c == 0) return '['; else if(c == 1) return '<'; else if(c == 2) return '>'; else if(c == 3) return ']'; else return -1;}void Char4EncoderDecoder::write2dst(std::vector<unsigned char>& dst, std::string& fin_dst) { std::vector<unsigned char> tmp_dst; for(size_t i = 0; i < dst.size(); i++) { tmp_dst.push_back(getEncodedChar((int)dst[i] & 0x03)); tmp_dst.push_back(getEncodedChar((int)(dst[i] & 0x0c) >> 2)); tmp_dst.push_back(getEncodedChar((int)(dst[i] & 0x30) >> 4)); tmp_dst.push_back(getEncodedChar((int)(dst[i] & 0xC0) >> 6)); } fin_dst.clear(); fin_dst.resize(tmp_dst.size()); for(size_t i = 0; i < tmp_dst.size(); i++) fin_dst[i] = tmp_dst[i]; // std::cin.get();}main.cpp#include "includes.h"#include "encoder.h"void challengeRST(void) { Char4EncoderDecoder *enc = 0; std::string src, dst; std::ofstream f; f.open ("RST_out.txt"); enc = new Char4EncoderDecoder(); src = "RST Forums"; enc->encode(src, dst); std::cout << "Encoded value: " << dst << "\n"; enc->decode(dst, src); std::cout << "Decoded value: " << src << "\n"; src = "abc"; enc->encode(src, dst); std::cout << "Encoded value: " << dst << "\n"; f << "Encoded value: " << dst << "\n"; enc->decode(dst, src); std::cout << "Decoded value: " << src << "\n"; f << "Encoded value: " << src << "\n"; src = "abc123"; enc->encode(src, dst); std::cout << "Encoded value: " << dst << "\n"; f << "Encoded value: " << dst << "\n"; enc->decode(dst, src); std::cout << "Decoded value: " << src << "\n"; f << "Encoded value: " << src << "\n"; src = "The quick brown fox jumps over the lazy dog"; enc->encode(src, dst); std::cout << "Encoded value: " << dst << "\n"; f << "Encoded value: " << dst << "\n"; enc->decode(dst, src); std::cout << "Decoded value: " << src << "\n"; f << "Encoded value: " << src << "\n"; src = "Romanian Security Team 2006 - 2014"; enc->encode(src, dst); std::cout << "Encoded value: " << dst << "\n"; f << "Encoded value: " << dst << "\n"; enc->decode(dst, src); std::cout << "Decoded value: " << src << "\n"; f << "Encoded value: " << src << "\n"; std::cout << "Done\n"; std::cin.get(); delete enc; enc = 0; f.close();}void usage(char *argv) { std::cout << "Usage: " << argv << "<-e/-d> <to be encoded string> \n"; std::exit(0);}int main(int argc, char **argv) {#ifndef NDEBUG challengeRST();#else if(argc != 3) usage(argv[0]); Char4EncoderDecoder *enc = 0; enc = new Char4EncoderDecoder(); std::string ed("argv[1]"); std::string src("argv[2]"); std::string dst; if(ed == "-e") { enc->encode(src, dst); std::cout << "Encoded string: " << dst << "\n"; } else if(ed == "-d") { enc->decode(src, dst); std::cout << "Decoded string: " << dst << "\n"; } else { usage(argv[0]); } // Deallocate memory delete enc; enc = 0;#endif return 0;}Algoritmul poate fi dezvoltat in continuare adaugand mai multe etape de procesare. Quote