Java mcrypt_create_iv [英] Java mcrypt_create_iv

查看:102
本文介绍了Java mcrypt_create_iv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Java,是否有等效的mcrypt_create_iv函数?



我正在创建一个论坛,我需要用户不仅可以在网站上注册,还可以在客户端注册;我想两个注册方法都可以使用相同的加密。

解决方案

没有单次调用来创建一个随机的IV,如果你离开IV,那么Oracle的执行将默认为零IV(由N字节组成的IV,设置为 00 值,其中N是块大小)。另请注意,在Java中, getBlockSize()方法返回字节数,而不是位。

  Cipher enCipher = Cipher.getInstance(AES / CBC / PKCS5Padding); 

//创建正确大小的$数组
final byte [] ivData = new byte [enCipher.getBlockSize()];
//创建(或检索)加密安全随机实现(自动种子)
final SecureRandom rng = new SecureRandom();
//用随机数据填充IV数组
rng.nextBytes(ivData);
//生成ParameterSpec(为Cipher.init()创建一个通用参数)
IvParameterSpec iv = new IvParameterSpec(ivData);
//并用新的IV
enCipher.init(Cipher.ENCRYPT_MODE,key,iv)初始化;


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; I would like both registering methods to use the same encryption if possible.

解决方案

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);

这篇关于Java mcrypt_create_iv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆