java - 如何在 Android 中修复密钥长度 256 位
问题描述
在我的应用程序中,我想使用AES、CBC下载加密文件并将此文件解密到我的应用程序中!
我在我的应用程序中编写了以下代码,但在应用程序显示此错误后logcat
:
E/newDecryptLog: 0 : Key length not 128/192/256 bits.
我的密码是: 7BOF%aZQMpfJ#2wUS*S6!@K+ZB$Sz+J0
我的代码是:
public class EncryptDecryptUtils {
public static EncryptDecryptUtils instance = null;
private static PrefUtils prefUtils;
public static EncryptDecryptUtils getInstance(Context context) {
if (null == instance)
instance = new EncryptDecryptUtils();
if (null == prefUtils)
prefUtils = PrefUtils.getInstance(context);
return instance;
}
public static byte[] encode(SecretKey yourKey, byte[] fileData)
throws Exception {
byte[] data = yourKey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(data, 0, data.length, KEY_SPEC_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
return cipher.doFinal(fileData);
}
public static byte[] decode(SecretKey yourKey, byte[] fileData)
throws Exception {
byte[] decrypted;
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, PROVIDER);
cipher.init(Cipher.DECRYPT_MODE, yourKey, new IvParameterSpec(new byte[cipher.getBlockSize()]));
decrypted = cipher.doFinal(fileData);
return decrypted;
}
public void saveSecretKey(SecretKey secretKey) {
String encodedKey = Base64.encodeToString(secretKey.getEncoded(), Base64.NO_WRAP);
prefUtils.saveSecretKey(encodedKey);
}
public SecretKey getSecretKey() {
String encodedKey = "7BOF%aZQMpfJ#2wUS*S6!@K+ZB$Sz+J0";
if (null == encodedKey || encodedKey.isEmpty()) {
SecureRandom secureRandom = new SecureRandom();
KeyGenerator keyGenerator = null;
try {
keyGenerator = KeyGenerator.getInstance(KEY_SPEC_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
keyGenerator.init(OUTPUT_KEY_LENGTH, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
saveSecretKey(secretKey);
return secretKey;
}
byte[] decodedKey = Base64.decode(encodedKey, Base64.NO_WRAP);
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, KEY_SPEC_ALGORITHM);
return originalKey;
}
}
我在上面的类中使用了这个代码:
@Nullable
public static byte[] decryptFile(Context context, String fileName) {
try {
byte[] fileData = FileUtils.readFile(FileUtils.getFilePath(context, fileName));
byte[] decryptedBytes = EncryptDecryptUtils.decode(EncryptDecryptUtils.getInstance(context).getSecretKey(), fileData);
return decryptedBytes;
} catch (Exception e) {
Log.e("newDecryptLog", "0 : " + e.getMessage());
}
return null;
}
但是当使用这个 run catch 方法并告诉我上面的错误时!
我该如何解决?
解决方案
您可以缩小您的 getSecretKey 方法,因为您的 if 子句永远不会遇到“错误”。您的 encodedKey 不是 Base64 字符串,而是直接输入,可以用作键:
因为我在 Desktop-Java 上,所以我不知道 StandardCharsets 在 Android 中是否可用。
public SecretKey getSecretKey() {
String encodedKey = "7BOF%aZQMpfJ#2wUS*S6!@K+ZB$Sz+J0";
return new SecretKeySpec(encodedKey.getBytes(StandardCharsets.UTF_8), KEY_SPEC_ALGORITHM);
}
推荐阅读
- git - 如何在 git 标签上使用过滤器并在最后或中间使用过滤器?
- c# - 如何在 UWP 中使用 Windows.UI.Xaml.Controls.Symbol 作为自定义指针
- reactjs - 使用 React 从本地存储中获取图像
- html - 如何在 html css 上制作响应式垂直视频?
- python - Pandas 从许多数据帧中查找并返回相应的值
- javascript - 使用 Vue 为电子邮件提交带有内联样式的 HTML
- python - 如何自定义 mask r-cnn 的 display_instances() 函数的输出
- javascript - 反应引导手风琴加减指标问题
- python - 如何在 django 中覆盖管理列名“is_staff”和“is_superuser”?
- docker - Kubernetes 无法运行本地构建的 docker 镜像