在android中如何使用Triple DES完成加密?是否有預定義的類可用?在android中加密
2
A
回答
1
這可能會或可能不會爲你工作。 玩得開心,JAL
public BoolString tryEncrypt(String inString, String key){
boolean success= true;
String err="";
String outString="Encrypted"; // BoolString.value
try {
byte[] byteKey= key.getBytes("UTF8");
if (byteKey.length != 24) {
success= false;
err= "Key is "+byteKey.length+" bytes. Key must be exactly 24 bytes in length.";
throw new Exception(err); // could also return here
}
KeySpec ks= new DESedeKeySpec(byteKey);
SecretKeyFactory skf= SecretKeyFactory.getInstance("DESede");
SecretKey sk= skf.generateSecret(ks);
Cipher cph=Cipher.getInstance("DESede");
cph.init(Cipher.ENCRYPT_MODE, sk);
byte[] byteInString= inString.getBytes("UTF8");
byte[] byteEncoded= cph.doFinal(byteInString);
outString= Base64.encodeToString(byteEncoded, Base64.DEFAULT);
}
catch (UnsupportedEncodingException e){err="Unable to convert key to byte array."; success= false;}
catch (InvalidKeyException e){err="Unable to generate KeySpec from key";success= false;}
catch (NoSuchAlgorithmException e){err="Unable to find algorithm.";success= false;}
catch (InvalidKeySpecException e){err="Invalid Key Specification";success= false;}
catch (NoSuchPaddingException e){err="No such padding";success= false;}
catch (IllegalArgumentException e){err="Illegal argument";success= false;}
catch (Exception e){err=e.getMessage();success= false;}
return new BoolString(success,err,outString);
}
// a utility class to signal success or failure, return an error message, and return a useful String value
// see Try Out in C#
public final class BoolString {
public final boolean success;
public final String err;
public final String value;
public BoolString(boolean success, String err, String value){
this.success= success;
this.err= err;
this.value= value;
}
}
1
您需要指定「DESede」作爲密碼;見DESedeKeySpec和this example。
0
有一些可以用來加密任何類型的數據更加密算法。 我更喜歡使用AES(高級加密標準),原因很多。首先,它提供了更大的密鑰大小,在創建加密應用程序之前,沒有必須識別的弱和半弱密鑰。此外,AES不容易受到其他理論攻擊,例如差分密碼分析等...
相關問題
- 1. 在android中加密解密
- 2. 在Android中加密數據
- 3. 在android中的Blowfish加密
- 4. PDF加密/解密在android中?
- 5. android中的加密
- 6. 在C#中加密加密#
- 7. 在Android中加密,使用phpseclib在PHP中解密
- 8. RSA - 在Android中加密/解密在PHP中
- 9. 在Android中加密文件,然後在PC中解密它
- 10. 在android/ios中加密,在php中解密
- 11. 如何在.net中加密文件並在android中解密
- 12. 如何在Java中加密並在Android和iOS中解密
- 13. 在Android中加密數據並使用AESCrypt在Ruby中解密
- 14. 如何在android中加密和解密密碼
- 15. Android中的RSA加密解密
- 16. Android中的AES加密解密算法
- 17. AES加密 - 在Android上存儲密碼
- 18. 加密python文件和解密在android
- 19. Android RC2加密
- 20. 的Android - 加密
- 21. Android加密
- 22. Android rc4加密
- 23. Android加密BadPaddingException
- 24. android加密
- 25. Android中的加密系統
- 26. Android中的RSA加密
- 27. Android中的SharedPreferences的加密
- 28. Android中的視頻加密
- 29. AES 128加密在Android
- 30. 在Android設備上加密
非常感謝! – cris 2011-02-05 22:55:49