I have big problem with encryption by ASCII

This is a discussion on I have big problem with encryption by ASCII within the C++ Programming forums, part of the General Programming Boards category; hello every body here just today I knew I am stupid in C++ because I taken 2 hours thinking about ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    10

    I have big problem with encryption by ASCII

    hello every body here

    just today I knew I am stupid in C++ because I taken 2 hours thinking about this program

    I have to write two programs

    first program encrypt file from hard drive (.txt) and ask the user to enter the key to encrypt the file for example:

    A=65 if we ask the user enter key and he enter 3

    3+65 = 68 the letter after encrypt will be D because D=68

    the kind of encryption is ASCII

    second program same thing but decryption the file.

    this my tried for quiestion 1

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    void main()
    {
    	int key;
    	char x;
    
    	ifstream in_stream;
    	ofstream out_stream;
    
    	in_stream.open("encrypt.txt"); //it's already in the hard drive and contain some words or digits.
        out_stream.open("decrypt.txt");//the program it will be create this file.
    	in_stream >> x;
    
    	
    
    	while(!in_stream.eof())
    	{
    		if(x>='a' && x<='z' || x=='A' && x=='Z' ||  x>=0 && x<9)
    		{
    			cout <<"Enter the key to encrypt your file\n";
    			cin>>key;
    			in_stream.get(x);
    			x=key+x;
    			cout<<x<<endl;
    		}
    	}
    	in_stream.close();
    	out_stream.close();
    }

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    So what's the problem?

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    10
    the problem how can I encrypt the file.

    the code doesn't work.
    Last edited by LINUX; 04-27-2007 at 08:38 AM.

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Here's some code for cin that encrypts with 3. You probably want the key to be set once and used for every character instead of a different key for each character. That way you don't have to keep save all the keys for decryption.
    Code:
    #include <iostream>
    #include <cctype>
    
    
    using namespace std;
    
    
    int main() {
      int key = 3;
      char x;
    
      while ( cin.get( x ) ) {
        if ( isalnum( x ) ) {
          x += key;
        }
    
        cout << x;
      }
    }
    To decrypt you need the same key and just do -= instead of +=.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    10
    thank you very much Noir.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    This is a very simply encryption scheme however

    Simple shift, easily broken, if you want to busy yourself in the future you can look into some more advanced stuff

    have fun!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    india->tamil nadu
    Posts
    18
    try to open the file via fstream

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    27

    Thumbs down

    Quote Originally Posted by Junior89 View Post
    This is a very simply encryption scheme however

    Simple shift, easily broken, if you want to busy yourself in the future you can look into some more advanced stuff

    have fun!
    This is most likely a homework thing. It says questions 1 and 2. I did something like this in CS160 I believe. It's call the ceaser cipher, or the ceaser shift, and at any rate, it was created by ceaser as a means of "encrypting" his messages so if lost they couldn't be figured out. Being that old, its not that bad of an idea lol. At any rate, don't bash the encryption if it was assinged... theres no reason to do something more complicated and get a bad grade due to incompetence in following directions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple, silly yet big problem....
    By punk in forum C++ Programming
    Replies: 7
    Last Post: 12-20-2007, 10:25 AM
  2. Problem To Sum All Ascii
    By gtr_s15 in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2005, 01:44 AM
  3. Small program big problem :-(
    By Fibre Optic in forum C++ Programming
    Replies: 4
    Last Post: 09-20-2005, 08:53 AM
  4. ASCII Exceptions problem
    By Bazza in forum C Programming
    Replies: 6
    Last Post: 07-10-2002, 08:58 AM
  5. Results for the Encryption Contest -- June 23, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-07-2002, 08:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21