首先,我已经看到 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) 以下代码适用于除最新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) 我需要将加密数据从Java客户端发送到C#服务器.现在我正在学习如何使用AES(要求)加密数据.继这个接受的答案用AES加密/解密我正在做以下事情:
byte[] keyStart = "qweroiwejrwoejlsifeoisrn".getBytes(); // Random character string
byte[] toEncrypt = myMessageString.getBytes();
keyGen = KeyGenerator.getInstance("AES");
sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(keyStart);
keyGen.init(128, sr);
SecretKey secretKey = keyGen.generateKey();
byte[] secretKeyByte = secretKey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(secretKeyByte, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
cipher.doFinal(toEncrypt);
Run Code Online (Sandbox Code Playgroud)
由于算法使用了一个SecureRandom使用keyStart我不知道这是否可以解码,C#甚至在另一个Java程序中解码,没有SecureRandom.
这个加密/解密是否能够只知道keyStart我正在使用的值或者因为SecureRandom我仍然需要传递其他东西才能解密?
还有,有更好的方法来做这个还是这个好吗?
最近从BC 1.34升级到1.45.我用以下方法解码一些以前编码的数据:
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
Run Code Online (Sandbox Code Playgroud)
当使用BC 1.45时,我得到了这个例外:
javax.crypto.BadPaddingException: pad block corrupted
at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:715)
at javax.crypto.Cipher.doFinal(Cipher.java:1090)
Run Code Online (Sandbox Code Playgroud)
编辑:有关此问题的更多信息.我使用以下方法从密码短语生成原始密钥:
KeyGenerator kgen = KeyGenerator.getInstance("AES", "BC");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
sr.setSeed(seed);
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
Run Code Online (Sandbox Code Playgroud)
我发现,这导致BC 1.34和1.45两个不同的值.
它可能也不是BouncyCastle相关的(我在Android 2.3上测试)
我正在制作一个Android应用程序,我想在将其发送到DataBase之前对其进行加密,并且加密是正确的.解密String时会出现问题,因为我收到了BadPaddingException并且我不知道问题出在哪里.这是代码:
public final static String HEX = "36A52C8FB7DF9A3F";
public static String encrypt(String seed, String cleartext) throws Exception
{
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted) throws Exception
{
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public static byte[] toByte(String …Run Code Online (Sandbox Code Playgroud) android ×4
cryptography ×3
encryption ×3
bouncycastle ×2
java ×2
c# ×1
exception ×1
unit-testing ×1