Thread: Problem converting binary data to text using base64

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200

    Question Problem converting binary data to text using base64

    Hello, I have two C functions, one is RC4 which succesfull produce binary data from any text, second is base64 which also works great. The problem occur when I want to combine these two method to gain simple method to convert and save binary data to text using base64.

    Usage of program is very simple, you threw some text into it and it's returning encoded and decoded version. Even if it works for very short text like "123" completely fails with longer like "Hello World"

    I belive it's not hard to resolve but I have not enought C knowledge to do this. Could you please help ?

    http://codepad.org/CrAgF6dU

    Thanks for help

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    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
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Quote Originally Posted by laserlight View Post
    How does it not work?
    Have you tried it with even simple text like " Hello world !" ? If so, take a look at decoded string, unfortunately it's wrong.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MartinR View Post
    Hello, I have two C functions, one is RC4 which succesfull produce binary data from any text, second is base64 which also works great. The problem occur when I want to combine these two method to gain simple method to convert and save binary data to text using base64.

    Usage of program is very simple, you threw some text into it and it's returning encoded and decoded version. Even if it works for very short text like "123" completely fails with longer like "Hello World"

    I belive it's not hard to resolve but I have not enought C knowledge to do this. Could you please help ?


    C code - 226 lines - codepad

    Thanks for help
    That suggests that either one or the other does not "work great". Compiling your code gives the following:
    Code:
    crypt.c: In function 'Base64Decode':
    crypt.c:41:38: warning: initializer-string for array of chars is too long [enabled by default]
    crypt.c: In function 'encodeblock':
    crypt.c:71:38: warning: initializer-string for array of chars is too long [enabled by default]
    crypt.c: In function 'main':
    crypt.c:165:9: warning: unused variable 'input' [-Wunused-variable]
    crypt.c:164:9: warning: variable 'rc' set but not used [-Wunused-but-set-variable]
    crypt.c:162:59: warning: unused variable 'pch' [-Wunused-variable]
    The unused variables aren't going to matter, but the fact that you have 66 characters in your base64 very well might.

  5. #5
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Quote Originally Posted by tabstop View Post
    The unused variables aren't going to matter, but the fact that you have 66 characters in your base64 very well might.
    Yes, there was indeed two more charasters but if you repair it, function still doesn't work as it should.

    I think there is something wrong in proces of encoding and decoding binary data, as I said before it works with very short 4 - letters string but fails with longer.
    It sounds bad since I need to convert 300 lines of pure text this way ;/

    So, could you please look closely on this problem ?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    #define BASE64CHARSET   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"\
                            "abcdefghijklmnopqrstuvwxyz"\
                            "0123456789"\
                            "+/?!"\
                            "";
    Creating a macro with a ";" semicolon at the end is almost always a mistake.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Here's what I suggest: write a comprehensive suite of unit tests for each of the 4 functions. Test with small input, large input, input with all sorts of different characters. This will either show you that they don't actually work all that great, or give you confidence that the problem probably lies elsewhere.
    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

  8. #8
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    If they are separated they works good, both RC4 encode and decode as they should with short and long text. As I said the problem occur when I joined them together and here I did something wrong. I am not a C guru and have problem to figure it out. Could you look at this ?

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try using unsigned char instead of char. Make a typedef:
    Code:
    typedef unsigned char uchar;
    This code
    Code:
            for (ix = strlen(buf) - 1; (ix >= 0) && (buf[ix] == 10); ix--)
                buf[ix] = 0;
    removes multiple newlines from the end of buf, but fgets will only
    deposit a single newline at the most. If you mean to remove whitespace characters generally, use isspace() (prototyped in ctype.h). And when you want to indicate a newline, use '\n' instead of 10.

    And the whole "Hungarian notation" thing (e.g., pszKey) is generally frowned upon outside the Windows world.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Thanks for answer, have you tested it and those changes solved problem ?

    Quote Originally Posted by oogabooga View Post
    Try using unsigned char instead of char. Make a typedef:
    Code:
    typedef unsigned char uchar;
    Code:
    typedef unsigned char uchar;
    this code instead oof what ?

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by MartinR View Post
    Thanks for answer, have you tested it and those changes solved problem ?


    Code:
    typedef unsigned char uchar;
    this code instead oof what ?
    FYI: Bitwise operators should only be used on unsigned types in most cases.
    You tend to get weird results on signed values on some systems. Yours might be such a system.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Quote Originally Posted by stahta01 View Post
    FYI: Bitwise operators should only be used on unsigned types in most cases.
    You tend to get weird results on signed values on some systems. Yours might be such a system.

    Tim S.
    Are you trying to say that this program works good on your system ? I am on Windows anyway. Once again, this program works good only for couple of first letters in my case. After first 3/4 letters it prints something weird.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MartinR View Post
    Are you trying to say that this program works good on your system ? I am on Windows anyway. Once again, this program works good only for couple of first letters in my case. After first 3/4 letters it prints something weird.
    It doesn't even properly work on one letter, so it's not as though any of it works.

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by MartinR View Post
    Are you trying to say that this program works good on your system ? I am on Windows anyway.
    It probably wouldn't work properly on his system either because his system is likely similar to yours.

    The problem is that when you right-shift a signed integer that has the sign bit set, it is up to the implementation to "sign-extend" the result (an "arithmetic" shift) or not (a "logical" shift). It's also up to the implementation whether or not char is signed or unsigned.

    On your system, char is probably signed, so if any of your manipulations set the sign bit and this is followed by an arithmetic right-shift, the result will not be what you want (since, essentially, 1's come in from the left instead of 0's).

    However, I haven't bothered to test the unsigned char idea (because I don't feel like it) so, although that is probably a problem, it may not be the specific one you're currently hunting.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  15. #15
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Could you please try to fix this code, I am unable to do this since I lack knowledge in C programming.

    I expect to use this program from external file with aprox 300 lins to convert it to base64.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting binary data in a txt file into HEX
    By Creative.23 in forum C Programming
    Replies: 6
    Last Post: 04-17-2013, 08:37 PM
  2. converting a binary file to a text array
    By ckeays in forum C Programming
    Replies: 2
    Last Post: 03-29-2011, 03:39 PM
  3. Converting text file to binary
    By rocketman03 in forum C++ Programming
    Replies: 6
    Last Post: 10-11-2009, 12:43 PM
  4. converting text to binary
    By nhallahan in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2006, 04:55 PM
  5. Converting Text to Binary (And Back)
    By Rune Hunter in forum C++ Programming
    Replies: 21
    Last Post: 08-24-2005, 10:17 PM