c# - Encrypting string to 15 characters -


i doing serial key thing format xxxxx-xxxxx-xxxxx (15 characters). can encrypt string problems are:

  1. 6 characters become 20+ characters.
  2. has underieable characters such as: +, /, =

so best way encrypt string number of characters? in case: 6 characters encrypted 15 characters.

i using this:

    public string encryptstring(string cleartext, string password)     {         byte[] clearbytes = system.text.encoding.unicode.getbytes(cleartext);         rfc2898derivebytes pdb = new rfc2898derivebytes(password, bytegetter(usagemode, "password"));          byte[] encrypteddata = encryptbyte(clearbytes, pdb.getbytes(32), pdb.getbytes(16));         return convert.tobase64string(encrypteddata);     }       public static byte[] encryptbyte(byte[] cleardata, byte[] key, byte[] iv)     {         memorystream ms = new memorystream();         rijndael alg = rijndael.create();         alg.key = key;         alg.iv = iv;         cryptostream cs = new cryptostream(ms, alg.createencryptor(), cryptostreammode.write);         cs.write(cleardata, 0, cleardata.length);         cs.close();         byte[] encrypteddata = ms.toarray();         return encrypteddata;     } 

synthetising comment thread question:

if want serial code (reversibly) encrypted data, can't constrain size well. you're stuck whatever encryption algorithm gives you, , operate on fixed-size blocks.

the obvious choices encoding encrypted data bunch of ascii characters are:

  • base64 - supported, can't implemented using alphanumerics, , case-sensitive
  • base32 - obscure - you'll have write / find implementation. rfc-specified standard requires padding =s. (just base64.) more compact base16, , needs case-insensitive alphanumerics data, advantage if expect people type code hand.
  • base16 - supported, never requires padding, far less compact other two.

you can work around padding issue knowing length of encoded string has multiple of 8 base32, , multiple of 4 base64. (i'm not 100% sure this, makes sense, since want encoded string represent multiple of 8 bits.) knowing can add padding characters make decoder stop complaining invalid input length. (convert.frombase64string() complain.)

if want constrain output size given input size in specific way, , aren't willing discard data (i.e. hash instead of encrypting), you'll have roll own encryption algorithm. seems counterproductive.


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -