encryption - Java TripleDES PKCS5Padding decryption to C# - bad data / padding error -


i'm trying write c# equivalent following java code:

protected static final string des_ecb_pkcs5padding = "desede/ecb/pkcs5padding";   public static string decryptvaluedirect(string value, string key)             throws nosuchalgorithmexception, nosuchpaddingexception,             generalsecurityexception, illegalblocksizeexception,             badpaddingexception {         byte[] bytes = base64.decodebase64(value);         cipher cipher = cipher.getinstance(des_ecb_pkcs5padding);         cipher.init(cipher.decrypt_mode, convertsecretkey(key.getbytes()));         byte[] decryptedvalue = cipher.dofinal(bytes);          string nstr =  new string(decryptedvalue);         return nstr;     } protected static secretkey convertsecretkey(byte[] encryptionkey) throws generalsecurityexception {         if (encryptionkey == null || encryptionkey.length == 0)             throw new illegalargumentexception("encryption key must specified");          secretkeyfactory keyfactory = secretkeyfactory.getinstance(tripledes);         keyspec keyspec = new desedekeyspec(encryptionkey);         return keyfactory.generatesecret(keyspec);     } 

the source text base64 encoded, encrypted , base64 encoded transport on rabbit queue. our vendor handles encryption provided above decryption in java, has no idea c#.

the input on encryption side key, random string. use same string encryption/decryption 012345678901234567890123456789 in our dev env. input, no salt, hashing (that see) or pw iterations. requirement is @ least 24 chars long.

my c# code below , fiddle of attempt here.

using system; using system.io; using system.security.cryptography; using system.text;  public class program {     public static void main()     {         //message data value         //we using encrypted multibyte.              string mydata = @"roe8oyev7b6fausvfix0xe55vss9ir5dlwgrbsm+lmkmlcajsa13vudwwlaeytlud8+nmxshky0grsxsk0z9cqe5v45xnaifuhnyzi9a0jtmfc8xniz5dbclpo/v73qnieizdkbnv5cpo3bm+l79ai96kb/gkf3xuerfpxvwejtpywboyo+ffnyfps4gayditsyiaeh39vp4eipmq5zc18ba39lajq3uavewsxz7h+x3ooe2szjt/tqwrkiojsefwexqzkhiloq0mocivd9xtwplynsl3lmwyf6h8f0py4fc57lvghvuz7dsb9nwuanmg3uqbsonnfvhuxyvjtwnyfohwfzomx6xdljjfhgzhahg2vrescfnputonqy08rgojbngyjnrqk8uravi3bqkq8y7f/9hmetmiiqe6kuutmu=";         string mykey = "012345678901234567890123456789";//development env key.         console.writeline("decrypt1:");         string s = decrypt1(mydata, mykey);         console.readline();     }      public static string decrypt1(string value, string decryptionkey)     {         string decryptstring = "";         tripledescryptoserviceprovider tdesalg = new tripledescryptoserviceprovider();         md5cryptoserviceprovider hashmd5provider = new md5cryptoserviceprovider();         try         {             byte[] decodeddata = convert.frombase64string(value);             tdesalg.mode = ciphermode.ecb;             tdesalg.padding = paddingmode.pkcs7;//according ms, same pkcs5padding              byte[] key = hashmd5provider.computehash(encoding.utf8.getbytes(decryptionkey));             //byte[] iv = tdesalg.iv;             byte[] iv = new byte[tdesalg.blocksize / 8]; //the size of iv property must same blocksize property divided 8              var memorystream = new memorystream(decodeddata);             var cryptostream = new cryptostream(memorystream, tdesalg.createdecryptor(key, iv), cryptostreammode.read);             var reader = new streamreader(cryptostream);             decryptstring = reader.readtoend();             byte[] decryptdata = convert.frombase64string(decryptstring);         }         catch (exception e)         {             console.writeline("a cryptographic error occurred: {0}", e.message + e.stacktrace);             return null;         }          return decryptstring;     }  } 

searching seems point same answer, key, encoding, ... must same. don't know equivalent java source provided. :) suggestions helpful.

md5 has 16-byte output, triple des (3des) requires 24-byte key. there key size mis-match.

the c# , java key derivations substantially different:

c#:
byte[] key = hashmd5provider.computehash(encoding.utf8.getbytes(decryptionkey));
returns 16-bytes.

java:
secretkeyfactory.getinstance(tripledes)
returns 24-bytes.

there key option (2tdea) 16-byte key used , first 8-bytes duplicated create last 8-bytes. nist has deprecated option.

some implementations accept 16-byte key , extend key 24-bytes , some not. should provide 24-bytes 3des, not rely on implementation create 24-byte key.

note: question updated not clear the actual encrytpion key derived.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - VueJS2 and the Window Object - how to use? -