问题
I am new to Java. I followed this tutorial about Encryption and Decryption using 3DES algorithm.
I have implemented like this:
- Created a class and placed the 3DES code provided in the above link.
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....
回答1:
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());
回答2:
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");
来源:https://stackoverflow.com/questions/16335223/nosuchproviderexception-when-encrypting-string-with-3des