使用AES-128算法在Java中加密Excel文件,在C中解密#
我试图用Java加密excel文件,用C#解密,但解密后的文件显示垃圾字符。我能够在Java中加密.txt和.docx文件,并在C#中解密,没有任何问题 请检查代码并让我知道我是否做错了什么?如果有什么需要补充的,请告诉我 加密xls文件的Java代码使用AES-128算法在Java中加密Excel文件,在C中解密#,java,c#,encryption,cryptography,aes,Java,C#,Encryption,Cryptography,Aes,我试图用Java加密excel文件,用C#解密,但解密后的文件显示垃圾字符。我能够在Java中加密.txt和.docx文件,并在C#中解密,没有任何问题 请检查代码并让我知道我是否做错了什么?如果有什么需要补充的,请告诉我 加密xls文件的Java代码 // file to be encrypted File file = new File("D:\\SynchData\\output.xls"); FileInputStream inFile = new FileInp
// file to be encrypted
File file = new File("D:\\SynchData\\output.xls");
FileInputStream inFile = new FileInputStream(file);
FileOutputStream outFile = new FileOutputStream("D:\\SynchData\\output.dec");
// password to encrypt the file
String password = "MAKV2SPBNI99212";
// password, iv and salt should be transferred to the other end
// in a secure manner
// salt is used for encoding
// writing it to a file
// salt should be transferred to the recipient securely
// for decryption
byte[] salt = new byte[16];
/*SecureRandom secureRandom = new SecureRandom();
secureRandom.nextBytes(salt);*/
FileOutputStream saltOutFile = new FileOutputStream("salt.enc");
saltOutFile.write(salt);
saltOutFile.close();
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 1024, 128);
SecretKey secretKey = factory.generateSecret(keySpec);
SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
AlgorithmParameters params = cipher.getParameters();
// iv adds randomness to the text and just makes the mechanism more
// secure
// used while initializing the cipher
// file to store the iv
FileOutputStream ivOutFile = new FileOutputStream("iv.enc");
byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
log.info("iv length: "+iv.length);
ivOutFile.write(iv);
ivOutFile.close();
//file encryption
byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = inFile.read(input)) != -1) {
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null)
outFile.write(output);
}
byte[] output = cipher.doFinal();
if (output != null)
outFile.write(output);
inFile.close();
outFile.flush();
outFile.close();
log.info("File Encrypted. exportEncryptFile");
要解密的C#代码
byte[] salt = new byte[16];
int iterations = 1024;
string EncryptionKey = "MAKV2SPBNI99212";
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, salt,iterations);
encryptor.Key = pdb.GetBytes(16);
encryptor.IV = pdb.GetBytes(16);
using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
{
// StreamReader strReader = new StreamReader(fsInput);
//string str= strReader.ReadToEnd();
using (CryptoStream cs = new CryptoStream(fsInput, encryptor.CreateDecryptor(), CryptoStreamMode.Read))
{
using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create))
{
int data;
while ((data = cs.ReadByte()) != -1)
{
fsOutput.WriteByte((byte)data);
}
}
}
}
}
我看不出有什么明显的问题。文件中的垃圾字节在哪里(开始/结束/中间)?这些是额外的字节还是替换了原始文件中的其他字节?我看不到有人试图确保两侧的编码是相同的。也许这就是问题所在。另外,你在哪里读取与加密相同的salt来解密?我看到你使用的是一个空白字节数组。你不应该比较文档,你应该比较二进制文件。使用随机数据和大小创建文件,然后进行二进制比较,查看失败的地方。如果它没有失败,那可能不是加密。@MaartenBodewes我不认为他在比较文档,我认为他在试图解密原始文件时得到了损坏的数据。我看不出有什么明显的问题。文件中的垃圾字节在哪里(开始/结束/中间)?这些是额外的字节还是替换了原始文件中的其他字节?我看不到有人试图确保两侧的编码是相同的。也许这就是问题所在。另外,你在哪里读取与加密相同的salt来解密?我看到你使用的是一个空白字节数组。你不应该比较文档,你应该比较二进制文件。使用随机数据和大小创建文件,然后进行二进制比较,查看失败的地方。如果它没有失败,那可能不是加密。@MaartenBodewes我不认为他在比较文档,我认为他在试图解密原始文件时得到了损坏的数据。