0

I'm working on a licensing system for my application. I'd like to put all licensing information (licensee name, expiration date, and enabled features) into an object, encrypt that object with a private key, then represent the encrypted data as a single text string which I can send via email to my customers.

I've managed to get the encrypted data into a byte stream, but I don't know how to convert that byte stream into a text value -- something that contains no control characters or whitespace. Can anyone offer advice on how to do that? I've been researching the Encoding class, but I can't find a text-only encoding.

I'm using Net 2.0 -- mostly VB, but I can do C# also.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Todd
  • 339
  • 1
  • 3
  • 11
  • can you just use the extension method .ToString() ? – Evan Larsen Mar 31 '11 at 20:30
  • 1
    And you didn't find it necessary to show the code you have so far for *encrypting the data into a byte stream*? Because before answering your question about decrypting something we should to know how this something was encrypted, shouldn't we? – Darin Dimitrov Mar 31 '11 at 20:31
  • Evan, I've found that ToString() fails to remove many characters I want eliminated -- whitespace, for instance. Besides, I neglected to mention this, but the operation must be reversiable, and I don't know how to reverse the ToString() method. – Todd Mar 31 '11 at 20:40
  • Darin, I've experimented with a couple of ways to generate the byte stream. One is directly via the encryption operation. Another is to generate a file, sign it, and compress it. I perceive there are other ways too, but they all leave me with the problem of converting the byte stream into text. If I can't make headway with the other suggestions here, I'll gladly post more details. – Todd Mar 31 '11 at 20:47
  • Any decent encryption algorithm takes byte[], not string. The ones in the System.Security.Cryptography namespace for example. – Hans Passant Mar 31 '11 at 21:02

3 Answers3

1

Use a Base64Encoder to convert it to a text string that can be decoded with a Base64Decoder. It is great for representing arbitary binary data in a text friendly manner, only upper and lower case A-Z and 0-9 digits.

Joe Zitzelberger
  • 4,238
  • 2
  • 28
  • 42
  • 1
    Sorry, had to look up the package name. Use System.Convert.toBase64Binary(byte[]) to make it text. Then use System.Convert.fromBase64Binary(String) to make it a byte[] again. – Joe Zitzelberger Mar 31 '11 at 20:46
0

BinHex is an example of one way to do that. It may not be exactly what you want -- for example, you might want to encode your data such that it's impossible to inadvertently spell words in your string, and you may or may not care about maximizing the density of information. But it's an example that may help you come up with your own encoding.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Caleb, thanks for the tip. BinHex does look like what I need. However, I tried the Base64Encoder that Joe suggested and it is working for me, so I'll stick with it. – Todd Apr 01 '11 at 16:21
0

I've found Base32 useful for license keys before. There are some C# implementations linked from this answer. My own license code is based on this implementation, which avoids ambiguous characters to make it easier to retype the keys.

Community
  • 1
  • 1
Adrian Cox
  • 6,204
  • 5
  • 41
  • 68
  • Adrian, thanks. If I were using short keys which the users could type into the application, I'd probably choose your Base32 solution. In my case, however, the license keys are going to be long, so I prefer Base64 for the shorter keys. – Todd Apr 01 '11 at 16:25