Java AES / CBC解密后,初始字节不正确

以下示例出了什么问题?

问题在于解密字符串的第一部分是胡说八道。但是,其余的都很好,我得到了…

Result: `£eB6O�geS��i are you? Have a nice day.

@Test

public void testEncrypt() {

try {

String s = "Hello there. How are you? Have a nice day.";

// Generate key

KeyGenerator kgen = KeyGenerator.getInstance("AES");

kgen.init(128);

SecretKey aesKey = kgen.generateKey();

// Encrypt cipher

Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

encryptCipher.init(Cipher.ENCRYPT_MODE, aesKey);

// Encrypt

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, encryptCipher);

cipherOutputStream.write(s.getBytes());

cipherOutputStream.flush();

cipherOutputStream.close();

byte[] encryptedBytes = outputStream.toByteArray();

// Decrypt cipher

Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

IvParameterSpec ivParameterSpec = new IvParameterSpec(aesKey.getEncoded());

decryptCipher.init(Cipher.DECRYPT_MODE, aesKey, ivParameterSpec);

// Decrypt

outputStream = new ByteArrayOutputStream();

ByteArrayInputStream inStream = new ByteArrayInputStream(encryptedBytes);

CipherInputStream cipherInputStream = new CipherInputStream(inStream, decryptCipher);

byte[] buf = new byte[1024];

int bytesRead;

while ((bytesRead = cipherInputStream.read(buf)) >= 0) {

outputStream.write(buf, 0, bytesRead);

}

System.out.println("Result: " + new String(outputStream.toByteArray()));

}

catch (Exception ex) {

ex.printStackTrace();

}

}

回答:

由于缺少一些信息,例如忘记转换为Base64,初始化向量,字符集等信息,包括我在内的很多人在进行这项工作时都面临很多问题。因此,我想到了编写功能全面的代码。

希望这对大家有用:要进行编译,你需要其他Apache Commons Codec jar,可从以下位置获得:http : //commons.apache.org/proper/commons-codec/download_codec.cgi

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class Encryptor {

public static String encrypt(String key, String initVector, String value) {

try {

IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));

SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");

cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

byte[] encrypted = cipher.doFinal(value.getBytes());

System.out.println("encrypted string: "

+ Base64.encodeBase64String(encrypted));

return Base64.encodeBase64String(encrypted);

} catch (Exception ex) {

ex.printStackTrace();

}

return null;

}

public static String decrypt(String key, String initVector, String encrypted) {

try {

IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));

SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");

cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

return new String(original);

} catch (Exception ex) {

ex.printStackTrace();

}

return null;

}

public static void main(String[] args) {

String key = "Bar12345Bar12345"; // 128 bit key

String initVector = "RandomInitVector"; // 16 bytes IV

System.out.println(decrypt(key, initVector,

encrypt(key, initVector, "Hello World")));

}

}

以上是 Java AES / CBC解密后,初始字节不正确 的全部内容, 来源链接: utcz.com/qa/423145.html

回到顶部