c# - emulating PBEWITHMD5ANDDES encryption under .net from JAVA -


i trying emulate same behaviour of java in c#. not getting desired result. key generated both different.

java code

        public static string generatedecryptedkey(string secretkey, string authkey)     {         string strdecryptedkey = "";         byte[] salt = { (byte)0x09, (byte)0xd5, (byte)0xa1, (byte)0xa6, (byte)0xa3, (byte)0xa7, (byte)0xa9, (byte)0xa0 };         int iterationcount = 10;         keyspec keyspec = new pbekeyspec(secretkey.tochararray(), salt, iterationcount);         secretkey key = secretkeyfactory.getinstance("pbewithmd5anddes").generatesecret(keyspec);         algorithmparameterspec paramspec = new pbeparameterspec(salt, iterationcount);         dcipher = cipher.getinstance(key.getalgorithm());         dcipher.init(cipher.decrypt_mode, key, paramspec);         byte[] enc = base64.decodebase64(authkey.getbytes());         byte[] utf8 = dcipher.dofinal(enc);         strdecryptedkey = new string(utf8, "utf-8");         return strdecryptedkey;     } 

c# code

        public static string generatedecryptedkey(string secretkey,string authkey)     {         string strdecryptedkey = string.empty;         byte[] salt = { (byte)0x09, (byte)0xd5, (byte)0xa1, (byte)0xa6, (byte)0xa3, (byte)0xa7, (byte)0xa9, (byte)0xa0 };         int iterationcount = 10;         pkcskeygenerator kp = new pkcskeygenerator();         icryptotransform crypt = kp.generate(secretkey, salt, iterationcount, 1);            var bytes = encoding.utf8.getbytes(authkey);              byte[] resultbytes = crypt.transformfinalblock(bytes, 0, bytes.length);         strdecryptedkey = convert.tobase64string(resultbytes);           return strdecryptedkey;     } 

result generated both function upon same input coming wrong. new cryptography, please explain doing wrong. below link class written bobjanova using in c# conversion.

https://www.codeproject.com/articles/16450/emulating-pbewithmd-anddes-encryption-under-net

note: don't want reveal salt value had changed value. hope understand.

you should use

var bytes = convert.frombase64string(authkey); 

instead of

var bytes = encoding.utf8.getbytes(authkey); 

and

strdecryptedkey = encoding.utf8.getstring(resultbytes);   

instead of

strdecryptedkey = convert.tobase64string(resultbytes); 

in c#.

also, authkey.getbytes() in java might lead problems down line. specify encoding authkey.getbytes("utf-8").


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -