Basic string encrypthion and decryption

This is a discussion on Basic string encrypthion and decryption within the C Programming forums, part of the General Programming Boards category; is there a function (except base64) for this?I meant the string has to be encrypt then decrypt. thanks in advanced...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    32

    Basic string encryption and decryption

    is there a function (except base64) for this?I meant the string has to be encrypt then decrypt.
    thanks in advanced
    Last edited by |HBO|; 08-31-2007 at 02:06 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Encrypting/decrypting for what purpose. Base64 not really encryption, it is basicly a way to encode non-printable characters as printable ones, by encoding 6 bits at a time into one printable character.

    Encryption in my vocabulary means changing something from clear-text to "unreadable" by preforming some sort of translation of the characters. These range from a simple "shuffle the alphabet" (e.g. A => B, B => C, C => D, and so on, to Z => A). So "HELLO" becomes "IFMMP". This is relatively easy to implement in a computer, but also very easy to decrypt without actually knowing the key.

    There are encryption algorithms that are MUCH more complex and "intrusion proof".

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Quote Originally Posted by matsp View Post

    Encryption in my vocabulary means changing something from clear-text to "unreadable" by preforming some sort of translation of the characters. These range from a simple "shuffle the alphabet" (e.g. A => B, B => C, C => D, and so on, to Z => A). So "HELLO" becomes "IFMMP". This is relatively easy to implement in a computer, but also very easy to decrypt without actually knowing the key.
    Mats
    This is exactly what i am looking for.Any ideas or funcs name or funcss will be appreciated.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, there isn't, as far as I know, any standard function that does this. There are plenty of algorithms about if you google for "encryption algorithm". There is also, I'm certain without looking, a collection of encryption/decryption libraries around in places like www.sourceforge.org or such. These will contain the encryption methods in a way that you can use in your application.

    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    anyway..
    i have a func like:
    Code:
     int i=0,j=0;
     while(i < strlen(source)) {
      from[i] += 1;
      i++; j++;
      if (j == strlen(destination)-1) j=0;
      }
    But this contains all ascii s.but i want just to use letter.s.(not others)
    How can i do it?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So you mean if you have "1234ABC12" it should be converted to "1234BCD12"?

    Try using "isalpha()", it will return true for letters, nothing else.

    --
    Mats

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Quote Originally Posted by matsp View Post
    So you mean if you have "1234ABC12" it should be converted to "1234BCD12"?

    Try using "isalpha()", it will return true for letters, nothing else.

    --
    Mats
    Not really.I meant if the string is "HBO" then the output must be "ICP" but if the string contains "z" things getting complicated because of after 'z' there is a '{'.I want to use just letters.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by |HBO| View Post
    Not really.I meant if the string is "HBO" then the output must be "ICP" but if the string contains "z" things getting complicated because of after 'z' there is a '{'.I want to use just letters.
    Ok, so you could build a table that contains your translation, so you index it by "current letter", and at that index, you get the "translated letter".

    Code:
    char encode[26] = "BCDEFGHIJKLMNOPQRSTUVWXYZA";
    char decode[26] = "ZABCDEFGHIJKLMNOPQRSTUVWYXY";
    --
    Mats

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Quote Originally Posted by matsp View Post
    Ok, so you could build a table that contains your translation, so you index it by "current letter", and at that index, you get the "translated letter".

    Code:
    char encode[26] = "BCDEFGHIJKLMNOPQRSTUVWXYZA";
    char decode[26] = "ZABCDEFGHIJKLMNOPQRSTUVWYXY";
    --
    Mats
    But this is the thing that i am confused about .How ?
    Besides thanks for your concern.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Assuming above arrays:

    Code:
    char encodechar(char c)
    {
       return encode[c - 'A'];
    }
    Decode is obviously same but using decode array.

    This assumes that your incoming letter is an upper-case in the range 'A'..'Z' - anything else will have a undefined result.

    --
    Mats

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    even u could use just this way as well

    Code:
    if(str[i] == 'z' || str[i] == 'Z')
          str[i] -= 25;
    else
          str[i] += 1;
    ssharish2005

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    Or, you could do something like (assuming strings always end with a '\0'):
    Code:
    //Buffer size incudes '\0'
    #define BUFFER_SIZE 255
    
    int encrypt(char *source, char *encrypted){
    	int i;
    	for (i=0; source[i]!='\0' && i <BUFFER_SIZE -1 ; i++){
    		encrypted[i]= source[i]+ (long long int) ( i+ tan(i) + exp(i) + pow(i+2,8)) ;
    	};
    	encrypted[i]='\0';
    	return 0;
    }
    
    int decrypt(char *encrypted, char *decrypted){
    	int i;
    	for (i=0; encrypted[i]!='\0' && i < BUFFER_SIZE - 1; i++){
    		decrypted[i]= encrypted[i] - (long long int) ( i+ tan(i) + exp(i) + pow(i+2,8));
    	};
    	decrypted[i]='\0';
    	return 0;
    }
    Last edited by saman007uk; 09-02-2007 at 08:23 AM.

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Quote Originally Posted by saman007uk View Post
    Or, you could do something like (assuming strings always end with a '\0'):
    Code:
    //Buffer size incudes '\0'
    #define BUFFER_SIZE 255
    
    int encrypt(char *source, char *encrypted){
    	int i;
    	for (i=0; source[i]!='\0' && i <BUFFER_SIZE -1 ; i++){
    		encrypted[i]= source[i]+ (long long int) ( i+ tan(i) + exp(i) + pow(i+2,8)) ;
    	};
    	encrypted[i]='\0';
    	return 0;
    }
    
    int decrypt(char *encrypted, char *decrypted){
    	int i;
    	for (i=0; encrypted[i]!='\0' && i < BUFFER_SIZE - 1; i++){
    		decrypted[i]= encrypted[i] - (long long int) ( i+ tan(i) + exp(i) + pow(i+2,8));
    	};
    	decrypted[i]='\0';
    	return 0;
    }
    Thank you but it is not working.
    for encryption:
    Code:
            char buf2[128];
            char *buf1 = "hbo";
            encrypt(buf1, buf2);
            printf("Encrypted: %s\n", buf2);
    ./encrypt
    Encrypted: v

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    Try this:
    Code:
    #include <stdio.h>
    #include <math.h>
    #define BUFFER_SIZE 256
    //Buffer size incudes '\0'
    
    int encrypt(char *source, char *encrypted){
    	int i;
    	for (i=0; source[i]!='\0' && i <BUFFER_SIZE -1 ; i++){
    		encrypted[i]= source[i]+ (long long int) ( i*8 -i+ 100*cos(i) + exp(i) + pow(i+2,8)) ;
    	};
    	encrypted[i]='\0';
    	return 0;
    }
    
    int decrypt(char *encrypted, char *decrypted){
    	int i;
    	for (i=0; encrypted[i]!='\0' && i <BUFFER_SIZE -1 ; i++){
    		decrypted[i]= encrypted[i]- (long long int) ( i*8 -i+ 100*cos(i) + exp(i) + pow(i+2,8)) ;
    	};
    	decrypted[i]='\0';
    	return 0;
    }
    
    int main(void){
    	char source[BUFFER_SIZE]={"TestingTesting ..."};
    	char encrypted[BUFFER_SIZE]={0}, decrypted[BUFFER_SIZE]={0};
    	encrypt(source, encrypted);
    	
    	printf("Encrypted String\n");
    	printf("################\n");
    	printf("&#37;s\n", encrypted); fflush(stdout);
    	printf("################\n\n");
    	
    	printf("Decrypted String\n");
    	printf("################\n");
    	decrypt(encrypted, decrypted);
    	printf("%s\n", decrypted);fflush(stdout);
    	printf("################\n\n");
    	return 0;
    }
    Note that I had to chage the functions a bit (as tan(i) tending to -/+infinity can cause problems).

    Output:
    Code:
    Encrypted String
    ################
    U�}}�;;��ׄp55�
    ################
    
    Decrypted String
    ################
    TestingTesting ...
    ################
    Note that blank spaces and new lines can also be part of the encrypted string!
    Last edited by saman007uk; 09-02-2007 at 12:43 PM.

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Thank you very much.
    Encrypted String
    ################
    U�}}�;;��ׄp55�
    ################
    But this is the thing i am dealing with.I don't wanna it contains punctuation mark.(i meant the output must be isalnum char.)
    Regards.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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