A 16-bit register was used, the Fibonacci configuration.
For the program to work, its name is first entered, followed by the names:
- original file;
- output, file to write the key and the flag "-cod" - encode the source file, "decod" - decode.
Encoding is normal, and when decoding the original text does not work.
The program is rewritten from the language "Java", where the work is correct.
I have been learning the C ++ language for several months, I just donβt know. Is any error possible due to the char
, std::string
types and their conversions?
The java program is here . Runs just like in C ++. Also, do not mind listening to other tips about the code, as I only learn the language.
#include <iostream> #include <fstream> #include <cstdlib> #include <cstring> #include <ctime> // ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ time() void coding(char *); //ΠΊΠΎΠ΄ΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅, ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ ΠΏΡΡΡ ΠΊ ΡΠ°ΠΉΠ»Ρ int binToDec ( const char* ); //ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΡΡΠΎΠΊΠΈ Π² ΡΠΈΡΠ»ΠΎ void overlayGamma(int); //Π½Π°Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ Π³Π°ΠΌΠΌΡ, ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ 16-Π±ΠΈΡΠ½ΡΠΉ ΠΊΠ»ΡΡ void decoding(char * ch); //Π΄Π΅ΠΊΠΎΠ΄ΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅, ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ ΠΊΠ»ΡΡ ΠΊ ΡΠ°ΠΉΠ»Ρ std::ofstream keyIn; //Π·Π°ΠΏΠΈΡΡ ΠΊΠ»ΡΡΠ° std::ifstream keyOut; //ΡΡΠ΅Π½ΠΈΠ΅ ΠΊΠ»ΡΡΠ° char * pathIn; //ΠΏΡΡΡ ΠΊ ΡΠ°ΠΉΠ»Ρ ΠΊΠ»ΡΡΠ° char * pathOut; //ΠΏΡΡΡ ΠΊ ΡΠ°ΠΉΠ»Ρ ΠΊΠ»ΡΡΠ° int main(int argc, char * argv[]){ if(argc!=5){ std::cout << "\nΠ¨ΠΈΡΡΠΎΠ²Π°Π½ΠΈΠ΅: lfsr In.txt Out.txt. Key.txt -cod, Π³Π΄Π΅\n" << "In - ΡΠ°ΠΉΠ», ΠΊΠΎΡΠΎΡΡΠΉ ΡΡΠ΅Π±ΡΠ΅ΡΡΡ Π·Π°ΡΠΈΡΡΠΎΠ²Π°ΡΡ\n" << "Out - ΡΠ°ΠΉΠ», ΠΊΡΠ΄Π° Π±ΡΠ΄Π΅Ρ Π·Π°ΠΏΠΈΡΠ°Π½ ΡΠΈΡΡΠΎΠ²Π°Π½Π½ΡΠΉ ΡΠ΅ΠΊΡΡ\n" << "Key - ΡΠ°ΠΉΠ», ΠΊΡΠ΄Π° Π±ΡΠ΄Π΅Ρ Π·Π°ΠΏΠΈΡΠ°Π½ 16-Π±ΠΈΡΠ½ΡΠΉ ΠΊΠ»ΡΡ\n" << "-cod - ΠΊΠΎΠΌΠ°Π½Π΄Π° ΡΠΈΡΡΠ°ΡΠΈΠΈ\n" << "\nΠΠ΅ΡΠΈΡΡΠ°ΡΠΈΡ: LFSR In.txt Out.txt. Key.txt -decod, Π³Π΄Π΅\n" << "In - ΡΠ°ΠΉΠ», ΠΊΠΎΡΠΎΡΡΠΉ ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΡΠ°ΡΡΠΈΡΡΠΎΠ²Π°ΡΡ\n" << "Out - ΡΠ°ΠΉΠ», ΠΊΡΠ΄Π° Π±ΡΠ΄Π΅Ρ Π·Π°ΠΏΠΈΡΠ°Π½ ΡΠ°ΡΡΠΈΡΡΠΎΠ²Π°Π½Π½ΡΠΉ ΡΠ΅ΠΊΡΡ\n" << "Key - ΡΠ°ΠΉΠ» Ρ 16-Π±ΠΈΡΠ½ΡΠΌ ΠΊΠ»ΡΡΠΎΠΌ\n" << "-decod - ΠΊΠΎΠΌΠ°Π½Π΄Π° Π΄Π΅ΡΠΈΡΡΠ°ΡΠΈΠΈ\n"; return -1; } //ΡΠ°ΠΉΠ» Π΄Π»Ρ ΡΠΈΡΡΠΎΠ²Π°Π½ΠΈΡ std::ifstream in(argv[1], std::ios::in); if(!in){ std::cout << " Π€Π°ΠΉΠ» " ; std::cout << argv[1] << " Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½"; return -2; } //ΠΏΡΡΠΈ ΠΊ ΡΠ°ΠΉΠ»Π°ΠΌ Ρ ΠΊΠ»ΡΡΠΎΠΌ pathIn = argv[1]; pathOut = argv[2]; if(! strcmp(argv[4], "-cod")) coding(argv[3]); //ΡΠΈΡΡΠ°ΡΠΈΡ, ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΠΌ ΠΏΡΡΡ ΠΊΠ»ΡΡΠ° else if(! strcmp(argv[4], "-decod")) decoding(argv[3]); //Π΄Π΅ΡΠΈΡΡΠ°ΡΠΈΡ else { std::cout << "\nΠΠ°Π΄Π°Π½ Π½Π΅Π²Π΅ΡΠ½ΡΠΉ ΡΠ»Π°Π³ " << argv[3] << " , Π΄ΠΎΠΏΡΡΡΠΈΠΌΡ" << " -cod, -decod"; return -3; } } int * getKey(){ //ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΠ΅ 16-Π±ΠΈΡΠ½ΠΎΠ³ΠΎ ΠΊΠ»ΡΡΠ° Π² Π΄Π²ΠΎΠΈΡΠ½ΠΎΠΌ Π²ΠΈΠ΄Π΅ int * key = new int[16]; std::srand(time(NULL)); for (int i = 0; i < 16; i++) key[i] = rand()%(2); return key; } long getGamma(int * key){ //ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π³Π°ΠΌΠΌΡ, ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡ ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½ΡΠΉ ΠΊΠ»ΡΡ int n = sizeof(key); std::string g = ""; int S = 0; for(int t = 0; t <32; t++){ //Π³Π°ΠΌΠΌΠ° 32 Π±ΠΈΡΠ° //Π³Π°ΠΌΠΌΠ° ΠΏΠΎΠ»ΡΡΠ°Π΅ΡΡΡ Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ΠΌ ΡΠ΅Π³ΠΈΡΡΡΠ° ΡΠ΄Π²ΠΈΠ³Π° ΠΈ ΠΊΠΎΠ½ΡΠΈΠ³ΡΡΠ°ΡΠΈΠΈ Π€ΠΈΠ±ΠΎΠ½Π°ΡΡΠΈ S = key[n-1]; S = ((( (S >> 15) ^ (S >> 4) ^ (S >> 2) ^ (S >> 1) ^ S ) & 0x00000001 ) << 15 ) | (S >> 1); for (int i = n-1; i > 0; i--) //ΡΠ΄Π²ΠΈΠ³ ΡΡΠ΅Π΅ΠΊ ΡΠ΅Π³ΠΈΡΡΡΠ° key[i] = key[i-1]; key[0] = S; //Π·Π°ΠΏΠΈΡΡ ΡΡΠΎΡΠΌΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ Π±ΠΈΡΠ° if(S>0) g += "1"; //ΡΠΎΡΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ Π³Π°ΠΌΠΌΡ Π² Π² Π΄Π²ΠΎΠΈΡΠ½ΠΎΠΌ Π²ΠΈΠ΄Π΅ else g += "0"; } return binToDec (g.c_str()); //Π²Π΅ΡΠ½ΡΡΡ Π³Π°ΠΌΠΌΡ Π² 10 Π‘Π‘ // (16, 5, 3, 2, 0) - ΡΡΠ΅ΠΏΠ΅Π½ΠΈ } void overlayGamma(int gamma){ //Π½Π°Π»ΠΎΠΆΠΈΡΡ Π³Π°ΠΌΠΌΡ Π½Π° ΠΎΡΠΊΡΡΡΡΠΉ ΡΠ΅ΠΊΡΡ std::ifstream in(pathIn, std::ios::in); //ΠΎΡΠΊΡΡΡΡΠΉ ΡΠ΅ΠΊΡΡ std::ofstream out(pathOut, std::ios::in); //Π΄Π»Ρ ΡΠΈΡΡΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ ΡΠ΅ΠΊΡΡΠ° char ch; while(in.get(ch)) out << (char)(ch ^ gamma); //Π½Π°Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ } int binToDec ( const char* c) { int rez=0; int a=1; int len=strlen(c); for(int j = 0, k = len; j < len; j++, k-- ) if ( c[ j ] == '1' ) rez |= a << k - 1; std::cout << rez <<std::endl; return rez; } void coding(char * ch){ //ΡΠΈΡΡΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅ΠΊΡΡΠ° int * key = getKey(); //ΠΊΠ»ΡΡ 16 Π±ΠΈΡ //Π·Π°ΠΏΠΈΡΡ ΠΊΠ»ΡΡΠ° Π² ΡΠ°ΠΉΠ» std::ofstream keyIn(ch, std::ios::in); for(int i = 0; i < 16; i++) keyIn << key[i]; keyIn.close(); overlayGamma(getGamma(key)); //Π½Π°Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΠΎΠΉ Π³Π°ΠΌΠΌΡ Π½Π° ΠΎΡΠΊΡΡΡΡΠΉ ΡΠ΅ΠΊΡΡ } void decoding(char * ch){ //Π΄Π΅ΠΊΠΎΠ΄ΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅ΠΊΡΡΠ° int * key = new int[16]; std::ifstream keyOut(ch, std::ios::in); //ΠΏΠΎΡΠΎΠΊ Π΄Π»Ρ ΡΡΠ΅Π½ΠΈΡ ΠΊΠ»ΡΡΠ° std::string temp = ""; const char * k; if(keyOut) //ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΠ΅ ΠΊΠ»ΡΡΠ° ΠΈΠ· ΡΠ°ΠΉΠ»Π° std::getline(keyOut, temp); else { std::cout << "Π€Π°ΠΉΠ» Ρ ΠΊΠ»ΡΡΠΎΠΌ" << ch << " Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½."; exit(-1); } int lim = strlen(temp.c_str()); //Π΄Π»ΠΈΠ½Π° ΠΊΠ»ΡΡΠ° k = temp.c_str(); for(int i = 0; i < lim; i++) key[i] = (int) k[i]; //Π·Π°ΠΏΠΈΡΡΠ²Π°Π΅ΠΌ ΠΊΠ»ΡΡ Π² ΠΌΠ°ΡΡΠΈΠ² overlayGamma(getGamma(key)); //Π½Π°Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ Π³Π°ΠΌΠΌΡ Π΄Π»Ρ ΡΠ°ΡΡΠΈΡΡΠΎΠ²ΡΠ²Π°Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ° keyOut.close(); }