A 16-bit register was used, the Fibonacci configuration.

For the program to work, its name is first entered, followed by the names:

  1. original file;
  2. 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(); } 
  • Possibly with strings error. Try the unsigned char buffer. - Vladimir Martyanov
  • one
    @Roman, thank you very much, the problem was really in it, everything turned out! - Rob

1 answer 1

Judging by the java-code, you have the key in the file written in the form of a string of numbers. But in c ++ code you incorrectly read the key from the file, key[i] = (int) k[i] does not turn the character '1' into the number 1 .