使用3DES加密字符串时的NoSuchProviderException

时间:2013-05-02 10:00:42

标签: java android encryption 3des

我是Java新手。我使用3DES算法跟踪了tutorial关于加密和解密的内容。

我的实施方式如下:

  1. 创建了一个类并放置了上述链接中提供的3DES代码。
  2. 在上面的链接中调用加密方法如下:

    String encryptedPassword = Encrypter.encrypt(edtText.getText().toString()); 
    
  3. 我在logcat中获得异常,如下所示:

     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)
    

    请帮帮我。如何解决这个问题....

2 个答案:

答案 0 :(得分:0)

使用此代码加密字符串

    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;
    }
 }   
    }

这是加密字符串的方法

String encryptedPassword = EncryptionHelper.encryptText(edtText.getText().toString());

答案 1 :(得分:0)

对不起,我很懒。这条线

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

表示您正在指定特定的提供者。通常,您需要一个非常好的理由来执行此操作,例如,您可能需要使用符合FIPS的提供程序。 Android上不存在SunJCE提供程序。只需使用默认提供程序,只需省略该参数即可获得。所以试试:

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

同样,改变

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

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