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.

DiplomacyNotWar
  • 31,605
  • 6
  • 57
  • 75
Jabez
  • 666
  • 6
  • 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,065
  • 3
  • 15
  • 26
  • 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,000
  • 2
  • 10
  • 29
  • 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