8 Replies - 3214 Views - Last Post: 22 August 2012 - 10:11 AM Rate Topic: -----

#1 mbaker421  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-August 12

Writing Code for using strings

Posted 22 August 2012 - 08:04 AM

The focus of this lab is on using strings. You will have an opportunity to work with both C style strings and the string data type. This lab also gives you an opportunity to use what you have learned previously, including using functions, array processing, repetition, and selection. You will also have an opportunity to work with file input and output.
You are to design and implement a program which does encryption and decryption of data from files. Encryption is the process of taking plain lines of text and performing some algorithmic transformation on the data to create an encrypted line of text which looks nothing like the original. Decryption is the process of taking an encrypted line of text and performing some algorithmic transformation on the data to recover the original line of plain text.
Encryption and Decryption Approach
Our approach to encryption and decryption involves two strings. The first is an encryption / decryption string which we will allow to be up to 128 lower case alphabetical characters in length. The second string is a line of text from a file that is to be encrypted or decrypted.
Our basic strategy for encrypting data is based on mapping alphabetical characters to specific values, then doing some simple mathematical operations to create a new value. First of all, every character in either the encryption string or the input string is mapped to a number between 0 and 25 based on its position in the alphabet.
A = a = 0
B = b = 1
Z = z = 25
The mapped value of a character is easily obtained by doing the following:
For lower case characters, subtract 'a' from the character.
For upper case characters, subtract 'A' from the character.
To calculate the modified value of the first character of input we add its mapped value to the mapped value from the first character of the encryption string. This modified value is then adjusted using % 26 to make sure that the final modified value is within the 0 - 25 range. To create the final encrypted character value for the first character, simply do the following:
For lower case characters, add 'a' to the modified value.
For upper case characters, add 'A' to the modified value.
This is done for each alphabetic character in the input string. Non-alphabetic characters simply maintain their present value. If the input string is longer than the encryption string, simply reuse mapped values from the encryption string. For instance, if the encryption string has 10 characters (index values 0 - 9), when processing the 11th input character (index 10), simply use the input character index % length of encryption string (in this case 10 % 10 is 0) to select the value from the encryption string to use for mapping.
The decryption process is basically the same as the encryption process. The only difference is the value of the mapped character from the encryption string.
For lower case encryption, the mapped value = character from encryption string - 'a'
For upper case encryption, the mapped value = character from encryption string - 'A'
For lower case decryption, the mapped value = 26 - (character from encryption string - 'a')
For upper case decryption, the mapped value = 26 - (character from encryption string - 'A')
Program Requirements
Your program must meet the following requirements:
1. You must ask the user if they want to perform an encryption or decryption operation.
2. You must ask the user to enter the name of the file they want to encrypt or decrypt.
3. You must get an encryption key from the user which can be up to 128 characters. The key must be all lower case alphabetic characters.
4. You must have a function which takes the encryption key and creates an encryption map from it. For each character in the encryption key string, subtract the lower case letter 'a' and store the result in the corresponding encryption map array.
5. You must have a function which takes the encryption key and creates a decryption map from it. For each character in the encryption key string, subtract the lower case letter 'a' from it. Then subtract that result from 26 and store the value in the corresponding decryption map array.
6. You must have a function which will do the encryption or decryption transformation. This function takes the following parameters:
A constant C string containing the line of text to be transformed.
A constant C character array which contains the encryption or decryption map.
An integer which contains the length of the encryption map.
A string reference (output) which will contain the encrypted or decrypted string upon completion.
The core of the encryption / decryption algorithm is as follows:
For each character (the ith character) in the text input line do the following:
if the character is not alphabetical, add it to the end of the output string
if the character is lower case alphabetical
subtract the character 'a' from the character
get the ith % map length element from the map and add it to the character
adjust the value of the character % 26 to keep it within the alphabet
add the character 'a' to the character
add the encrypted character value to the end of the output string
if the character is upper case alphabetical
do the same thing as for lower case except use 'A' instead of 'a'
7. For decryption, the main program should create an ifstream for the file to be decrypted. It should use the getline method of the ifstream to read lines from the file, call the encryption / decryption function with the line to be decrypted, and display the string which contains the result of the encryption / decryption function call. Repeat until the ifstream reaches the end of the file, then close the ifstream.
8. For encryption, the main program should create an ifstream for the file to be encrypted. It should also create an ofstream for the file where the encrypted result will be stored. The file name for this file can be gotten from the user or can be the input file name with a special extension added at the end. The getline method of the ifstream is used to read lines from the input file. Then the encryption / decryption function is called to encrypt the line. Display the string containing the result and write the string to the ofstream. Close the ifstream and ofstreams when finished.
9. Make sure that your program allows the user to encrypt / decrypt more than one file per session. This means adding a loop which allows the entire program to repeat until the user has nothing more to do.
Hints
1. Use C strings for the encryption string and the file names. Use char arrays for the encryption and decryption maps. You cannot treat these as C strings because the maps can contain 0 as a valid data item rather than the end of string marker.
2. Use a string type variable to hold the encrypted and decrypted strings. The string type allows you to add characters to the end of a string using the push_back method, and it allows you to dump the contents of the string using the erase method.
3. For input streams, you can use the eof method to determine when you have reached the end of a file.
4. Use a character array to read data from the files. Set the maximum length for this buffer to be 256 characters.
Development Strategy
I would recommend that you build this project in two phases. The first phase should concentrate on getting the encryption and decryption map functions and the encryption / decryption function working. You can test this by using fixed C strings for the input line and the encryption string. Call the map functions, then encrypt the fixed input string, output the result, then decrypt the encrypted string and output the result. When your final output is the same as the original input, your encryption / decryption functions are working. The second phase adds the file operations.
Testing and Deliverables
When you think you have a working program, use Notepad to create a file with plain text in it. You should enter some different length lines containing a variety of characters. Your file should have at least 10 lines. You should try using short and long encryption keys. Using your program, encrypt the file, then decrypt the encrypted file. Take a screen shot of your decrypted output and paste it into a Word document. Also copy the contents of your original file and the encrypted file into the Word document. Clearly label the contents of the Word document. Then copy your source code into your document. Make sure that you have used proper coding style and commenting conventions!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void mapitE(string,char[]);
void mapitD(string,char[]);
void encryptdecrypt(const string,const char[],int,string&);
int main()
{string ende[]={"encrypt","decrypt"},buffer,key,newbuffer="";
char filename[80],enmap[128],demap[128],again;
int i,maplength;
ofstream out;
ifstream in;
int choice;
do
{
cout<<"Enter 1 to encrypt, 2 to decrypt: ";
cin>>choice;
while(choice<1||choice>2)
{cout<<"invalid choice\n";
cout<<"Enter 1 to encrypt, 2 to decrypt: ";
cin>>choice;
}

cout<<"Enter name of your file to "<<ende[choice-1]<<": ";
cin>>filename;
in.open(filename);
if(in.fail())
{ cout<<"input file did not open please check it\n";
system("pause");
return 1;
}

cout<<"Enter name of your output file: ";
cin>>filename;
out.open(filename);
cout<<"Enter your encryption key (max 128 characters): ";
cin>>key;
if(key.length()>128)
{cout<<"key too long\n";
cout<<"Enter your encryption key (max 128 characters): ";
cin>>key;
}
maplength=key.length();

if(choice==1)
mapitE(key,enmap);
else
mapitD(key,demap);
getline(in,buffer);
while(in)
{
if(choice==1)
encryptdecrypt(buffer,enmap,maplength,newbuffer);
else
encryptdecrypt(buffer,demap,maplength,newbuffer);
out<<newbuffer;
newbuffer.erase(0);
getline(in,buffer);

}
out.close();
in.close();
in.clear();
out.clear();
newbuffer.erase(0);
cout<<"do it again(y/n)? ";
cin>>again;
}while(toupper(again)=='Y');
return 0;
}
void encryptdecrypt(const string buffer,const char map[],int len,string& newbuffer)
{int i=0;
char t,code;
for(i=0;i<buffer.length();i++)
{ //cout<<buffer[i]<<" "<<i<<endl;
if(isalpha(buffer[i]))
{if(islower(buffer[i]))
code='a';
else
code='A';
t=buffer[i]-code;
//cout<<code<<" "<<buffer[i]<<" "<<t<<" "<<i<<endl;
t=t+map[i%len];
t=t%26;
t=t+code;
newbuffer.push_back(t);
}

else
newbuffer.push_back(buffer[i]);
}
newbuffer.push_back('\n');
}
void mapitE(string key,char map[])
{
int i;
for(i=0;i<key.length();i++)
map[i]=key[i]-'a';
}
void mapitD(string key,char map[])
{
int i;
for(i=0;i<key.length();i++)
map[i]=26-(key[i]-'a');
}



I have this code.. but I keep getting this when I plug it through visual studio.. it's like it can't locate the notepad file I have created with my input to encrypt...

Attached image(s)

  • Attached Image

This post has been edited by jimblumberg: 22 August 2012 - 08:12 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Writing Code for using strings

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: Writing Code for using strings

Posted 22 August 2012 - 08:19 AM

Quote

I have this code.. but I keep getting this when I plug it through visual studio.. it's like it can't locate the notepad file I have created with my input to encrypt..

What exactly did you enter for the input file name?

Your Windows error states that it doesn't like the "\\ilabs\home$" path.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 mbaker421  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-August 12

Re: Writing Code for using strings

Posted 22 August 2012 - 08:25 AM

I have another code that I am using instead which seems to work better but I still don't understand how to get here.."Using your program, encrypt the file, then decrypt the encrypted file. " Here is the other code...
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void mapitE(string,char[]);
void mapitD(string,char[]);
void encryptdecrypt(const string,const char[],int,string&);
int main()
{string ende[]={"encrypt","decrypt"},buffer,key,newbuffer="";
char filename[80],enmap[128],demap[128],again;
int i,maplength;
ofstream out;
ifstream in;
int choice;
do
{
cout<<"Enter 1 to encrypt, 2 to decrypt: ";
cin>>choice;
while(choice<1||choice>2)
{cout<<"invalid choice\n";
cout<<"Enter 1 to encrypt, 2 to decrypt: ";
cin>>choice;
}

cout<<"Enter name of your file to "<<ende[choice-1]<<": ";
cin>>filename;
if (filename=="input")
in.open("input.txt");
if(in.fail())
{ cout<<"input file did not open please check it\n";
system("pause");
return 1;
}

cout<<"Enter name of your output file: ";
cin>>filename;
if (filename=="output")
out.open("output.txt");
cout<<"Enter your encryption key (max 128 characters): ";
cin>>key;
if(key.length()>128)
{cout<<"key too long\n";
cout<<"Enter your encryption key (max 128 characters): ";
cin>>key;
}
maplength=key.length();

if(choice==1)
mapitE(key,enmap);
else
mapitD(key,demap);
getline(in,buffer);
while(in)
{
if(choice==1)
encryptdecrypt(buffer,enmap,maplength,newbuffer);
else
encryptdecrypt(buffer,demap,maplength,newbuffer);
out<<newbuffer;
newbuffer.erase(0);
getline(in,buffer);

}
out.close();
in.close();
in.clear();
out.clear();
newbuffer.erase(0);
cout<<"do it again(y/n)? ";
cin>>again;
}while(toupper(again)=='Y');
return 0;
}
void encryptdecrypt(const string buffer,const char map[],int len,string& newbuffer)
{int i=0;
char t,code;
for(i=0;i<buffer.length();i++)
{ //cout<<buffer[i]<<" "<<i<<endl;
if(isalpha(buffer[i]))
{if(islower(buffer[i]))
code='a';
else
code='A';
t=buffer[i]-code;
//cout<<code<<" "<<buffer[i]<<" "<<t<<" "<<i<<endl;
t=t+map[i%len];
//t=t&;
t=t+code;
newbuffer.push_back(t);
}

else
newbuffer.push_back(buffer[i]);
}
newbuffer.push_back('\n');
}
void mapitE(string key,char map[])
{
int i;
for(i=0;i<key.length();i++)
map[i]=key[i]-'a';
}
void mapitD(string key,char map[])
{
int i;
for(i=0;i<key.length();i++)
map[i]=26-(key[i]-'a');
}


my input file to encrypt is saved as a notepad file on my computer as just simply "input.txt"...

This post has been edited by jimblumberg: 22 August 2012 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: Writing Code for using strings

Posted 22 August 2012 - 08:29 AM

Please use code tags when posting code!!!

:code:

Next find an indentation style you like and use it consistently, this will make you code much more readable and easier to troubleshoot.

Jim

This post has been edited by jimblumberg: 22 August 2012 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 mbaker421  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-August 12

Re: Writing Code for using strings

Posted 22 August 2012 - 08:33 AM

Sorry.. thanks...Can you help me with this part of the code program..:

Using your program, encrypt the file, then decrypt the encrypted file.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: Writing Code for using strings

Posted 22 August 2012 - 08:35 AM

Please explain why you ask the user for the name of a file, yet insist that they enter "input"?
cout<<"Enter name of your file to "<<ende[choice-1]<<": ";
cin>>filename;
if (filename=="input")
in.open("input.txt");

All you need is the in.open() line if you want to always open "input.txt".

Jim
Was This Post Helpful? 0
  • +
  • -

#7 mbaker421  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-August 12

Re: Writing Code for using strings

Posted 22 August 2012 - 08:50 AM

It doesn't always have to be input.txt.... that is just what the name of the file in notepad is...it could be named something else
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Writing Code for using strings

Posted 22 August 2012 - 09:29 AM

Hmmmm....
Was This Post Helpful? 2
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: Writing Code for using strings

Posted 22 August 2012 - 10:11 AM

Well he was careful to state "I have this code". He never said "I wrote this code". :lol:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1