Java mcrypt_create_iv

后端 未结 1 653
北恋
北恋 2021-01-03 17:05

Is there an equivalent mcrypt_create_iv function for Java?

I\'m creating a forum, I need users to be able to signup not only on the website but also within a client

相关标签:
1条回答
  • 2021-01-03 17:28

    There is no single call to create a random IV, and if you leave the IV out then the Oracle implementation will default to a zero IV (an IV consisting of N bytes set to 00 value, where N is the block size). Also note that in Java the getBlockSize() method returns the amount of bytes, not bits.

    Cipher enCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    
    // create IV array of the correct size
    final byte[] ivData = new byte[enCipher.getBlockSize()];
    // create (or retrieve) a cryptographic secure random implementation (auto-seeded)
    final SecureRandom rng = new SecureRandom();
    // fill the IV array with random data
    rng.nextBytes(ivData);
    // generate the ParameterSpec (to create a general parameter for Cipher.init())
    IvParameterSpec iv = new IvParameterSpec(ivData);
    // and initialize with the new IV
    enCipher.init(Cipher.ENCRYPT_MODE, key, iv);
    
    0 讨论(0)
提交回复
热议问题