0

I am creating a license software key and currently I get a byte array after encryption process. How do I convert this byte array (may be of size 2000) to a product key that looks like xxxxx - xxxxx - xxxxx - xxxxx - xxxxx where x can be a value that ranges from upper case A-Z and 0-9?

I am trying this out to generate a license key in c# console app and to validate the generated license.

var bytes = memoryStream.ToArray();
//memoryStream is a cryptostream and the size of the byte array can be around 2000

I expect the byte array to be converted into a nicely formatted key that looks like the format I mentioned above. I have been stuck with this for past 2 weeks. Any help is much appreciated.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Jabez
  • 795
  • 9
  • 18
  • Possible duplicate of [How to mask string?](https://stackoverflow.com/questions/9705955/how-to-mask-string) – collenbrecht Jan 30 '19 at 08:59
  • @collenbrecht, that is one workaround I am possibly thinking of to use but it requires additional steps while validating a key. – Jabez Jan 30 '19 at 11:28

2 Answers2

0

Try using Convert.ToBase64String(byte[]). This will give you a string with A-Z, a-z, 0-9. Then you can manually add - using string.Substring method.

Waldemar
  • 5,363
  • 3
  • 17
  • 28
  • I tried this but it contains other characters other that what I need. Also, the resulting string is more than the expected length of 34. – Jabez Jan 30 '19 at 11:25
  • @Jabez, you can encode 1 char (A-Z,0-9) with 1 byte. So you can not wrap 2000 bytes into 25-char string. If you want a short string you can get a hash function from your byte array – Waldemar Jan 30 '19 at 11:30
0

I suggest you to use Guid.

Guid.NewGuid();

Documentation about Guid

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
  • Guid works good, but there are other crypto techniques used in my code that resulted in a byte array of size more than 2000 and hence the problem to convert that into a string of length 34 as mentioned in the question. – Jabez Jan 30 '19 at 11:26
  • Create an Hash with sha256 – Marco Salerno Jan 30 '19 at 11:28