Thread: Encryption using bitwise operator in C

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    51

    Encryption using bitwise operator in C

    How to check that my encryption is successful.
    Code:
    #include<stdio.h>
    #include<conio.h> 
    
    
    main()
    {
        FILE *fp, *ft;
        char ch;
        fp = fopen("Encrypt.txt","r");
        ft = fopen("Encrypt1.txt","w");
        if (fp == NULL)
        printf("File can't be opened\n");
        if (ft == NULL)
        printf("File can't be opened\n");
    
    
        while((ch = getc(fp)) != EOF)
        {
            putc(~ch , ft);
        //    printf("%c", ch);
        //    system("pause");
        }
        while((ch = getc(ft)) != EOF)
        {
            printf("%c", ch);
        }
        system("pause");
        fclose(fp);
        fclose(ft);
        
        
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you open the file for writing, write to it, close it, then open the file for reading and print the integer values of the characters read so you can check that the computation was performed as you expected.

    Note that this barely qualifies as encryption: you don't have a secret key other than the bitwise not algorithm itself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "ch" should be declared as an int (this is because of EOF)

    If your opening of fp/ft fail, don't let the code get to the while loop.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Well... to be blunt, this is not "encryption", but "encoding"...

    I suggest to study DES (old standard) and AES. Both standards available online.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    As for your question - To see if it worked, try not using any encoding, see what is printed in your output file.

    Once that is working, add your encoding back in and then try to open the file.

  6. #6
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by flp1969 View Post
    Well... to be blunt, this is not "encryption", but "encoding"...

    I suggest to study DES (old standard) and AES. Both standards available online.
    OK Sir.
    I don't know why the teacher is saying it like that i.e. bitwise operator, one's complement can encrypt data. Maybe just to give me an example of practical usage. It is me who take things seriously! my fault Sir!

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by rm82co View Post
    OK Sir.
    I don't know why the teacher is saying it like that i.e. bitwise operator, one's complement can encrypt data. Maybe just to give me an example of practical usage. It is me who take things seriously! my fault Sir!
    IMHO, it is only a matter of semanthics... If you never heard abour morse code this is a "hidden message":

    .... .. -.. -.. . -. ....... -- . ... ... .- --. .

    But it is a code... If you never heard about Julius Caesar's ROT13 algorithm, you can think the same way... but it is s code... To "encrypt" a message you need an algorithm and a key, to "encode" you only need the algorithm.


    Again, IMHO...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by flp1969
    IMHO, it is only a matter of semanthics... If you never heard abour morse code this is a "hidden message":

    .... .. -.. -.. . -. ....... -- . ... ... .- --. .

    But it is a code... If you never heard about Julius Caesar's ROT13 algorithm, you can think the same way... but it is s code... To "encrypt" a message you need an algorithm and a key, to "encode" you only need the algorithm.
    ROT13 is not a "code", it is a cipher: a Caesar cipher with a key of 13 in the modern English alphabet. The term "code" as used in Morse code is arguably different from "code" as used in cryptography, where it refers more to encoding messages at word-level or even sentence-level into another form that obscures their meaning, rather than to encoding from one character representation to another. Hence, rm82co's scheme isn't a cryptographic "code", but a "cipher", although an exceedingly simple substitution cipher in which the key is fixed to just one possible value.
    Last edited by laserlight; 04-09-2019 at 02:01 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by flp1969 View Post
    IMHO, it is only a matter of semanthics... If you never heard abour morse code this is a "hidden message":

    .... .. -.. -.. . -. ....... -- . ... ... .- --. .

    But it is a code... If you never heard about Julius Caesar's ROT13 algorithm, you can think the same way... but it is s code... To "encrypt" a message you need an algorithm and a key, to "encode" you only need the algorithm.


    Again, IMHO...
    Morse code I have studied in my Telecommunication course.
    I could get the point. Thanks

  10. #10
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Between! I have following one. It is working i.e. encoding original file and then decoding back to original file as teacher was saying one's complement will encode and then again one's complement will decode. Encryption using bitwise operator in C-original-png
    Encryption using bitwise operator in C-encode-pngEncryption using bitwise operator in C-decode-png
    Code:
    #include<stdio.h>
    #include<conio.h> 
    #include<stdlib.h>
    main()
    {
    	FILE *fp, *ft, *fs;
    	char ch;
    	fp = fopen("original.txt","r");
    	ft = fopen("encode.txt","w+");
    	fs = fopen("decode.txt","w");
    	if (fp == NULL)
    		{
    		printf("File can't be opened\n");
    		exit(0);
    		}	
    	if (ft == NULL)
    	{
    	printf("File can't be opened\n");
    	exit(0);	
    	}
    	
    	if (fs == NULL)
    	{
    	printf("File can't be opened\n");
    	exit(0);	
    	}
    	
    	while((ch = getc(fp)) != EOF)
    	{
    		putc(~ch , ft);
    	//	printf("%c", ch);
    	//	system("pause");
    	}
    	fclose(fp);
    	fclose(ft);
    	//remove("original.txt");
    	//rename("original.txt", "encode.txt");
    	fp = fopen("encode.txt", "r");
    	if(fp == NULL)
    	{
    		printf("File can't be opened\n");
    		exit(0);
    	}
    	while((ch = getc(fp)) != EOF)
    	{
    		/*printf("%c", ch); after encrypt data is not showing character ascii values so nothing display
    		encrypt data now may be in form of control commands or else.
    		To check if encryption is successful see the file in hard disk*/
    		putc(~ch , fs);
    		
    	}
    	
    	system("pause");
    	fclose(fp);
    	fclose(ft);
    	fclose(fs);
    	
    	/*
    	Both r+ and w+ can read and write to a file. However, r+ doesn't delete the content of the file and doesn't create 
    	a new file if such file doesn't exist, whereas w+ deletes the content of the file and creates it if it doesn't exist.
    	*/
    	
    	
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You shouldn't open three files at once when you only need two at that time: the first for reading and the second for writing. After closing those two files, when you need to open the second file again for reading, you can then do that.

    Also, ch is still a char when it should be an int, as mentioned by Click_here in post #3.

    And honestly, just call it "encryption" if you will. As I noted in post #8, in a cryptographic context, the use of "code" and "encoding" here is inappropriate as it refers to a different though related concept: it is a simple substitution cipher, as exceedingly simple as it may be.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Enciphering is encoding, in a sense that both transforms a group of symbols in another.

    Morse Code is a simple table driven transformation. ROT13 is a simple arithmetic transformation. Base64 is a simple grouping of bits. These are codes, not ciphers, because, knowing the method, anyone can decode the message.

    If you know method, but need also a key to decode, then is "encryption". The method cannot be the key.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You only know the key for ROT13 because it is in the name. It's actually a Caesar cipher with a particular key, one so popular that it was given its own name. The other two you mentioned have never had a cryptographic context (even if Base64 might be used post-encryption: it has never been regarded as part of the encryption itself).

    Yes, I agree with you on the overlap, and so in that sense any simple substitution cipher for which the key is fixed/known is an "encoding", but that's a more general sense of the term, not how "code" has historically been used in cryptography.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    About the rm82co's code... Why not do something like this:

    Code:
    /* not_encode.c */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( int argc, char *argv[] )
    {
      int c;
    
      // No arguments!
      if ( argc != 1 )
        return EXIT_FAILURE;
    
      while ( ( c = fgetc( stdin ) ) != EOF )
        fputc( ~c, stdout );
    
      return EXIT_SUCCESS;
    }
    You just have to call the program as:

    Code:
    $ ./not_encode < text > encoded_text

  15. #15
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    @flp1969 This code is displaying nothing on the screen.
    Last edited by rm82co; 04-11-2019 at 05:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using bitwise operator to see if x>y
    By Lina_inverse in forum C Programming
    Replies: 33
    Last Post: 09-23-2012, 07:16 PM
  2. Replies: 6
    Last Post: 03-08-2012, 05:37 PM
  3. bitwise operator
    By donkey_C in forum C Programming
    Replies: 7
    Last Post: 09-26-2010, 02:47 PM
  4. Bitwise Operator Help
    By ggraz in forum C Programming
    Replies: 2
    Last Post: 09-28-2008, 09:20 AM
  5. bitwise operator
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 09-30-2005, 01:17 AM

Tags for this Thread