gpt4 book ai didi

java - 解密在Android上加密的桌面数据

转载 作者:行者123 更新时间:2023-11-30 03:01:46 24 4
gpt4 key购买 nike

我有一个应用程序可以加密一些文本字符串,然后将它们写入一个文件。应用程序的桌面版本正在读取文件并解密数据。问题是,每当我在桌面版本上解密时,我都会收到“javax.crypto.BadPaddingException:给定的最终 block 未正确填充”

应用程序和桌面都使用相同的代码:

import java.security.SecureRandom;

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

public class SSL {

private final static String HEX = "0123456789ABCDEF";

public static String encrypt(Session current, String cleartext) throws Exception {
byte[] rawKey = getRawKey(current.getCurrentSession().getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}

public static String decrypt(Session current, String encrypted) throws Exception {
byte[] rawKey = getRawKey(current.getCurrentSession().getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}

private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}


private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}

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 hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}

public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}

private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}

为什么我在桌面版无法解密数据? Android SDK 和 java 1.7 中的加密实现是否不同?

注意:如果我在 android 上解密加密的 android 数据,它就可以工作。如果我在桌面上加解密,也是可以的。问题似乎介于这两者之间。

最佳答案

我终于找到了完整的解决方案。

遇到了一些比较大的问题,在此说明一下,让更多的用户找到答案。首先,需要解决 Duncan 指出的两件事。

解决这些问题后,我仍然遇到同样的问题,并且发现使用伪随机数创建原始 key 的方式因不同的平台/操作系统而异。如果您想拥有跨平台独立性,请不要使用 SHA1PRNG 作为您的 key 算法。相反,请使用 PBEWithSHA256And256BitAES-CBC-BC。我正在使用 BouncyCaSTLe 的实现,请参阅下面的完整加密代码。

import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.security.spec.KeySpec;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class SSL {

private final static String HEX = "0123456789ABCDEF";
private final static String ENC = "US-ASCII";
private final static int ITERATION = 1337;

private static final String RANDOM_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";
private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String SECRET_KEY_ALGORITHM = "AES";

private static IvParameterSpec ips;

public static void init(byte[] iv) {
if(iv == null) {
iv = new byte[16];

Random random = new Random();
random.nextBytes(iv);
}

ips = new IvParameterSpec(iv);

Security.addProvider(new BouncyCastleProvider());
}

public static byte[] getCertificate() {
return ips.getIV();
}

public static String encrypt(Session current, String cleartext) throws Exception {
byte[] rawKey = getRawKey(current.getCurrentSession().toCharArray());
byte[] result = encrypt(rawKey, cleartext.getBytes(ENC));
return toHex(result);
}

public static String decrypt(Session current, String encrypted) throws Exception {
byte[] rawKey = getRawKey(current.getCurrentSession().toCharArray());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result, ENC);
}

private static byte[] getRawKey(char[] seed) throws Exception {
KeySpec keySpec = new PBEKeySpec(seed, ips.getIV(), ITERATION);

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(RANDOM_ALGORITHM);
byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");

return secretKey.getEncoded();
}


private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, SECRET_KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, SECRET_KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}

public static String toHex(String txt) throws UnsupportedEncodingException {
return toHex(txt.getBytes(ENC));
}
public static String fromHex(String hex) throws UnsupportedEncodingException {
return new String(toByte(hex), ENC);
}

public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}

public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}

private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}

关于java - 解密在Android上加密的桌面数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22406368/

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