2010-03-31 3 views
3

Я пытаюсь адаптировать this DES шифрования примера AES, так что я сделал изменения, и попробуйте запустить это: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, что обычно используемый размер буфера?

ответ

3

Я думаю, что это говорит, что хочет 16 байт, но это только 8 байт:

byte[] iv = new byte[] { 
    (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A 
}; 

Может попробовать это?

byte[] iv = new byte[] { 
    (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A, 
    (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A 
}; 
4

Ваш главный вопрос был дан ответ, но я хотел бы добавить, что вы не должны вообще использовать фиксированную строку IV, если вы не знаете, что вы делаете. Возможно, вы также захотите использовать PKCS5Padding вместо NoPadding.

+0

Спасибо, я прочитаю PKCS5Padding .. –

Смежные вопросы