gpt4 book ai didi

java - 安卓 SDK : BASE64 Encoder/Decoder not importing

转载 作者:行者123 更新时间:2023-12-01 18:45:23 24 4
gpt4 key购买 nike

所以我正在尝试实现一个从 link to code 获得的字符串加密类

当我尝试将其放入我的 android 项目时,我收到以下错误:

Description Resource Path Location Type The import sun cannot be resolved SimpleProtector.java /EncryptedSMS/src/com/nsaers/encryptedsms line 7 Java Problem BASE64Decoder cannot be resolved to a type SimpleProtector.java /EncryptedSMS/src/com/nsaers/encryptedsms line 29 Java Problem BASE64Encoder cannot be resolved to a type SimpleProtector.java /EncryptedSMS/src/com/nsaers/encryptedsms line 21 Java Problem

有什么想法吗?

这是我的类(class)代码:

package com.nsaers.encryptedsms;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class SimpleProtector {
private static final String ALGORITHM = "AES";
private static final byte[] keyValue = new byte[] { 'T', 'h', 'i', 's',
'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };

public static String encrypt(String valueToEnc) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new BASE64Encoder().encode(encValue);
return encryptedValue;
}

public static String decrypt(String encryptedValue) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}

private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
// SecretKeyFactory keyFactory =
// SecretKeyFactory.getInstance(ALGORITHM);
// key = keyFactory.generateSecret(new DESKeySpec(keyValue));
return key;
}
}

最佳答案

使用Base64 Android SDK 中的类,而不是 BASE64Encoder/BASE64Decoder。 API 可能略有不同,但它会满足您的需要。

但是,您的代码仍然不理想:任何时候您使用 String.getBytes 或采用字节数组的 String 构造函数,您都应该指定编码使用 - UTF-8 可能是最合适的。目前,您将使用系统默认编码,可能是 UTF-8(我感觉 Android 中始终如此),但会让您的代码变得非常脆弱。

关于java - 安卓 SDK : BASE64 Encoder/Decoder not importing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17952994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com