c# 2.0 - 3DES - Decrypt encrypted text (by JAVA) in C# -


here situation:

  1. the encrypted text done in java (which have no java background @ all)
  2. the method 3des
  3. the padded pkcs#5
  4. base 64

the decryption in c#, , here code:

    public static string decryptstring(string message, string passphrase)     {         byte[] results;         utf8encoding utf8 = new utf8encoding();          md5cryptoserviceprovider hashprovider = new md5cryptoserviceprovider();         byte[] tdeskey = hashprovider.computehash(utf8.getbytes(passphrase));          tripledescryptoserviceprovider tdesalgorithm = new tripledescryptoserviceprovider();          tdesalgorithm.key = tdeskey;         tdesalgorithm.mode = ciphermode.ecb;         tdesalgorithm.padding = paddingmode.pkcs7;          byte[] datatodecrypt = convert.frombase64string(message);          try         {             icryptotransform decryptor = tdesalgorithm.createdecryptor();             results = decryptor.transformfinalblock(datatodecrypt, 0, datatodecrypt.length);         }                 {             tdesalgorithm.clear();             hashprovider.clear();         }          return utf8.getstring(results);     } 

however, when tried decrypt, got error message: bad data

where missing here?

thanks in advance.

added, , here's how encryption works:

<cffunction name="gettoken" returntype="string" output="false">     <cfscript>         plaintext = getplaintext();         rawsecretkey = createobject("java","sun.misc.base64decoder").decodebuffer(variables.encryptionkey);          secretkeyspec = createobject("java","javax.crypto.spec.secretkeyspec").init(rawsecretkey,"desede");          cipher = createobject("java","javax.crypto.cipher").getinstance("desede");         cipher.init(cipher.encrypt_mode, secretkeyspec);          encrypted = cipher.dofinal(plaintext.getbytes()); // byte array (a binary in cf)          return urlencodedformat(tostring(tobase64(encrypted)));     </cfscript> </cffunction> 

update: issue has been resolved. problem key needed converted base64.

the answer:

instead of:

byte[] tdeskey = hashprovider.computehash(utf8.getbytes(passphrase)); 

do this:

byte[] tdeskey = convert.frombase64string(passphrase); 

that solves issue.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -