gpt4 book ai didi

java - 这是一种安全的加密方法吗

转载 作者:太空宇宙 更新时间:2023-11-03 13:06:24 26 4
gpt4 key购买 nike

我正在为 Android 编写一个应用程序,它使用对称 key 加密来保护敏感数据。据我所知,Android 只直接支持“PBEWithMD5AndDES”。这个算法有多安全?另外,我在下面包含了我的代码(非安卓)。我的代码是否正确地加密了数据?

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class CipherTest
{

private static class EncryptInfo
{

private final byte[] encryptedData;
private final byte[] initVector;
private final byte[] salt;

public EncryptInfo(byte[] encryptedData, byte[] initVector, byte[] salt)
{
this.encryptedData = encryptedData.clone();
this.initVector = initVector.clone();
this.salt = salt.clone();
}

public byte[] getEncryptedData()
{
return encryptedData;
}

public byte[] getInitVector()
{
return initVector;
}

public byte[] getSalt()
{
return salt;
}

}

private static final String keyGenAlgorithm = "PBEWithMD5AndDES";
private static final String keyAlgorithm = "DES";
private static final String cipherTransform = "PBEWithMD5AndDES/CBC/PKCS5Padding";

private static EncryptInfo encrypt(char[] password, byte[] data)
throws NoSuchAlgorithmException, InvalidKeySpecException,
NoSuchPaddingException, InvalidKeyException,
InvalidParameterSpecException, IllegalBlockSizeException,
BadPaddingException, UnsupportedEncodingException
{

byte[] salt = new byte[16];
new SecureRandom().nextBytes(salt);

PBEKeySpec keySpec = new PBEKeySpec(password, salt, 1024);

SecretKeyFactory secretKeyFactory = SecretKeyFactory
.getInstance(keyGenAlgorithm);
SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
keySpec.clearPassword();
byte[] key = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(key, keyAlgorithm);
Cipher cipher = Cipher.getInstance(cipherTransform);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

byte[] initVector = cipher.getParameters().getParameterSpec(
IvParameterSpec.class).getIV();

return new EncryptInfo(cipher.doFinal(data), initVector, salt);
}

public static byte[] decrypt(byte[] data, char[] password, byte[] salt,
byte[] initVector) throws NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchPaddingException,
InvalidKeyException, InvalidAlgorithmParameterException,
IllegalBlockSizeException, BadPaddingException
{
PBEKeySpec keySpec = new PBEKeySpec(password, salt, 1024);

SecretKeyFactory secretKeyFactory = SecretKeyFactory
.getInstance(keyGenAlgorithm);
SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
keySpec.clearPassword();
byte[] key = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(key, keyAlgorithm);
Cipher cipher = Cipher.getInstance(cipherTransform);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(
initVector));
return cipher.doFinal(data);
}

public static void main(String[] args) throws Exception
{
char[] password = "password".toCharArray();

EncryptInfo info = encrypt(password, "Message".getBytes());

byte[] decyptedText = decrypt(info.getEncryptedData(), password, info
.getSalt(), info.getInitVector());

System.out.println(new String(decyptedText));

}
}

最佳答案

MD5 和 DES 都很弱。如果您的加密数据确实很有值(value),您应该寻找一些适用于 Android 的外部加密库,它提供 AES 和 SHA256/SHA512 算法。

关于java - 这是一种安全的加密方法吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3811302/

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