相关疑难解决方法(0)

Android 4.2破坏了我的加密/解密代码,提供的解决方案无效

首先,我已经看到 Android 4.2 在Android 4.2 和提供的解决方案破坏了我的AES加密/解密代码加密错误:

SecureRandom sr = null;
if (android.os.Build.VERSION.SDK_INT >= JELLY_BEAN_4_2) {
    sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
} else {
    sr = SecureRandom.getInstance("SHA1PRNG");
}
Run Code Online (Sandbox Code Playgroud)

对我不起作用,因为,当解码Android 4.2中Android <4.2加密的数据时,我得到:

javax.crypto.BadPaddingException: pad block corrupted
at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:709)
Run Code Online (Sandbox Code Playgroud)

我的代码很简单,直到Android 4.2才开始工作:

public static byte[] encrypt(byte[] data, String seed) throws Exception {

    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    SecureRandom secrand = SecureRandom.getInstance("SHA1PRNG");
    secrand.setSeed(seed.getBytes());
    keygen.init(128, secrand);

    SecretKey seckey = keygen.generateKey();
    byte[] rawKey = seckey.getEncoded();

    SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
    Cipher cipher = …
Run Code Online (Sandbox Code Playgroud)

android cryptography bouncycastle

34
推荐指数
4
解决办法
3万
查看次数

Android 4.2上的加密错误

以下代码适用于除最新4.2之外的所有Android版本

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Util class to perform encryption/decryption over strings. <br/>
 */
public final class UtilsEncryption
{
    /** The logging TAG */
    private static final String TAG = UtilsEncryption.class.getName();

    /** */
    private static final String KEY = "some_encryption_key";

    /**
     * Avoid instantiation. <br/>
     */
    private UtilsEncryption()
    {
    }

    /** The HEX characters */
    private final static String HEX = "0123456789ABCDEF"; …
Run Code Online (Sandbox Code Playgroud)

encryption android unit-testing

30
推荐指数
1
解决办法
2万
查看次数