Cipher File

This is a discussion on Cipher File within the C++ Programming forums, part of the General Programming Boards category; ı want to make a file which is cipher file.. my algorithm is open current file create new file while ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    44

    Cipher File

    ı want to make a file which is cipher file.. my algorithm is

    open current file
    create new file
    while EOF
    (
    read byte from current file (int form)
    byte XOR 1
    write new byte to new file
    )

    close
    close etc...
    is it possible?? if its possible how can ı do this.. give advice....

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by koyboy View Post
    ı want to make a file which is cipher file.. my algorithm is

    open current file
    create new file
    while EOF
    (
    read byte from current file (int form)
    byte XOR 1
    write new byte to new file
    )

    close
    close etc...
    is it possible?? if its possible how can ı do this.. give advice....
    do not use EOF to control loops - read FAQ
    search FAQ and forum for usage of fstream

    if you plan read byte-byte - searcg for usage of get() function

    write some code - post it when you have problems
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well you've all but written the code, all you need to do is make it syntactically correct C code and you're done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    koyboy,

    I recommend that you "develop" your code one step at a time... This is a good approach to any programming task...

    1. Start with a program that reads one byte at a time and displays it. I'd suggest displaying the numerical value and the ASCII character (if any) for each byte. (It's always helpful to "see" what your program is doing.)

    2. Add the code to save each byte to a new file. (If you read/write a simple text file, you can check your new file with Notepad (or a similar text editor) to make sure everything's working properly so far.

    3. Add the code to encrypt each byte before saving it. (It would probably be helpful to display the encrypted byte along with the unencrypted byte.)

    4. When everything works, you can delete (or comment-out) the code that displays the bytes.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    44
    thanks for your interest.. ı wrote it like this..

    Code:
    FILE *in,*out;
    in  = fopen(gonder,  "rb");
        out = fopen(yeni, "wb");
        while((ch = fgetc(in)) != EOF)
            putc(ch ^ 1, out);    
        fclose(out);
        fclose(in);

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Great start! You could also try something like some sort of simple password key.

    Example:
    Code:
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    
    #define BUF_SIZE  256
    
    int main(void)
    {
        char buffer[BUF_SIZE];
    
        std::puts("Password: ");
        std::fgets(buffer, BUF_SIZE, stdin);
    
        std::FILE *in, *out;
    
        in = std::fopen("gonder", "rb");
        out = std::fopen("yeni", "wb");
    
        if(in && out)
        {
            int size = std::strlen(buffer), i = 0, ch;
    
            while(!std::feof(in))
            {
                if((ch = std::fgetc(in)) != EOF)
                    std::putc(ch ^ buffer[i % size], out);
                ++i;
            }
    
            std::fclose(in);
            std::fclose(out);
    
            return 0;
        }
    
        if(in)
           std::fclose(in);
        if(out)
           std::fclose(out);
    
        return -1;
    }
    Just to maybe give you a nudge in the direction you are wishing to go.

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    is this for an assignment or do you just need some encryption/decryption code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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