开发者

How to decrypt string (encrypted in .NET) in Java client using apache.commons.codec Base64?

开发者 https://www.devze.com 2023-03-17 02:17 出处:网络
I have a tricky situation here. I have a web service written in C# and it returns JSON stuff encrypted with the methods described below. This web service is consumed by a Java client, but I\'m not abl

I have a tricky situation here. I have a web service written in C# and it returns JSON stuff encrypted with the methods described below. This web service is consumed by a Java client, but I'm not able to get my string decrypted in client side, I've tried several Java examples of Java functions using DES algorithm but no luck since I always got this error:

java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.decodeBase64

(My environment is Eclipse, jdk1.6.0_26, Android 2.1update emulator)

Please help!!

My C# functions are:

/// <summary>
/// 
/// </summary>
/// <param name="stringToEncrypt"></param>
/// <returns></returns>
public static string Encrypt(string stringToEncrypt)
{
        try
        {
                const string key1 = "MY KEY HERE";

                byte[] iv = { Convert.ToByte("1"), Convert.ToByte("4"), Convert.ToByte("6"), Convert.ToByte("8"), Convert.ToByte("0"), Convert.ToByte("2"), Convert.ToByte("4"), Convert.ToByte("8") };

                byte[] key = Encoding.UTF8.GetBytes(key1);

                var des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
                var ms = new MemoryStream();
                var cs = new CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
        }
        catch (Exception ex)
        {
                Logger.Write(new ExceptionLogEntry(ex.ToString()));
        }

        return null;
}

/// <summary>
/// 
/// </summary>
/// <param name="stringToDecrypt"></param>
/// <returns></returns>
public static string Decrypt(string stringToDecrypt)
{
        try
        {
                const string key1 = "MY KEY HERE";

                byte[] iv = { Convert.ToByte("1"), Convert.ToByte("4"), Convert.ToByte("6"), Convert.ToByte("8"), Convert.ToByte("0"), Convert.ToByte("2"), Convert.ToByte("4"), Convert.ToByte("8") };

                byte[] key = Encoding.UTF8.GetBytes(key1);

                var des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Convert.FromBase64String(stringToDecrypt);
                var ms = new MemoryStream();
                var cs = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                var encoding = Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
        }
        catch(Exception ex)
        {
                Logger.Write(new ExceptionLogEntry(ex.ToString()));
                return "-1";
        }
}

My Java function using commons-codec-1.5.jar is:

KeySpec ks = new DESKeySpec("MY KEY HERE".getBytes("UTF-8"));
SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(ks);
IvParameterSpec iv = new IvParamete开发者_运维技巧rSpec(Hex.decodeHex("1234567890ABCDEF".toCharArray()));
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);

byte[] decoded = cipher.doFinal(Base64.decodeBase64("MY ENCRYPTED STRING HERE"));

Log.e(TAG, "DECODED->" + new String(decoded, "UTF-8"));

But I always got this error:

07-05 04:48:20.822: ERROR/AndroidRuntime(1270): java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.decodeBase64


This error:

java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.decodeBase64

shows that the problem isn't (necessarily) in your code at all - it can't find Base64.decodeBase64, so you're probably not deploying the Apache Commons Codec library properly. (Admittedly it's not exactly the error I'd have expected to see, but I'm more used to desktop Java than Android.)

However, you might as well use Android's built-in Base64 class instead of working out why you couldn't get Apache Commons Codec to work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号