AES 密码未获取 IV

发布于 2024-10-10 12:48:58 字数 3711 浏览 5 评论 0原文

我正在尝试将 IV 与 AES 结合使用,以便加密文本不可预测。但是,加密的十六进制字符串始终相同。

我实际上尝试了几种方法,试图通过向密码初始化调用传递一些附加参数来添加一些随机性:

1)手动 IV 生成

byte[] iv = generateIv();
IvParameterSpec ivspec = new IvParameterSpec(iv);

2)要求密码生成 IV

AlgorithmParameters params = cipher.getParameters();
params.getParameterSpec(IvParameterSpec.class);

3)使用 PBEParameterSpec

byte[] encryptionSalt = generateSalt();
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);

所有这些似乎都对加密文本...帮助!!!

我的代码:

package com.citc.testencryption;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Main extends Activity {

 public static final int SALT_LENGTH = 20;
 public static final int PBE_ITERATION_COUNT = 1000;

 private static final String RANDOM_ALGORITHM = "SHA1PRNG";
 private static final String PBE_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";
 private static final String CIPHER_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";

 private static final String TAG = Main.class.getSimpleName();

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  try {

   String password = "password";
   String plainText = "plaintext message to be encrypted";

   // byte[] salt = generateSalt();
   byte[] salt = "dfghjklpoiuytgftgyhj".getBytes();
   Log.i(TAG, "Salt: " + salt.length + " " + HexEncoder.toHex(salt));
   PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT);
   SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBE_ALGORITHM);
   SecretKey secretKey = keyFac.generateSecret(pbeKeySpec);
   byte[] key = secretKey.getEncoded();
   Log.i(TAG, "Key: " + HexEncoder.toHex(key));

   // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATION_COUNT);

   Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);

   // byte[] encryptionSalt = generateSalt();
   // Log.i(TAG, "Encrypted Salt: " + encryptionSalt.length + " " + HexEncoder.toHex(encryptionSalt));
   // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);
   // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
   // Log.i(TAG, encryptionCipher.getParameters() + " ");
   byte[] iv = generateIv();
   IvParameterSpec ivspec = new IvParameterSpec(iv);

   encryptionCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
   byte[] encryptedText = encryptionCipher.doFinal(plainText.getBytes());
   Log.i(TAG, "Encrypted: " + HexEncoder.toHex(encryptedText)); // <== Why is this always the same :(

   Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);
   decryptionCipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
   byte[] decryptedText = decryptionCipher.doFinal(encryptedText);
   Log.i(TAG, "Decrypted: " + new String(decryptedText));

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 private byte[] generateSalt() throws NoSuchAlgorithmException {
  SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
  byte[] salt = new byte[SALT_LENGTH];
  random.nextBytes(salt);
  return salt;
 }

 private byte[] generateIv() throws NoSuchAlgorithmException {
  SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
  byte[] iv = new byte[16];
  random.nextBytes(iv);
  return iv;
 }

}

I am trying to use an IV with AES so that the encrypted text is unpredictable. However, the encrypted hex string is always the same.

I have actually tried a few methods of attempting to add some randomness by passing some additional parameters to the cipher init call:

1) Manual IV generation

byte[] iv = generateIv();
IvParameterSpec ivspec = new IvParameterSpec(iv);

2) Asking cipher to generate IV

AlgorithmParameters params = cipher.getParameters();
params.getParameterSpec(IvParameterSpec.class);

3) Using a PBEParameterSpec

byte[] encryptionSalt = generateSalt();
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);

All of these seem to have no influence on the encrypted text.... help!!!

My code:

package com.citc.testencryption;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Main extends Activity {

 public static final int SALT_LENGTH = 20;
 public static final int PBE_ITERATION_COUNT = 1000;

 private static final String RANDOM_ALGORITHM = "SHA1PRNG";
 private static final String PBE_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";
 private static final String CIPHER_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";

 private static final String TAG = Main.class.getSimpleName();

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  try {

   String password = "password";
   String plainText = "plaintext message to be encrypted";

   // byte[] salt = generateSalt();
   byte[] salt = "dfghjklpoiuytgftgyhj".getBytes();
   Log.i(TAG, "Salt: " + salt.length + " " + HexEncoder.toHex(salt));
   PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT);
   SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBE_ALGORITHM);
   SecretKey secretKey = keyFac.generateSecret(pbeKeySpec);
   byte[] key = secretKey.getEncoded();
   Log.i(TAG, "Key: " + HexEncoder.toHex(key));

   // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATION_COUNT);

   Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);

   // byte[] encryptionSalt = generateSalt();
   // Log.i(TAG, "Encrypted Salt: " + encryptionSalt.length + " " + HexEncoder.toHex(encryptionSalt));
   // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);
   // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
   // Log.i(TAG, encryptionCipher.getParameters() + " ");
   byte[] iv = generateIv();
   IvParameterSpec ivspec = new IvParameterSpec(iv);

   encryptionCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
   byte[] encryptedText = encryptionCipher.doFinal(plainText.getBytes());
   Log.i(TAG, "Encrypted: " + HexEncoder.toHex(encryptedText)); // <== Why is this always the same :(

   Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);
   decryptionCipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
   byte[] decryptedText = decryptionCipher.doFinal(encryptedText);
   Log.i(TAG, "Decrypted: " + new String(decryptedText));

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 private byte[] generateSalt() throws NoSuchAlgorithmException {
  SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
  byte[] salt = new byte[SALT_LENGTH];
  random.nextBytes(salt);
  return salt;
 }

 private byte[] generateIv() throws NoSuchAlgorithmException {
  SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
  byte[] iv = new byte[16];
  random.nextBytes(iv);
  return iv;
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美胚控场 2024-10-17 12:48:58

我改变了生成密钥的方式...现在已修复。

package com.citc.testencryption;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

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 android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Main extends Activity {

    public static final int SALT_LENGTH = 20;
    public static final int PBE_ITERATION_COUNT = 1000;

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

    private static final String TAG = Main.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {

            String password = "password";
            String plainText = "plaintext message to be encrypted";

            // byte[] salt = generateSalt();
            byte[] salt = "dfghjklpoiuytgftgyhj".getBytes();
            Log.i(TAG, "Salt: " + salt.length + " " + HexEncoder.toHex(salt));
            PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256);
            SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM);
            SecretKey tmp = factory.generateSecret(pbeKeySpec);
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
            byte[] key = secret.getEncoded();
            Log.i(TAG, "Key: " + HexEncoder.toHex(key));

            // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATION_COUNT);

            Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);

            // byte[] encryptionSalt = generateSalt();
            // Log.i(TAG, "Encrypted Salt: " + encryptionSalt.length + " " + HexEncoder.toHex(encryptionSalt));
            // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);
            // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
            Log.i(TAG, encryptionCipher.getParameters() + " ");
            byte[] iv = generateIv();
            IvParameterSpec ivspec = new IvParameterSpec(iv);

            encryptionCipher.init(Cipher.ENCRYPT_MODE, secret, ivspec);
            byte[] encryptedText = encryptionCipher.doFinal(plainText.getBytes());
            Log.i(TAG, "Encrypted: " + HexEncoder.toHex(encryptedText));

            Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);
            decryptionCipher.init(Cipher.DECRYPT_MODE, secret, ivspec);
            byte[] decryptedText = decryptionCipher.doFinal(encryptedText);
            Log.i(TAG, "Decrypted: " + new String(decryptedText));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private byte[] generateSalt() throws NoSuchAlgorithmException {
        SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
        byte[] salt = new byte[SALT_LENGTH];
        random.nextBytes(salt);
        return salt;
    }

    private byte[] generateIv() throws NoSuchAlgorithmException {
        SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
        byte[] iv = new byte[16];
        random.nextBytes(iv);
        return iv;
    }

}

I changed the way I was generating the secret key... fixed now.

package com.citc.testencryption;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

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 android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Main extends Activity {

    public static final int SALT_LENGTH = 20;
    public static final int PBE_ITERATION_COUNT = 1000;

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

    private static final String TAG = Main.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {

            String password = "password";
            String plainText = "plaintext message to be encrypted";

            // byte[] salt = generateSalt();
            byte[] salt = "dfghjklpoiuytgftgyhj".getBytes();
            Log.i(TAG, "Salt: " + salt.length + " " + HexEncoder.toHex(salt));
            PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256);
            SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM);
            SecretKey tmp = factory.generateSecret(pbeKeySpec);
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
            byte[] key = secret.getEncoded();
            Log.i(TAG, "Key: " + HexEncoder.toHex(key));

            // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATION_COUNT);

            Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);

            // byte[] encryptionSalt = generateSalt();
            // Log.i(TAG, "Encrypted Salt: " + encryptionSalt.length + " " + HexEncoder.toHex(encryptionSalt));
            // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);
            // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
            Log.i(TAG, encryptionCipher.getParameters() + " ");
            byte[] iv = generateIv();
            IvParameterSpec ivspec = new IvParameterSpec(iv);

            encryptionCipher.init(Cipher.ENCRYPT_MODE, secret, ivspec);
            byte[] encryptedText = encryptionCipher.doFinal(plainText.getBytes());
            Log.i(TAG, "Encrypted: " + HexEncoder.toHex(encryptedText));

            Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);
            decryptionCipher.init(Cipher.DECRYPT_MODE, secret, ivspec);
            byte[] decryptedText = decryptionCipher.doFinal(encryptedText);
            Log.i(TAG, "Decrypted: " + new String(decryptedText));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private byte[] generateSalt() throws NoSuchAlgorithmException {
        SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
        byte[] salt = new byte[SALT_LENGTH];
        random.nextBytes(salt);
        return salt;
    }

    private byte[] generateIv() throws NoSuchAlgorithmException {
        SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
        byte[] iv = new byte[16];
        random.nextBytes(iv);
        return iv;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文