Hejsa gruppe
Jeg er lige startet på DTU, hvor vi bl.a. skal lære C++.
Vi er nu nået til et projekt hvor vi skal kryptere et password, så jeg
kiggede lidt på nettet (mest for sjov) for at finde ud af en måde at
kryptere på. Jeg fandt nedenstående kode, som jeg stort set forstår. MEN:
Jeg kan ikke forstå hved cin.getline linierne skal bruges til. Jeg har
prøvet at lave om på dem, fjerne dem og erstatte dem med cin.get, men så gik
programmet bare amork. Er der nogle der har mulighed for at forklare en
stort set nybegynner, hvad disse linier gør godt for? Specielt den første
(som jeg har markeret med ** i siden), hvor det er en normal cin>> linie
ovenover. Det ville jeg da mene var nok til at få en karakter?
mvh
Søren
Koden:
// **************************************
// INCLUDE files for :XEncryptor v1.1
// **************************************
#include <iostream.h>
#include <stdio.h>
// **************************************
// Name: XEncryptor v1.1
// Description:Ecrypts any file with a key and very secure encryption.
// By: Marco Lugo
//
//
// Inputs:File to encrypt, file to save the encrypton, key to encrypt.
//
// Returns:None
//
// Assumes:None
//
// Side Effects:That on decrypting the output file is overwriten.
// This code is copyrighted and has limited warranties.
// Please see
http://www.1CPlusPlusStreet.com/xq/ASP/txtCodeId.1742/lngWId.3/qx/vb/scripts
/ShowCode.htm
// for details.
// **************************************
int main(void) // Main function, It also the only one here
{
// Declaring all variables, arrays and stuff
register int byte, workwme, key;
register long size_by_now = 0;
char FName[2][256], clean[2];
int choice;
FILE *IFile, *OFile;
// Start-Up and Menu showing
cout << " XEncryptor v1.1" << endl;
cout << " Author: Marco Lugo" << endl;
cout << endl;
cout << endl;
cout << " Select an option:" << endl;
cout << " |-------------------|" << endl;
cout << " | 1) Encode.|" << endl;
cout << " | 2) Decode.|" << endl;
cout << " | 3) Exit. |" << endl;
cout << " |-------------------|" << endl;
cout << " Enter your choice: ";
cin >> choice;
**cin.getline(clean, 2);
cout << endl;
cout << endl;
// Switch statement for choice
switch(choice)
{
// Encode
case 1:
cout << " Enter the filename of file to encrypt: ";
cin.getline(FName[0], 256, '\n'); // Get filename
cout << " Enter the filename of the file to saved the encrypted file
to: " << endl;
cout << " ";
cin.getline(FName[1], 256, '\n');
cout << " Enter the key to encrypt the file(can't have spaces): ";
cin >> key;//Get the key
cin.getline(clean, 2);
IFile = fopen(FName[0], "rb");//Open input file
if(!(IFile))//Checking file existance
{
cout << " File " << FName[0] << " not found..." << endl;
break;
}
OFile = fopen(FName[1], "wb");// Open output file
while(byte != -1) // While byte not equal to -1
{
byte = fgetc(IFile); // Get byte from file
if(byte == -1) break;
workwme = byte;
size_by_now++;
workwme = (~byte) ^ key;
fputc(workwme, OFile);
}
fclose(IFile);
fclose(OFile);
break;
// Decode
case 2:
cout << " Enter the filename of file to decrypt: ";
cin.getline(FName[0], 256, '\n'); // Get filename
cout << " Enter the filename of file you want to save the decrypted
file to: " << endl;
cout << " ";
cin.getline(FName[1], 256, '\n');
cout << " Enter the key to decrypt the file(can't have spaces): ";
cin >> key;// Get the key
cin.getline(clean, 2);
IFile = fopen(FName[0], "rb");//Open input file
if(!(IFile))// Checking file existance
{
cout << " File " << FName[0] << " not found..." << endl;
break;
}
OFile = fopen(FName[1], "wb");// Open output file
while(byte != -1) // While byte not equal to -1
{
byte = fgetc(IFile); // Get byte from file
if(byte == -1) break;
workwme = byte;
size_by_now++;
workwme = (~byte) ^ key;
fputc(workwme, OFile);
}
fclose(IFile);
fclose(OFile);
break;
// Exit
case 3: break;
}
// Saying bye-bye to the user
cout << endl;
cout << endl;
cout << endl;
cout << " Thanks for using XEncrytor, hope It was useful for you." <<
endl;
cout << " XEncryptor was made in C/C++ with the Dev-C++ 4.0 compiler."
<< endl;
cout << " Marco Lugo" << endl;
cout << endl;
cout << " Press ENTER to exit...";
cin.get();
return(0);
}