Encripting Using ASCII

This is a discussion on Encripting Using ASCII within the C Programming forums, part of the General Programming Boards category; Hello there, yet again, my noobness. Ok, so I need to encrypt a file using this delimitations: -You will be ...

  1. #1
    Registered User
    Join Date
    Sep 2011

    Stack Structure -Help me outttttt

    Hello there, yet again, my noobness.

    Ok, so I need to encrypt a file using this delimitations:

    -You will be given a file with a character, which is your key.(in this hypothetical situation lets use 'a')

    -You will encrypt a string by each character. for instance



    -add the value of your character with your key and then multiply it by 2.
    For the sake of not doing this as big as it will be,
    Lets do a test with h

    h=104 + a=97
    104+97=201 * 2 = 402(which doesn't exist..)

    AS there are only 127 characters, how can I limit this to be in range of the 127.

    ***Consider that I need to take the characters which are the encryption and turn them back to normal using the same value. (in this case 'a')
    btw all this has to be done with only stdlib.h stdio.h(no fancy libraries or commands)

    I need desperate help.

    Ill virtually take a knee and kiss your feet if you help me out.

    (>'')> kirby hug!

    Thank you for being supportive to your fellow humans.

    TOTAL EDIT I've managed to do the encryption and come back from it but now Im having a problem with staking structures...

    Heres the code, For some reason when I try to print the information y the nodes it doesnt go forward. from "hola" it just prints "a" when it should print "aloh"
    Whats wrong?

    typedef struct datos{
      char letra;
      struct datos *sig;
    int main(int argc, char **argv)
      LISTA *inicio, *nodo;
     char caracter;///caracter leido desde archivo, es archivo a modificar.
      char llave;///caracter leido desde el archivo llave
      int variable;
      int contador=0;
      FILE *lectura;///archivo a leer(encriptar)
      FILE *escritura;///archivo donde se encriptara
      FILE *llavetura;///archivo llave
      if((lectura=fopen(argv[1],"r"))==NULL)//abrimos archivos
          printf("no se puede abrir lectura\n");
          printf("si se pudo abrir lectura\n");
      if(( escritura=fopen(argv[2],"w"))==NULL)
          printf("No se pudo escritura\n");
          printf("si se pudo escritura\n");
      if(( llavetura=fopen(argv[3],"r"))==NULL)
          printf("No se pudo llavetura\n");
          printf("si se pudo llavetura\n");
        }    ////acabamos de abrir archivos
      nodo=malloc(sizeof(LISTA));//Create Space
          printf("No hay memoria");
     while((caracter=getc(lectura))!=EOF)//save on the space each character...but I dont think this is working, could you help me out?
    printf("Estamos entrando por %d con la variable %c\n",contador,nodo->letra);
        }//guardamos en cada espacio cada caracter.
          printf("%c \n", nodo->letra);
    Last edited by XIIIX; 09-19-2011 at 06:45 PM. Reason: Something got solved.

  2. #2
    Registered User
    Join Date
    May 2011
    Around 8.3 light-minutes from the Sun
    No need to start a new thread. Post the code you have developed so far and we will go from there. In order to do this you only need to know Loops and File operations
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    Mods, any way you can merge this thread with: Comparing and integer variable with a char variable.?

    @XIIIX: My help isn't desperate, but perhaps you'll accept it anyway?

    This post was much clearer in identifying what you have to do and what your problem is.

    As I stated earlier, there are only 128 different ASCII values (0..127), but that doesn't mean you can't store non-ASCII values in a file. ASCII is one way to interpret data -- as a text we can read. But there's nothing stopping you from reading/writing larger values (say an int) to your file. The file is encrypted, so readability is not an issue, and we don't need to worry whether 402 has an ASCII value. You just need to store that number in the file. I might ask your professor (I can only assume this is a homework assignment) exactly what he wants you to do with encrypted values that are larger than one byte. Regardless, you can not use putc or the like since those only write one byte, and encrypted values like 402 don't fit in a byte. You'll probably have to use fwrite. The general approach you will take is:
    while ((read c from input file) != EOF)
        c = 2 * (c + key)
        write c to output file

  4. #4
    Registered User
    Join Date
    Sep 2011
    I've edited my original question. Now Im having trouble with the structure (stack type) (LIFO)...

  5. #5
    Registered User
    Join Date
    Sep 2011
    I hate structures.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    A structure is just a grouping of items. You group them by calling them a struct <name> and surrounding them with a pare of braces:
    struct yourstructname
        ...everything you want to group...
    Then you simply declare one when you want to use it:
    struct yourstructname yourvariablename;
    To access the items you have grouped, you use the dot operator:
    struct ysn yvn;
    yvn.thingigrouped1 = somevalue;
    someothervalue = yvn.otherthingigrouped2;

    Hope is the first step on the road to disappointment.

  7. #7
    Join Date
    Aug 2010
    Ontario Canada
    OK... What you need to do is step away from your keyboard for a bit and actually think about what you are being asked to do...

    Think about it step by step... don't think about code or writing source files... think about the problem itself. Analyse the problem...

    1) The first character in the file is your "key"... so, you have the basis of your math.
    2) The encryption method described produces values bigger than a char can hold... what variable type can hold it?
    3) What is the reverse process and how does it work?

    Sit down and work the problem with pencil and paper... take a word, say "regulations"... encrypt it manually... what do you get?
    Now sit down and work out how to reverse that encryption... what do you need to do that and what is the process?

    Ok... so once you understand the actual problem you are given, it's time to start planning out how you intend to solve the problem...
    What elements does your program need... What functions must it perform and in what order.
    Keep breaking it down until you know in small detail exactly how to make your code do what you want.

    Now, step three ... sit down with your new understanding of the problem and your planned solution and write the code.

    Finally test your code and make sure it behaves as you need...

    It's the same process for every program you write... analyse -> plan -> write -> test ... no exceptions.

    The classic error beginners make is that they want to get straight to the keyboard and start writing program code... this is feasably impossible unless you actually understand both the problem and it's solution before hand. As I've said way too many times... no programmer, no matter how skilled or experienced, can ever write the solution to a problem he or she does not understand... it simply cannot be done (As you've now discovered.)

    So there's your next step... stop trying to write code for a problem you do not yet understand...

    I've looked over the requirement you gave at the top of this thread and I could write the solution in something like 50 lines, using no structures, no stacks or state machines, no linked lists and no memory allocations... It's a simple math and file i/o problem... nothing more.

  8. #8
    Registered User
    Join Date
    May 2009
    This is the only time I see you allocate space for a node; you need to do it for each node. I do NOT think you do that.

    nodo=malloc(sizeof(LISTA));//Create Space
    Edit: Do what Tater suggested and think about the problem; you might try breaking the problem down into steps and deciding which steps can be coded/tested by themselves as separate functions.

    Tim S.
    Last edited by stahta01; 09-19-2011 at 07:20 PM.

  9. #9
    Join Date
    Aug 2010
    Ontario Canada
    I need desperate help.
    Ill virtually take a knee and kiss your feet if you help me out.
    I'll probably take it on the chops for this but here's your encryptor/decryptor, tested and working in 53 lines...
    (This is your one and only freebie... from here follow the 4 steps: Analyse -> Plan -> Write -> Test, that's exactly what I did.)

    crypt.c (compile as crypt.exe)
    // file encrypt and decrypt example
    // encrypt and dectypt using char + key * 2 
    #include <stdio.h>
    #include <stdlib.h>
    int main (int argc, char *argv[])
      { int crypt;  // encryption key
        int fval;   // file values
        FILE *infile, *outfile;
        if(argc < 4)
          { printf("File encryptor/dectyptor...\n");
            printf("Encrypt or decrypt a file based based on a secret method\n\n");
            printf("Usage : \n");
            printf("     crypt <key> <sourcefile> <outputfile>\n\n");
            printf("Key :\n");
            printf("   To encrypt enter a single character as your encryption key\n");
            printf("   To decrypt enter * as the key\n\n");
            printf("Examples :\n");
            printf("     crypt x mywork.txt safework.txt\n");
            printf("     crypt * safework.txt homework.txt\n\n");
            exit(255); }
        // open the files
        infile = fopen(argv[2],"r");
        if (! infile)
          { printf("Unable to open input file\n\n");
            exit(254); }
        outfile = fopen(argv[3],"w");
        if (! outfile)
          { printf("Unable to open output file\n\n");
            exit(253); }
        // active encryptor     
        if ((argv[1][0] == '*'))
          { printf("Decrypting...\n\n");
            // first value in the file is the key
            if ( fscanf(infile,"%d",&crypt) > 0)
              while(fscanf(infile,"%d",&fval) > 0)
                fputc((fval/2) - crypt,outfile);  }
          { printf("Encrypting...\n\n");
            // set key as first value in file}    
            fprintf(outfile,"%d ",argv[1][0]);
            while( (fval = fgetc(infile)) > 0 )
              fprintf(outfile,"%d ", (fval + argv[1][0]) * 2); }
        return 0; }
    Last edited by CommonTater; 09-19-2011 at 08:44 PM. Reason: typos

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. encripting a string
    By j_spinto in forum C Programming
    Replies: 8
    Last Post: 07-28-2005, 08:47 AM
  4. "encripting" by using ^
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 11-10-2004, 06:50 PM
  5. encripting 4-digit integer?
    By o0o in forum C++ Programming
    Replies: 5
    Last Post: 12-25-2003, 08:48 PM

Tags for this Thread

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