我必须使用 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) 当我尝试为TripleDES加密器创建IV初始化向量时,出现以下错误.
请参阅代码示例:
TripleDESCryptoServiceProvider tripDES = new TripleDESCryptoServiceProvider();
byte[] key = Encoding.ASCII.GetBytes("SomeKey132123ABC");
byte[] v4 = key;
byte[] connectionString = Encoding.ASCII.GetBytes("SomeConnectionStringValue");
byte[] encryptedConnectionString = Encoding.ASCII.GetBytes("");
// Read the key and convert it to byte stream
tripDES.Key = key;
tripDES.IV = v4;
Run Code Online (Sandbox Code Playgroud)
这是我从VS获得的例外.
指定的初始化向量(IV)与此算法的块大小不匹配.
我哪里错了?
谢谢
我正在使用.net System.Security中的Rijndael类来加密我的RSA密钥
这是我如何设置它:
static Rijndael CreateRijndael (byte[] userID, string password, string pepper)
{
if (userID == null)
throw new ArgumentNullException ("userID");
if (password == null)
throw new ArgumentNullException ("password");
if (pepper == null)
throw new ArgumentNullException ("pepper");
string passpepper = password + pepper;
Rijndael Rij = Rijndael.Create ();
Rij.KeySize = 256;
Rij.Padding = PaddingMode.ISO10126;
Rij.Mode = CipherMode.CBC;
Rfc2898DeriveBytes aesKey = new Rfc2898DeriveBytes (passpepper, userID, 65536);
Rij.Key = aesKey.GetBytes (Rij.KeySize / 8);
Rij.GenerateIV ();
return Rij;
}
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
????????alue><Modulus>kgOu5EG6vbabnvq6xB+cRmxDL....
Run Code Online (Sandbox Code Playgroud)
代替
<RSAKeyValue><Modulus>kgOu5EG6vbabnvq6xB+cRmxDL... …Run Code Online (Sandbox Code Playgroud) 我想向客户端提供加密令牌,并且我使用 javax.crypto.Cipher 进行加密。根据我的阅读,不建议将 ECB 作为加密模式,因此我使用带有初始化向量的 CBC 和 TripleDES 作为密码方法。据我了解,有几点是正确的。
因此,对于这些情况,我通过存储附加到加密字节数组的 IV 来实现我的实现,并且在解密阶段,IV 从字节数组中切出并用于解密。以下是 Scala 代码,但在 Java 中不会有太大差异。
val algorithmName = "TripleDES"
def encrypt(bytes: Array[Byte], secret: String): Array[Byte] = {
val secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), algorithmName)
val encipher = Cipher.getInstance(algorithmName + "/CBC/PKCS5Padding")
val iv = encipher.getParameters.getParameterSpec(classOf[IvParameterSpec])
encipher.init(Cipher.ENCRYPT_MODE, secretKey, iv)
encipher.doFinal(bytes) ++ iv.getIV
}
def decrypt(bytes: Array[Byte], secret: String): Array[Byte] = {
val secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), algorithmName)
val encipher = Cipher.getInstance(algorithmName + "/CBC/PKCS5Padding") …Run Code Online (Sandbox Code Playgroud) 我正在尝试openssl使用aes-128-cbc加密来加密文本文件,我想知道是否有一种方法可以仅使用密钥而不是 iv 对其进行加密?
每次我尝试运行时:
openssl enc -aes-128-cbc -e -in dummy_file.txt -out dummy.aes-128-cbc.bin -K 00112233445566778889aabbccddeeff
Run Code Online (Sandbox Code Playgroud)
我收到错误消息iv undefined,它生成的加密文件为空,甚至不是二进制文件。
我知道 IV 应该是随机的并与纯文本进行异或以开始加密。我的问题是,除了密钥之外,我是否还必须记住随机 IV 才能解密?
encryption ×5
aes ×2
cryptography ×2
java ×2
c# ×1
cbc-mode ×1
rijndael ×1
scala ×1
security ×1