我想在 Android 上用 Java 解密我的加密字符串。
使用以下代码,我在 C# 中加密了我的原始字符串:
public static string Encrypt(string decryptedString)
{
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
desProvider.Mode = CipherMode.ECB;
desProvider.Padding = PaddingMode.PKCS7;
desProvider.Key = Encoding.ASCII.GetBytes("password");
using (MemoryStream stream = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(stream, desProvider.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] data = Encoding.Default.GetBytes(decryptedString);
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(stream.ToArray());
}
}
}
我试图用Java解密它:
public String decrypt(){
String keyStr = "password";
String msg = "KGFL1GG5VLQ=";
String erg = "";
try{
KeySpec ks = new DESKeySpec(keyStr.getBytes("UTF-8"));
SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(ks);
IvParameterSpec iv = new IvParameterSpec(Hex.decodeHex("1234567890ABCDEF".toCharArray()));
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] decoded = cipher.doFinal(Base64.decodeBase64(msg));
erg = new String(decoded);
} catch (Exception e){
erg = "error";
}
return erg;
}
我使用 commons-codec-1.8.jar。它崩溃并出现以下错误:
W/dalvikvm(17859): VFY: unable to resolve static method 339: Lorg/apache/commons/codec/binary/Base64;.decodeBase64 (Ljava/lang/String;)[B
请指出我哪里出错了。