在Android 上如何加密/解密 __CODE__
和其他文件有一个很好的例子__CODE__
吗?我正在开发一个需要加密/解密数据的项目,但我不确定该怎么做.
首先,我已经看到 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) 这是我第一次在这里寻求帮助,我的部门(政府)已经在市场上发布了一些应用程序(谷歌播放),加密和描述工作非常好,直到昨天我拿到了Jelly Bean 4.2关系.加密工作正常,它实际上加密了要存储的信息.虽然解密它时,我得到一个完全相同的例外:pad block corrupted.我检查了字符串,并且在其他设备上与它一致(使用相同的密钥进行测试),这意味着它完全相同.问题是我们需要保持与以前版本的后兼容性,这意味着如果我在代码中更改某些内容,它应该能够读取旧的加密信息.它存储在SQLite上的加密信息,因为我需要将其编码为Base64.此行发生异常byte [] decrypted = cipher.doFinal(encrypted);
这是我的班级:
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;
public class EncodeDecodeAES {
private final static String HEX = "0123456789ABCDEF";
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
String fromHex = toHex(result);
String base64 = new String(Base64.encodeToString(fromHex.getBytes(), 0));
return base64;
}
public static String decrypt(String seed, String encrypted) throws Exception {
String base64 …
Run Code Online (Sandbox Code Playgroud) 所以我正在为自己制作个人项目,我正在尝试加密手机上的文件.这些文件可以是任何东西,例如文档,照片等.现在我正在尝试使其正常工作.当我运行加密时,它似乎正常工作并加密文件.当我运行解密时,有时它会起作用,有时却不起作用.当它失败时,我通常会在"敲定密码,填充块损坏"错误时出现"错误".我也没有使用不同的测试文件,因此它不像某些文件有效,而有些则不然.这是我每次尝试的两个文件.
public static void encryptfile(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] key = (salt + Pass).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
fis.close();
}
public static …
Run Code Online (Sandbox Code Playgroud) 用户可以购买我的应用程序的"专业版".当他们这样做时,我按如下方式存储和验证他们的购买.
SecureRandom.getInstance("SHA1PRNG", "Crypto")
- 这就是问题!所以,不是最好的系统,但是对于我的简陋应用程序来说,所有内容都是模糊不清的.
问题是SecureRandom.getInstance("SHA1PRNG", "Crypto")
在N 上失败,因为不支持"加密".我了解到依赖于特定的提供程序是不好的做法,并且N不支持加密.哎呀.
所以我遇到了一个问题:我依赖于值种子对的加密来始终具有相同的输出.Android N不支持我使用的加密提供程序,因此我不知道如何确保N上的加密输出与其他设备上的加密输出相同.
我的问题:
我的代码:
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes(), seed);
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result); // "unlock code" which must always be the same for the same seed and clearText accross android versions
}
private static byte[] getRawKey(byte[] seed, …
Run Code Online (Sandbox Code Playgroud) random android cryptography secure-random android-7.0-nougat
我有加密\解密文件的算法:
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = new SecureRandom();
sr.setSeed(seed);
kgen.init(sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec …
Run Code Online (Sandbox Code Playgroud)