- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码存在一些问题,我没有使用相同的 IV 进行加密和解密。我知道为了正确地做到这一点,我必须在数据之前将 IV 写入我的输出文件,但是我正在努力实现这一点。谁能帮我解决这个问题吗?
再次编辑代码以显示完整范围
public class TestFileEncryption {
private static void mainCrypto(int cipherMode, File inputFile, File outputFile) throws Exception{
//Let the user enter the key they wish to use
Key secretKey = new SecretKeySpec(UITest.getStoreKey().getBytes(), UITest.getSendAlg()); //Generates a key based on the default keysize for the specified algorithm
//Generate an Initialization Vector (IV)
final int ALG_KEYLENGTH = UITest.getStoreKey().length(); //Change this as desired for the security level you want
byte[] iv = new byte[ALG_KEYLENGTH]; //Save the IV bytes or send it in plaintext with the encrypted data so you can decrypt the data later
SecureRandom prng = new SecureRandom(); //Use SecureRandom to generate random bits. The size of the IV matches the blocksize of the cipher
prng.nextBytes(iv); //Construct the appropriate IvParameterSpec object for the data to pass to Cipher's init() method
//Create a Cipher by specifying the following parameters: Alg name, Mode (CBC), Padding (PKC7/PKCS5)
Cipher cipherForEncryption = Cipher.getInstance(UITest.getSendAlg() + "/CBC/PKCS5PADDING"); // Must specify the mode explicitly as most JCE providers default to ECB mode
//Initialize the Cipher for Encryption
cipherForEncryption.init(cipherMode, secretKey, new IvParameterSpec(iv));
//Declare / Initialize the Data, Convert the Input to Bytes and encrypt or decrypt using doFinal.
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length() - ALG_KEYLENGTH];
inputStream.read(iv);
inputStream.read(inputBytes);
byte[] outputBytes = cipherForEncryption.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(iv);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
}
public static void encrypt(File inputFile, File outputFile) throws Exception {
mainCrypto(Cipher.ENCRYPT_MODE, inputFile, outputFile); //ENC_MODE = Constant used to initialize cipher to encryption mode.
}
public static void decrypt(File inputFile, File outputFile) throws Exception {
mainCrypto(Cipher.DECRYPT_MODE, inputFile, outputFile); //ENC_MODE = Constant used to initialize cipher to encryption mode.
}
public static void main(String[] args) {}
}
最佳答案
只是扩展@Javier的答案。
看起来您想使用相同的方法进行加密和解密(取决于模式),但是处理 IV 方面存在差异。
您生成了一个随机 IV,然后用(纯)输入的输入覆盖它,最后将其写入输出(无论它是解密的)。
所以你要区分模式是否为
类似的东西:
private void encrypt(File inputFile, File outputFile) {
//Declare / Initialize the Data, Convert the Input to Bytes and encrypt or decrypt using doFinal.
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
byte[] iv = new byte[16]; // 16 for AES-CBC
SecureRandom prng = new SecureRandom(); //Use SecureRandom to generate random bits. The size of the IV matches the blocksize of the cipher
prng.nextBytes(iv); //Construct the appropriate IvParameterSpec object for the data to pass to Cipher's init() method
//Create a Cipher by specifying the following parameters: Alg name, Mode (CBC), Padding (PKC7/PKCS5)
Cipher cipherForEncryption = Cipher.getInstance(UITest.getSendAlg() + "/CBC/PKCS5PADDING"); // Must specify the mode explicitly as most JCE providers default to ECB mode
//Initialize the Cipher for Encryption
cipherForEncryption.init(cipherMode, secretKey, new IvParameterSpec(iv));
inputStream.read(inputBytes);
byte[] outputBytes = cipherForEncryption.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(iv);
outputStream.write(outputBytes);
outputStream.flush();
inputStream.close();
outputStream.close();
}
}
private void decrypt(File inputFile, File outputFile) {
//Declare / Initialize the Data, Convert the Input to Bytes and encrypt or decrypt using doFinal.
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()-16];
byte[] iv = new byte[16]; // 16 for AES-CBC
//Create a Cipher by specifying the following parameters: Alg name, Mode (CBC), Padding (PKC7/PKCS5)
Cipher cipherForEncryption = Cipher.getInstance(UITest.getSendAlg() + "/CBC/PKCS5PADDING"); // Must specify the mode explicitly as most JCE providers default to ECB mode
//Initialize the Cipher for Encryption
cipherForEncryption.init(cipherMode, secretKey, new IvParameterSpec(iv));
inputStream.read(iv);
inputStream.read(inputBytes);
byte[] outputBytes = cipherForEncryption.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
outputStream.flush();
inputStream.close();
outputStream.close();
}
为了省略一些细节,也许您可以直接使用 Java CipherOutputStream 和 CiptherInputStream,并且实现将为您处理这些细节(如果您不关心确切的格式)。
接下来您缺少的是身份验证标签,至少是保证密文完整性的明文哈希。 (这称为认证加密)
关于java - AES 使用相同的 IV 进行加密和解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44971076/
题目地址:https://leetcode-cn.com/problems/course-schedule-iv/ 题目描述 你总共需要上 n 门课,课程编号依次为 0 到 n-1 。 有的课会
我即将使用以下脚本来加密和解密一些数据。我正在使用它,因为我当前的加密在我们的新服务器上不起作用。我们目前正在使用 mcrypt,所以我想更改为 openssl。 在我们的数据库中,我们使用 aes
有人可以告诉我为什么会收到此错误消息吗?显然这是我缺少的某种转换 预期 IV 长度为 16 但实际为 24 我用它来调用它 String encrypted = "E5ADDEB05D9D7B3925
这是我用来加密/解密数据的代码: // Set the method $method = 'AES-128-CBC'; // Set the encryption key $encryption_ke
1.题目 给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。 注意:你
已关闭。这个问题是 not about programming or software development 。目前不接受答案。 这个问题似乎不是关于 a specific programming
在 AES 中,我的理解是盐是使密码更安全的东西,它不会被添加到加密文本中。但是IV是用来加密第一个消息 block 的东西,将被添加到加密文本中。 我有什么问题吗? 最佳答案 AES 本身并不直接使
我需要编写一个简化的加密 API,它可以通过使用随机生成的 key 或密码派生的 key 轻松处理对称加密。 密码生成是使用 OpenSSL 库中的 PKCS5_PBKDF2_HMAC() 函数并使用
我正在 Java 中使用 AES/CTR/NoPadding 使用恒定 key 和随机 IV 来加密我的消息。 我计划将 IV 添加到密文前面,然后对其进行 Base64 编码。 现在有一个问题困扰着
如果在加密明文时使用随机IV,那么密文的接收者如何知道IV是什么以对其进行解密? 这是对上一个关于 IVs here. 的 stackoverflow 问题的回复的后续问题 The IV allows
我“到处”都找过了,但找不到。是否有示例说明如何使用 c++ Quantlib 来对具有合成行使价/到期日的期权价格进行插值? 例如,如果“今天”是 2017 年 2 月 6 日,我得到了 2017
当我尝试为 TripleDES 加密器创建 IV 初始化向量时出现以下错误。 请看代码示例: TripleDESCryptoServiceProvider tripDES = new TripleDE
我无法理解以下句子:“初始化向量 (IV) 中的数字全为零(不是 ASCII 字符‘0’)。 我的目标是使用 openssl enc 命令使用 aes-128-cbc 和 key K(比如 12345
我正在阅读有关如何对 key 加盐以确保加密安全的教程,但没能充分理解它。我对密码学了解不多,需要一些帮助。我正在使用 commoncrypto 来加密文件,并且已经完成,除了它不安全这一事实....
我正在尝试创建dBase文件,因为似乎我的Export vba代码中的.TransferDatabase()方法要求该文件存在(请继续获取“字段将不适合记录”。假设由于文件不存在)。 我在网上使用vb
是否建议我使用 initialization vector加密/解密我的数据?它会让事情变得更安全吗?这是需要根据具体情况进行评估的事情之一吗? 为了将其放入实际上下文中,Win32 加密函数 Cry
我可以知道一个 R 包和代码,用于通过可用的诊断测试(例如,弱仪器测试、外生性测试(使用 Wu-Hausman)、Sargan 测试)运行固定效应工具变量 (IV) 回归吗? 我知道 plm 代码提供
我可以知道一个 R 包和代码,用于通过可用的诊断测试(例如,弱仪器测试、外生性测试(使用 Wu-Hausman)、Sargan 测试)运行固定效应工具变量 (IV) 回归吗? 我知道 plm 代码提供
我试图在加密后但写入之前将一个 16 字节安全生成的 IV 附加到每个文件上。这样我以后就可以通过从文件中拉出 IV 来解密它。这是我到目前为止的代码: public static void encr
了解 PKCS5_PBKDF2_HMAC()需要一个salt 并返回一个derivedKey 还有那个GCM::Encryption.SetKeyWithIV()需要 iv(以及 derivedKey
我是一名优秀的程序员,十分优秀!