0

I am new to Java. I followed this tutorial about Encryption and Decryption using 3DES algorithm.

I have implemented like this:

  1. Created a class and placed the 3DES code provided in the above link.
  2. Called the encrypt method in the above link as below:

    String encryptedPassword = Encrypter.encrypt(edtText.getText().toString()); 
    

I am getting the exception in logcat as below:

 05-02 15:19:10.804: W/System.err(4445): java.security.NoSuchProviderException: Provider not available: SunJCE
    05-02 15:19:10.820: W/System.err(4445):     at javax.crypto.Cipher.getInstance(Cipher.java:209)
    05-02 15:19:10.820: W/System.err(4445):     at com.example.utilities.Encrypter.encrypt(Encrypter.java:46)
    05-02 15:19:10.820: W/System.err(4445):     at com.example.screens.RegisterScreen.onClick(RegisterScreen.java:152)
    05-02 15:19:10.820: W/System.err(4445):     at android.view.View.performClick(View.java:2485)
    05-02 15:19:10.820: W/System.err(4445):     at android.view.View$PerformClick.run(View.java:9080)
    05-02 15:19:10.828: W/System.err(4445):     at android.os.Handler.handleCallback(Handler.java:587)
    05-02 15:19:10.828: W/System.err(4445):     at android.os.Handler.dispatchMessage(Handler.java:92)
    05-02 15:19:10.828: W/System.err(4445):     at android.os.Looper.loop(Looper.java:130)
    05-02 15:19:10.828: W/System.err(4445):     at android.app.ActivityThread.main(ActivityThread.java:3687)
    05-02 15:19:10.835: W/System.err(4445):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-02 15:19:10.835: W/System.err(4445):     at java.lang.reflect.Method.invoke(Method.java:507)
    05-02 15:19:10.835: W/System.err(4445):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    05-02 15:19:10.835: W/System.err(4445):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    05-02 15:19:10.835: W/System.err(4445):     at dalvik.system.NativeStart.main(Native Method)

Please help me. How to solve this....

Avadhani Y
  • 7,476
  • 18
  • 59
  • 90
user2326860
  • 1
  • 1
  • 4
  • 12

2 Answers2

0

Use this code to encrypt your string

    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;

    import android.util.Base64;
    //string encryption
    public class EncryptionHelper {



        // Encrypts string and encode in Base64
        public static String encryptText(String plainText) throws Exception {
            // ---- Use specified 3DES key and IV from other source --------------
            byte[] plaintext = plainText.getBytes();//input
            byte[] tdesKeyData = Constants.getKey().getBytes();// your encryption key

            byte[] myIV = Constants.getInitializationVector().getBytes();// initialization vector

            Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DESede");
            IvParameterSpec ivspec = new IvParameterSpec(myIV);

            c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
            byte[] cipherText = c3des.doFinal(plaintext);
            String encryptedString = Base64.encodeToString(cipherText,
                    Base64.DEFAULT);
            // return Base64Coder.encodeString(new String(cipherText));
            return encryptedString;
        }

    private class Constants 
{
private static final String KEY="QsdPasd45FaSdnLjf";
    private static final String INITIALIZATION_VECTOR="l9yhTaWY";
public static String getKey() 
    {
        return KEY;
    }


    public static String getInitializationVector() 
    {
        return INITIALIZATION_VECTOR;
    }
 }   
    }

This is how you can encrypt the string

String encryptedPassword = EncryptionHelper.encryptText(edtText.getText().toString());
onkar
  • 4,397
  • 10
  • 47
  • 85
  • I am using eclipse... displaying redline under `Constants`, what is the import statement for that? – user2326860 May 02 '13 at 11:00
  • I have a class file called constants where I had key and IV stored. – onkar May 02 '13 at 11:02
  • `Constants.getKey().getBytes();` replace this with your key – onkar May 02 '13 at 11:03
  • What is the `key` value i need to provide? – user2326860 May 02 '13 at 11:04
  • `Constants.getInitializationVector().getBytes();` replace with your initialization vector – onkar May 02 '13 at 11:04
  • For 3DES u have to use a key and IV. It is just a string, like your password which must have secured access. – onkar May 02 '13 at 11:06
  • i cant understand what you r saying... What are the values i need to provide for Key and myIV.....sorry. i am newbie to java.... – user2326860 May 02 '13 at 11:06
  • I have provided another question here: http://stackoverflow.com/questions/16336017/android-how-to-use-3des-algorithm. On our server side, the implementation is done as shown in this link. Can u please tell me what the values i need to provide for Key and IV and those should match Key value on the server side as well. – user2326860 May 02 '13 at 11:09
  • If the key and IV used are equal, then this code will surely encrypt and decrypt your data. – onkar May 02 '13 at 11:11
  • got exception at `java.security.InvalidAlgorithmParameterException: IV must be 8 bytes long`. – user2326860 May 02 '13 at 11:17
  • ` private static final String INITIALIZATION_VECTOR="l9yhTaWY";` – onkar May 02 '13 at 11:21
  • getting different encrypted password. On the server side password encrypted as `c3VyeWF5dg==` and using ur code i got the encrypted password as `kPLFxOk0oP4=`. So both are not matching. How can i get the same values.... I have provided server side code here: http://stackoverflow.com/questions/16336017/android-how-to-use-3des-algorithm Can u please check and correct me what i need to provide `key` and `IV` values in the above code.... – user2326860 May 02 '13 at 11:27
  • You have to use same key and init vector, at server side and on the fone. Please share serverside code is not having key & IV so I cannot help you any further. – onkar May 02 '13 at 13:36
  • I agree with @GregS. The stacktrace in the question makes it clear what the problem is. Offering a chunk of alternative code is not a good solution. – Duncan Jones May 03 '13 at 06:44
0

Sorry, I was being lazy. The line

Cipher ecipher = Cipher.getInstance("DESede/CBC/PKCS5Padding","SunJCE");

shows that you are specifying a particular provider. Normally you would want a very good reason for doing this, for example you might be required to use a FIPS-compliant provider. The SunJCE provider does not exist on Android. Just use the default provider, which you get simply by leaving out that argument. So try:

Cipher ecipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");

Similarly, change

Cipher dcipher = Cipher.getInstance("DESede/CBC/PKCS5Padding","SunJCE");

to

Cipher dcipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
President James K. Polk
  • 38,341
  • 16
  • 90
  • 119