Sure, here’s the translation: 如何在安卓中修复密钥长度为256位。

huangapple go评论42阅读模式
英文:

How to fix Key length 256 bits in Android

问题

在我的应用程序中,我想使用AES和CBC下载加密文件,然后在我的应用程序中解密该文件!
我在我的应用程序中编写了以下代码,但在应用程序中显示以下错误:

E/newDecryptLog: 0:密钥长度不是128/192/256位。

我的密码是: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;
}

但是当使用此方法时,会捕获异常并显示上面的错误!如何修复它?

英文:

In my application i want download encrypted file with AES , CBC and decrypt this file into my app!<br>
I write below codes in my application but after application show me this error in logcat :

E/newDecryptLog: 0 : Key length not 128/192/256 bits.

My password is : 7BOF%aZQMpfJ#2wUS*S6!@K+ZB$Sz+J0

My codes is :

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 = &quot;7BOF%aZQMpfJ#2wUS*S6!@K+ZB$Sz+J0&quot;;
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;
}
}

I used with this codes for above class :

@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(&quot;newDecryptLog&quot;, &quot;0 : &quot; + e.getMessage());
}
return null;
}

But when use this run catch method and show me above error!

How can i fix it?

答案1

得分: 1

你可以将你的getSecretKey方法缩小,因为你的if条件永远不会进入"false"。你的encodedKey不是Base64字符串,而是直接的输入,可以用作密钥:

由于我在桌面Java上,不知道Android是否可用StandardCharsets。

public SecretKey getSecretKey() {
    String encodedKey = "7BOF%aZQMpfJ#2wUS*S6!@K+ZB$Sz+J0";
    return new SecretKeySpec(encodedKey.getBytes(StandardCharsets.UTF_8), KEY_SPEC_ALGORITHM);
}
英文:

You can shrink your getSecretKey-method as your if-clause would never run into "false". Your encodedKey is NOT
a Base64-string but the direct input and can be used as key:

As I'm on Desktop-Java I don't know if StandardCharsets are available in Android.

public SecretKey getSecretKey() {
String encodedKey = &quot;7BOF%aZQMpfJ#2wUS*S6!@K+ZB$Sz+J0&quot;;
return new SecretKeySpec(encodedKey.getBytes(StandardCharsets.UTF_8), KEY_SPEC_ALGORITHM);
}

huangapple
  • 本文由 发表于 2020年8月2日 23:46:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63218008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定