Thread: how to encrypt/decrypt

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    Question how to encrypt/decrypt

    I need to encrypt/decrypt a set of numbers such as '1234'. The next stept to encrypting this number set is to ad '7' to each of the charaters ex = 1(becomes 8), 2(becomes 9), 3(becomes 0) and 4(becomes 1). Now, the addition part isn't causing me headaches yet, but how to pull the 1, 2,3, & the 4 out and add the 7 to them. Can someone show me how this works. Without running the code, it took me all of 15 seconds to realize that 1234 + 7777 wasn't what I wanted.

    Thanks
    BitWise
    Last edited by bitWise; 10-13-2001 at 12:40 PM.

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Do you want to get these numbers (1234) from the user? This is how you would do this:
    Code:
    char string[20];
    int value[4];
    
    fgets(string, sizeof(string), stdin);
    sscanf(string, "%d%d%d%d", &value[0], &value[1], &value[2], &value[3]);
    This puts each value into the array.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi
    You could try storing your numbers as a string - the general way of encryption.
    Code:
    char numbers[5] = "1234";
    char encrypt[5];
    int i;
    
    for(i = 0; i < 5; i++)
          encrypt[i] = numbers[i] + 7;
    
    puts(encrypt);  /*show encrypted numbers*/
    Last edited by bigtamscot; 10-13-2001 at 12:30 PM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    That won't quite work, as '9' + 7 is not '6' as desired.

    This, I think, will work.
    try this:

    Code:
    int temp, int exp = 0, int encrypted = 0, int normal;
    
    //set normal to the val to be encrypted.
    
    while(normal > 0){
    	temp = normal % 10; // gets the last digit of normal
    	temp += 7; //adds 7
    	temp = temp % 10; //does wraparound such that 10 = 0, 11 = 1, etc.
    	encrypted += temp * pow(10,exp); // puts this digit in its appropriate place,
    	exp ++; //now, we want to do the next digit, so increase the exponent,
    	normal /= 10; //shift all the digits down, discarding the lowest which we've already encrypted.
    }
    Note, you can condense a lot of these steps(you can do it in 3 lines), but it's clearer written like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. looking for simple encrypt/decrypt commands
    By sept in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2007, 07:11 PM
  2. Encrypt/Decrypt files by XOR
    By amirahasanen1 in forum C++ Programming
    Replies: 27
    Last Post: 05-22-2005, 02:38 PM
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  4. Encrypt/Decrypt
    By bitWise in forum C Programming
    Replies: 2
    Last Post: 10-14-2001, 03:48 PM