第一次发帖,所以不要对我太严厉(尽管我已经阅读这个论坛有一段时间了)
我在Java中使用AES遇到的问题是这样的。
首先,我需要加密一个字符串并将其写入文本文件然后压缩它,这没有问题。我使用 AES 加密并定义自己的 key ,例如“123”
其次,我需要解压缩文件(或提取它?)并使用我在第一步中使用的相同 key 对其进行解密。
这里发生的情况是:第一步很好,但第二步解密文件失败,即使字符串结果相同,总字符数,单词等
这是写入文件的代码
private static void inputKeFile(String input) throws IOException
{
FileWriter fstream = new FileWriter("C:/Users/Sactio/Desktop/tyo/txtToZip.txt",false);
BufferedWriter out = new BufferedWriter(fstream);
out.write(input);
//Close the output stream
out.close();
}
压缩文件
private static void doZip() {
try {
String filename ="C:/Users/Sactio/Desktop/tyo/txtToZip.txt";
String zipfilename="C:/Users/Sactio/Desktop/OutputZipWrite";
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
long length = file.length();
byte[] buf = new byte[(int)length];
fis.read(buf,0,buf.length);
CRC32 crc = new CRC32();
ZipOutputStream s = new ZipOutputStream(new FileOutputStream(zipfilename));
s.setLevel(9);
ZipEntry entry = new ZipEntry(filename);
entry.setSize((long)buf.length);
crc.reset();
crc.update(buf);
entry.setCrc( crc.getValue());
s.putNextEntry(entry);
s.write(buf, 0, buf.length);
s.finish();
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
这就是加密
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class JcaTest {
private Cipher ecipher;
private Cipher dcipher;
JcaTest(SecretKey key) {
try {
ecipher = Cipher.getInstance("AES");
dcipher = Cipher.getInstance("AES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception e) {
System.out.println("Failed in initialization");
}
}
public String encrypt(String str) {
try {
byte[] utf8 = str.getBytes("UTF-8");
byte[] enc = ecipher.doFinal(utf8);
return new sun.misc.BASE64Encoder().encode(enc);
} catch (Exception e) {
System.out.println("Failed in Encryption");
}
return null;
}
public String decrypt(String str) {
try {
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF-8");
} catch (Exception e) {
System.out.println("Failed in Decryption");
}
return null;
}
最后,zip 提取器
private static void bacaZip(String zipfilename) throws IOException
{
ZipInputStream zinstream = new ZipInputStream(
new FileInputStream(zipfilename));
File file = new File(zipfilename);
FileInputStream fis = new FileInputStream(file);
long length = file.length();
byte[] buf = new byte[(int)length];
ZipEntry zentry = zinstream.getNextEntry();
System.out.println("Name of current Zip Entry : " + zentry + "\n");
while (zentry != null) {
String entryName = zentry.getName();
System.out.println("Name of Zip Entry : " + entryName);
FileOutputStream outstream = new FileOutputStream("C:/Users/Sactio/Desktop/OutputZipWrite.txt");
int n;
while ((n = zinstream.read(buf, 0, buf.length)) > -1) {
outstream.write(buf, 0, n);
}
System.out.println("Successfully Extracted File Name : "
+ entryName);
outstream.close();
zinstream.closeEntry();
zentry = zinstream.getNextEntry();
}
}
private static void extractZip(String jsonString) throws FileNotFoundException
{
try {
bacaZip(jsonString);
} catch (IOException e1) {
// TODO Auto-generated catch block
System.err.println("Exception: "+e1);
}
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("C:/Users/Sactio/Desktop/OutputZipWrite.txt"));
String text = null;
// repeat until all lines is read
while ((text = reader.readLine()) != null) {
contents.append(text)
.append(System.getProperty(
"line.separator"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// show file contents here
System.out.println("HASIL: "+contents.toString());
}
如果我跳过压缩和文件步骤,AES 效果很好,但如果我将字符串发送到文件中并压缩它,AES 加密会因某种原因失败。有人对这个问题有任何想法吗?
最佳答案
您解密的文件必须与加密过程的输出逐字节相同。你对我说“即使字符串结果是相同的,总字符、单词等”,这表明你将加密文件视为文本“字符”。它不是文本,而是字节。将其视为文本会导致灾难,因为字符可以通过多种不同的方式表示为字节。您需要逐字节检查身份,并始终将密文视为字节,而不是字符。
正如@Thilo 指出的那样,压缩加密数据是没有用的。使用顺序压缩 -> 加密 -> 解密 -> 扩展。
关于java - 将字符串压缩到文本文件(AES)后解密失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11115834/