0

i want to decrypt my encrpyted String in Java on Android.

With the following code i encrypted my raw String in C#:

        public static string Encrypt(string decryptedString)
    {
        DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
        desProvider.Mode = CipherMode.ECB;
        desProvider.Padding = PaddingMode.PKCS7;
        desProvider.Key = Encoding.ASCII.GetBytes("password");
        using (MemoryStream stream = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(stream, desProvider.CreateEncryptor(), CryptoStreamMode.Write))
            {
                byte[] data = Encoding.Default.GetBytes(decryptedString);
                cs.Write(data, 0, data.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(stream.ToArray());
            }
        }
    }

I tried to decrypt it in Java:

   public String decrypt(){
       String keyStr = "password";
       String msg = "KGFL1GG5VLQ=";
       String erg = "";
       try{


       KeySpec ks = new DESKeySpec(keyStr.getBytes("UTF-8"));
       SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(ks);
       IvParameterSpec iv = new IvParameterSpec(Hex.decodeHex("1234567890ABCDEF".toCharArray()));
       Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
       cipher.init(Cipher.DECRYPT_MODE, key, iv);

       byte[] decoded = cipher.doFinal(Base64.decodeBase64(msg));
       erg = new String(decoded);
       } catch (Exception e){
           erg = "error";
       }
       return erg;

   }

I use the commons-codec-1.8.jar. It crashes with the following Error:

W/dalvikvm(17859): VFY: unable to resolve static method 339: Lorg/apache/commons/codec/binary/Base64;.decodeBase64 (Ljava/lang/String;)[B

Please point out where I'm going wrong.

ErBeEn
  • 175
  • 2
  • 11
  • 1
    You'll have to be more precise than that. What's not working? It's not decrypting correctly, an exception is raised, etc? – Msonic Sep 09 '13 at 14:13
  • I think this is similar to: http://stackoverflow.com/questions/5147789/nosuchmethoderror-using-commonc-codec-in-android-application – Sandokas Sep 09 '13 at 15:24
  • Also, there are a few problems in your code, for example, you set an encryption key on your .NET code, but you don't in you Android code, also, you set an IV in your Android code that you don't in your C# code. – Rafael Sep 09 '13 at 17:51
  • If you're new to encryption, I recommend you to better use Bouncy Castle on both your C# and Android applications, that way you'll have a common ground for both. – Rafael Sep 09 '13 at 17:52

1 Answers1

0

I think this question is similar to:

NoSuchMethodError using commonc codec in Android application

and

Apache Commons Codec with Android: could not find method

Answer is, Android uses version 1.2 of the apache commons.

Community
  • 1
  • 1
Sandokas
  • 325
  • 2
  • 8