0

I am encrypting a string using the crypto libraries in C (using gcc in Linux). Here is what I would do:

 ien = encrypt ((unsigned char*)passwd, strlen(passwd), (unsigned char*)cypher_key, (unsigned char*)cypher_salt, cypher_text);

Now if I were to print out the contents of cypher_text, it would be all garbled. I need this to be saved to a text file (it will be hashed with other things so as to be obfuscated eventually). This right now is an experiment. I need to convert this raw binary data into a hexadecimal string that is readable in any text editor.

Then, I need to convert that hexadecimal string back into raw binary so I can do this and get the original text:

decrypt(cypher_text, ien, (unsigned char*)cypher_key, (unsigned char*)cypher_salt, (unsigned char*)passwd);

Can someone please point me in the right direction on how I could accomplish this?

UPDATE A suggested link about printing hex in C isn't exactly what I am after. I am taking direct unprintable data put out by the encrypt function (not hexadecimal values in an array) and trying to convert it into a string of hexadecimal numbers and be able to convert that hex string back into it's original binary form. I hope that helps clear up some of what I'm trying to do.

I'm going to look into the idea of using base64 encoding but I can't have headers, etc. in the encoding. I'll need just a straight string.

  • 1
    Typically you'd use something like [base64](https://en.wikipedia.org/wiki/Base64) to encode/decode binary data as text - it's very simple and there are no doubt open source libraries out there that you could use, in order to avoid "re-inventing the wheel". – Paul R Oct 21 '15 at 16:09
  • 2
    See http://stackoverflow.com/q/342409/1362568 – Mike Kinghan Oct 21 '15 at 17:33
  • @ArtjomB. That link they discussed something that was already in hex. What I'm trying to convert is something that is unprintable. –  Oct 22 '15 at 13:06
  • @RavenLX You're right. Turns out, it is a little hard to find a generic binary `char[]` to hex `char[]` conversion duplicate target question. – Artjom B. Oct 22 '15 at 13:14
  • Is there a way to remove the heading at the top of my question that says "This question may already have an answer here: Printing hexadecimal characters in C 7 answers"? That link doesn't answer the question. –  Oct 22 '15 at 21:40

0 Answers0