下面是我在对我的android项目的AES进行了一些研究之后收集起来的一个算法,我想知道的是,它安全吗?它可以改进吗?我之所以问这个问题,是因为我看到了很多不同的方法,只是需要一点帮助。
谢谢,我真的很感谢你的帮助。
private static final int pswdIterations = 1000;
private static final int keySize = 256;
private static final int saltlength = keySize / 8;
private static final String ENCODING = "UTF-8";
private static final String PBK = "PBKDF2WithHmacSHA1";
private static final String AES = "AES";
private static final String CIPHER = "AES/CBC/PKCS5Padding";
public String encrypt(String plainText) throws Exception {
//get text from password field
final String pass = password.getText().toString();
//get salt from generateSalt() method (see below)
String salt = generateSalt();
//convert salt to bytes
byte[] saltBytes = salt.getBytes(ENCODING);
// Derive the key from
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBK);
PBEKeySpec spec = new PBEKeySpec(
pass.toCharArray(),
saltBytes,
pswdIterations,
keySize
);
//encode key
SecretKey secretKey = factory.generateSecret(spec);
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), AES);
//encrypt the message
Cipher cipher = Cipher.getInstance(CIPHER);
cipher.init(Cipher.ENCRYPT_MODE, secret);
AlgorithmParameters params = cipher.getParameters();
byte[] ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes(ENCODING));
//encode text and output final encrypted text
String encodedText = Base64.encodeToString(encryptedTextBytes, Base64.DEFAULT);
String encodedIV = Base64.encodeToString(ivBytes, Base64.DEFAULT);
String encodedSalt = Base64.encodeToString(saltBytes, Base64.DEFAULT);
return encodedSalt + encodedText + encodedIV;
}
public static String generateSalt() {
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[saltlength];
random.nextBytes(bytes);
return new String(bytes);
}发布于 2017-10-24 19:57:36
基本上,该方案看起来是安全的,但不包括身份验证。
改进:
备注:
考虑使用RNCryptor
看看RNCryptor-Spec,看看如何将不同的项目打包在一起。
https://stackoverflow.com/questions/46915299
复制相似问题