use*_*601 2 java encryption aes initialization-vector
我必须使用 AES -256 密码以及 AES 256 密钥和 16 字节 IV 来加密我的文件,我想将密钥和 IV 保存在一个文件中并重新使用它进行解密。但目前我可以单独保存它。任何人都可以帮助我们如何将密钥和 IV 存储在单个文件中。
这是我的代码
SecureRandom srandom = new SecureRandom();
byte[] iv = new byte[16];
srandom.nextBytes(iv);
IvParameterSpec ivspec = new IvParameterSpec(iv);
FileOutputStream ivOutFile = new FileOutputStream("C:\\iv.key");
ivOutFile.write(iv);
ivOutFile.close();
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(256);
SecretKey skey = kgen.generateKey();
FileOutputStream out = new FileOutputStream("C:\\AES.key");
byte[] keyb = skey.getEncoded();
out.write(keyb);
out.close();
Cipher ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.ENCRYPT_MODE, skey, ivspec);
FileEncryptionUtils fileEncryptionUtils =new FileEncryptionUtils();
fileEncryptionUtils.processFile(ci, inFile, outFile);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6583 次 |
| 最近记录: |