gpt4 book ai didi

java - 该数组只能写入,不能读取

转载 作者:行者123 更新时间:2023-12-02 13:37:29 27 4
gpt4 key购买 nike

我尝试测试此代码的加密和解密。通过使用dst = new String(baos.toByteArray()); return dst;我无法解密密文。但是当我使用byte[] encryptedBytes = DatatypeConverter.parseHexBinary(src);时我没能运行该程序。我该如何解决这个问题?

import java.io.ByteArrayInputStream;    
import java.io.ByteArrayOutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

public class FileEncryption {

//Initial Vector
public static final byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

//EncryptAndDecrypt String -> Input : PlainText + Return : CipherText+DecipherText
public static String encryptString(String src) throws Exception
{
String dst="";
//Not Input!
if(src == null || src.length()==0)
return "";

//Encryption Setting
byte[] k="Multimediaproces".getBytes();
SecretKeySpec Key = new SecretKeySpec(k,"AES");
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE,Key,ivspec);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
CipherOutputStream cout = new CipherOutputStream(baos,encryptCipher);
cout.write(src.getBytes());
cout.flush(); //ByteOutputStream -> Write Encryption Text
cout.close();
dst = DatatypeConverter.printHexBinary(baos.toByteArray());
return dst;
}

//String src -> EncryptedData
public static String decryptString(String src) throws Exception
{
//src value is Encrypted Value!
//So, src value -> Not Byte!
String dst="";
byte[] encryptedBytes = src.getBytes();
//Not Input!
if(src == null || src.length()==0)
return "";
//Decryption Setting
IvParameterSpec ivspec = new IvParameterSpec(iv);
byte[] k="Multimediaproces".getBytes();
SecretKeySpec Key = new SecretKeySpec(k,"AES");
Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE,Key,ivspec);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream(encryptedBytes);
CipherInputStream cin = new CipherInputStream(bais,decryptCipher);
byte[] buf = new byte[1024];
int read;
while((read=cin.read(buf))>=0) //reading encrypted data!
{
baos.write(buf,0,read); //writing decrypted data!
}

// closing streams
cin.close();
byte[] encryptedBytes = DatatypeConverter.parseHexBinary(src);
return dst;
}
}

最佳答案

您的示例代码中存在三个错误:

  1. 它无法编译,因为 decryptString 方法末尾的 byte[] cryptoBytes = DatatypeConverter.parseHexBinary(src);

  2. 由于您已将加密字符串打印为十六进制表示形式,因此在尝试解密之前必须将其转换回普通字节数组。在 decryptString 中,您应该调用 byte[] cryptoBytes = DatatypeConverter.parseHexBinary(src); 。这将转换回 byte[] 而不是十六进制表示形式。

  3. 您需要在 decrpytString 末尾将 baos 转换为 String 的行。这就像 dst = baos.toString();

    一样简单

    通过这三个更改,我能够加密和解密字符串。

关于java - 该数组只能写入,不能读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42909308/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com