我是一个新手,对密码学领域不太了解。
我试着在我的应用程序中尝试使用GCM模式的AES加密来保护静止数据。
我查阅了NIST GCM模式建议。它提到IV的独特性非常重要。如果(密钥,IV)对重复,攻击者可以构造密文伪造。
以下是代码示例:
我能够加密和解密input.txt文件中的文本。ECM所需的IV必须是唯一的,并且我希望使用NIST 800-38D中指定的确定性Iv构造生成它。
目前使用[http://docs.oracle.com/javase/7/docs/api/javax/crypto/spec/IvParameterSpec.html][1]中提供的IV生成方法:
IvParameterSpec(byte[] iv) 使用iv中的字节创建IvParameterSpec对象。
是否有BouncyCastle或其他库中的实现可用以获得确定性构造的IV,还是需要构建自定义实现?另外,请指导如何创建使用确定性方法的自定义IV实现。
public static void main(String[] args) throws Exception {
byte[] keyBytes = MessageDigest.getInstance("MD5").digest(
"som3C0o7p@s5".getBytes());
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
IvParameterSpec ivSpec = new IvParameterSpec(new byte[96]);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
byte[] block = new byte[96];
int i;
long st, et;
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
BufferedInputStream bIn = new BufferedInputStream(
new ProgressMonitorInputStream(null, "Encrypting ...",
new FileInputStream("input.txt")));
CipherInputStream cIn = new CipherInputStream(bIn, cipher);
BufferedOutputStream bOut = new BufferedOutputStream(
new FileOutputStream("output.txt"));
int ch;
while ((i = cIn.read(block)) != -1) {
bOut.write(block, 0, i);
}
cIn.close();
bOut.close();
Thread.sleep(5000);
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
InputStream is = new FileInputStream("output.txt");
OutputStream os = new FileOutputStream("output2.txt");
os = new CipherOutputStream(os, cipher);
while ((i = is.read(block)) != -1)
{
os.write(block, 0, i);
}
is.close();
os.close();
}
我能够加密和解密input.txt文件中的文本。ECM所需的IV必须是唯一的,并且我希望使用NIST 800-38D中指定的确定性Iv构造生成它。
目前使用[http://docs.oracle.com/javase/7/docs/api/javax/crypto/spec/IvParameterSpec.html][1]中提供的IV生成方法:
IvParameterSpec(byte[] iv) 使用iv中的字节创建IvParameterSpec对象。
是否有BouncyCastle或其他库中的实现可用以获得确定性构造的IV,还是需要构建自定义实现?另外,请指导如何创建使用确定性方法的自定义IV实现。