We are implementing some kind of auto sign on in our application and got struck at decrypting something in C#, that was encrypted in java.
Basically, a Java app generates some kind of URL. When the users clicks on the link, I need to validate the query strings and if they match, let the user sign in.
Someone provided the java code. I need to convert the same code to C# because my app is in C#. I get errors when I implement it exactly in C#.
Here is the java decryption code :
String vParameter= "ksyR31QsRcbeJoysNOsAGBHajLKWsT00wavt9LJYGOMRC8zc_vqrNOeOlGHKJHIt3sLmFhDVw_JZKr4JT0H3Jj7_Di9bKNw99qCzMOKCXYM="; //The string that nees to be decoded.
byte[] encryptedV = Base64.decodeBase64(vParameter);
String salt = “jkjkyt4”; // the i parameter - user’s id
String password = “^hjkh673!v@!a89mz+%5rT”; // application specific
MessageDigest digester = MessageDigest.getInstance("SHA-1");
digester.update((salt + password).getBytes("UTF-8"));
byte[] key = digester.digest();
SecretKeySpec secretKey = new SecretKeySpec(key, 2, 16, “AES”);
String appIV = "SampleIV"// application specific
IvParameterSpec iv= new IvParameterSpec(appIV.getBytes(“UTF-8”));
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] decryptedV = cipher.doFinal(encryptedV, 0, encryptedV.length);
String v = new String(decryptedV, “UTF-8”);
Here is the corresponding C# code
string vParameter = "ksyR31QsRcbeJoysNOsAGBHajLKWsT00wavt9LJYGOMRC8zc_vqrNOeOlGHKJHIt3sLmFhDVw_JZKr4JT0H3Jj7_Di9bKNw99qCzMOKCXYM="; //v parameter
byte[] encryptedV = Encoding.UTF8.GetBytes(vParameter);
String salt = "jkjkyt4"; // the i parameter - user’s id
String password = "^hjkh673!v@!a89mz+%5rT"; // application specific
var sha1 = SHA1Managed.Create();
byte[] keyBytes = Encoding.UTF8.GetBytes(salt + password); //salt + password
byte[] key = sha1.ComputeHash(keyBytes);
byte[] finalKey = { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
String appIV = "SampleIV";
byte[] iv = Encoding.UTF8.GetBytes(appIV); //iv
Array.Copy(key, 2, finalKey, 0, 16); //key 2, 16
AesManaged tdes = new AesManaged();
tdes.Key = finalKey;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
tdes.IV = iv;
ICryptoTransform crypt = tdes.CreateDecryptor();
byte[] cipher = crypt.TransformFinalBlock(encryptedV, 0, encryptedV.Length);
string decryptedText = Convert.ToBase64String(cipher);
return decryptedText;
What am I doing wrong? Can anyone point out the mistake?
EDIT : I've update the V Parameter... Note - The keys, password and IV are not real. I had to change them since I didn't want my companies keys to be public.
EDIT 2 : Hi, I've updated the vParameter.. Now they are the same. The Java code is working... It's been implemented in another app. Now, I have to create a similar version for my C# app. Can you guys point out any issues in C# code?
Encoding.UTF8.GetBytes()try,Convert.FromBase64String()for the firstBase64.decodeBase64()anywayvParametercodes different, if the password and IV isn't? You would expect a different plain text, what use is that?