有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在AES encryptopn中将数组大小从SHA1输出的20字节更改为适合IV 16字节

我需要实现AES-CBC加密算法,但我手动创建initialize IV的示例如下:

//initialize IV  manually

byte[] ivBytes = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

我拥有的AES-CBC:

    package xxxx;
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.IvParameterSpec; 
    import javax.crypto.spec.SecretKeySpec;
    public class AES_CBC
    {
        public static void main(String[] args)
        {
            try
            {
                    //Lookup a key generator for the AES cipher
                        KeyGenerator kg = KeyGenerator.getInstance("AES");
                SecretKey key = kg.generateKey();
                SecretKeySpec keySpec = new
                        SecretKeySpec(key.getEncoded(), "AES");     
              //Lookup an instance of a AES cipher
               Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

      //initialize IV  manually
byte[] ivBytes = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
            //create IvParameterSpecobject
              IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);     

            //Initialize the cipher using the secter key

        cipher.init(Cipher.ENCRYPT_MODE, keySpec,ivSpec);

           //Message to encrypt
             String plainText = "This is a secret!";

             //Sequence of byte to encrypt 
             //Encrypt the message      
            byte[] cipherText = cipher.doFinal(plainText.getBytes());

                System.out.println("Resulting Cipher Text:\n");
                for(int i=0;i<cipherText.length;i++)
                {
                System.out.print(cipherText[i] + " ");
                }
                System.out.println("");
                        } catch (Exception e)
            {
                e.printStackTrace();
            } } }

我希望使用SHA-1的输出,而不是手动设置IV 作为initialize IV,由于每次我有不同的整数值,我使用SHA-1获得固定长度,并在AES中将其重新用作initialize IV,例如:

input : 10
digest : b1d5781111d84f7b3fe45a0852e59758cd7a87e5

所以,AES代码需要16字节,SHA-1产生20字节,我怎样才能将20B转换成16字节的AES代码

如何重用:b1d5781111d84f7b3fe45a0852e59758cd7a87e5而不是0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00


共 (1) 个答案

  1. # 1 楼答案

    不清楚您所说的“使用SHA-1的输出作为初始化IV”是什么意思。你散列了什么信息?这听起来像是默默无闻中的安全感

    好的静脉注射应该是绝对随机和不可预测的,以确保安全

    只需从SecureRandom中获取随机字节以用作IV:

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    
        SecureRandom rnd = new SecureRandom();
        byte[] iv = new byte[cipher.getBlockSize()];
        rnd.nextBytes(iv);
        IvParameterSpec ivParams = new IvParameterSpec(iv);
    
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), ivParams);
    
        byte[] ciphertext = cipher.doFinal(input.getBytes());
    

    然后将生成的IV与密文一起存储在加密输出中

    在解密过程中,首先读取IV,然后读取密文,然后解密