【问题标题】:NoSuchProviderException when encrypting string with 3DES使用 3DES 加密字符串时出现 NoSuchProviderException
【发布时间】:2013-05-29 17:38:47
【问题描述】:

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

我是这样实现的:

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

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

我在 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)

请帮助我。如何解决这个......

【问题讨论】:

  • 您在代码中的某处指定了 SunJCE 提供程序。即使在 Oracle 的 java 运行时这样的地方,这也没有任何意义。当然,Android 上不存在该提供程序。除非您有充分的理由不这样做,否则请使用默认提供程序。
  • @GregS 你想发布答案吗?
  • @GregS 你能发布一个解决方案吗?
  • @DuncanJones 你能帮帮我吗....
  • @user2326860 更改您的代码并删除它明确声明"SunJCE" 的任何地方。例如,将("DESede/CBC/PKCS5Padding","SunJCE"); 更改为("DESede/CBC/PKCS5Padding");

标签: java android encryption 3des


【解决方案1】:

使用此代码加密您的字符串

    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());

【讨论】:

  • 我正在使用 eclipse... 在Constants 下显示红线,对此的导入语句是什么?
  • 我有一个名为 constants 的类文件,其中存储了密钥和 IV。
  • Constants.getKey().getBytes(); 用您的密钥替换它
  • 我需要提供什么key 值?
  • Constants.getInitializationVector().getBytes(); 替换为你的初始化向量
【解决方案2】:

对不起,我偷懒了。线

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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2012-04-09
    • 2015-08-03
    相关资源
    最近更新 更多