java - AES 缓冲区大小

标签 java encryption aes

我正在尝试适应 this DES 加密示例到 AES,所以我进行了更改,并尝试运行:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

// Adapted from http://www.exampledepot.com/egs/javax.crypto/DesFile.html
public class AesEncrypter {

    private Cipher ecipher;
    private Cipher dcipher;

    // Buffer used to transport the bytes from one stream to another
    private byte[] buf = new byte[1024];

    public AesEncrypter(SecretKey key) throws Exception {
 // Create an 8-byte initialization vector
 byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A };
 AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

 ecipher = Cipher.getInstance("AES/CBC/NoPadding");
 dcipher = Cipher.getInstance("AES/CBC/NoPadding");

 // CBC requires an initialization vector
 ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
 dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    }

    public void encrypt(InputStream in, OutputStream out) throws Exception {
 // Bytes written to out will be encrypted
 out = new CipherOutputStream(out, ecipher);

 // Read in the cleartext bytes and write to out to encrypt
 int numRead = 0;
 while ((numRead = in.read(buf)) >= 0) {
     out.write(buf, 0, numRead);
 }
 out.close();
    }

    public void decrypt(InputStream in, OutputStream out) throws Exception {
 // Bytes read from in will be decrypted
 in = new CipherInputStream(in, dcipher);

 // Read in the decrypted bytes and write the cleartext to out
 int numRead = 0;
 while ((numRead = in.read(buf)) >= 0) {
     out.write(buf, 0, numRead);
 }
 out.close();
    }

    public static void main(String[] args) throws Exception {
 System.out.println("Starting...");

 SecretKey key = KeyGenerator.getInstance("AES").generateKey();

 InputStream in = new FileInputStream(new File("/home/wellington/Livros/O Alienista/speechgen0001.mp3/"));
 OutputStream out = System.out;

 AesEncrypter encrypter = new AesEncrypter(key);
 encrypter.encrypt(in, out);

 System.out.println("Done!");
    }

}

但我得到了异常:

InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long

所以我尝试通过改变来解决

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

对于

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv, 0, 16);

但结果是

IV buffer too short for given offset/length combination

我可以继续尝试直到它起作用,但我想听听谁使用 AES,常用的缓冲区大小是多少?

最佳答案

您的主要问题已得到解答,但我想补充一点,除非您知道自己在做什么,否则通常不应使用固定字符串 IV。您可能还想使用 PKCS5Padding 而不是 NoPadding。

关于java - AES 缓冲区大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2554062/

相关文章:

java - 替换 Java Bean 的 instanceof

java - 如何创建 API 来与我的应用程序交互

c++ - 生成的加密字符串在 PyCrypto 和 Crypto++ 中的大小不同

.net - 安全、可移植的加密配置值——可能吗?

javascript - 使用 JSBN 为 Javascript RSA key 生成选择 N 和 E

php - 将.net aes256加密代码转换为php

c# - iOS 和 Android 中的 AES 加密,以及 C#.NET 中的解密

java - CDI 1.1 : Is @Observes @Initialized(TransactionScoped. 类)应该可以工作吗?

java - Android- ActionBar AutoCompleteTextview

java - 安卓解密错误