java和Wp7中的压缩和加密不匹配
本文关键字:加密 不匹配 压缩 Wp7 java | 更新日期: 2023-09-27 18:05:54
我们已经在java和WP7中实现了Deflate压缩和AES-128加密。
当压缩和加密作为单独的模块实现时,它们在两个平台上提供相同的输出。
后来我们尝试整合两个模块(即。输入字符串首先被压缩,然后压缩字符串被加密),在这个集成之后,我们在两个平台上得到的输出不会相同。
代码如下
我们用作输入的字符串是:"测试WP7的压缩(Deflate算法),加密(AES算法)。"
Java的输出:"iJSblymVMAD94KdHSrLt1xLC3lioNc4mNtk4kobuk+qkBEiV4x9Em7ShPxdtXO1EKLjepZVcEvtzCTsmwc5/uGzr4yscgLOvsYvBL8Ku0FM="
WP7的输出:"muMPnBPUG6Ad+zolHtE7Mi9YWTgmm9dOtTWTtZP19oRucf0I02MiHdwIs488y33EGaaZJjQhd3ymaEDOR+HvVb9quQvRu7nkJwXW3uyZEcI="
Java集成代码
package com.emap.services;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.zip.Deflater;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class CompressEncrypt {
static byte[] ibv = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10};
public String compressEncryptData() {
String strData = "Testing compression(Deflate Algorothjm), encrption(AES algorithm) for WP7.";
byte[] inputData = strData.getBytes();
// supporting maximum 10 MB of data
byte[] outputData = new byte[1024 * 1024 * 10];
// initiate compressor instance
Deflater compresser = new Deflater();
// compressed base64 encoded string
byte [] compressedByte = null;
int compressedDataLength = 0;
//Encrypted String
String encryptedStr = "";
try {
compresser.setInput(inputData, 0, inputData.length);
compresser.finish();
compressedDataLength = compresser.deflate(outputData);
compressedByte = Arrays.copyOfRange(outputData, 0, compressedDataLength);
System.out.println("Compressed String is : " + compressedByte.toString());
} catch (Exception ex) {
System.out.println("Error : " + ex.getMessage());
}
try {
SecretKeySpec skeySpec = new SecretKeySpec("E2D5@eMap_AndIBB".getBytes(), "AES");
IvParameterSpec iv = new IvParameterSpec(ibv);
// Instantiate the cipher
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(compressedByte);
System.out.println("Encrypted String is : " + encrypted.toString());
encryptedStr = Base64.encode(encrypted);
} catch (Exception ex) {
System.out.println("No Such Padding Error: " + ex.getMessage());
encryptedStr = "No Such Padding Error: " + ex.getMessage();
}
System.out.println("Compressed Encrypted Encoded String is : " + encryptedStr);
return encryptedStr;
}
}
WP7集成代码
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using System.Text;
using System.Security;
using System.Security.Cryptography;
namespace CompressEncrypt
{
public class Code
{
static byte[] ibv = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10};
//public string CompressEncode(string strOrigianl)
public string CompressEncode(string strOrigianl)
{
MemoryStream memoryStream = new MemoryStream();
byte[] getBytes = System.Text.Encoding.UTF8.GetBytes(strOrigianl);
memoryStream = Deflate(getBytes);
byte[] objByte = new byte[memoryStream.Length];
memoryStream.Read(objByte, 0, (int)(memoryStream.Length));
string strResult = Convert.ToBase64String(memoryStream.ToArray());
strResult = Encrypt(memoryStream.ToArray(), "E2D5@eMap_AndIBB");
return strResult;
}
MemoryStream Deflate(byte[] data)
{
MemoryStream memoryStream = new MemoryStream();
{
Deflater deflater = new Deflater();
using (DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater))
{
outStream.IsStreamOwner = false;
outStream.Write(data, 0, data.Length);
outStream.Flush();
outStream.Finish();
}
return memoryStream;
}
}
//********************Encryption*************************************
public string Encrypt(byte[] data, string password)
{
AesManaged aes = null;
MemoryStream memStream = null;
CryptoStream crStream = null;
try
{
aes = new AesManaged();
aes.Key = Encoding.UTF8.GetBytes(password);
aes.IV = ibv;
memStream = new MemoryStream();
crStream = new CryptoStream(memStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
//byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt);
crStream.Write(data, 0, data.Length);
crStream.FlushFinalBlock();
//Return Base 64 String
return Convert.ToBase64String(memStream.ToArray());
}
finally
{
//cleanup
if (crStream != null)
crStream.Close();
if (memStream != null)
memStream.Close();
if (aes != null)
aes.Clear();
}
}
}
}
谢谢Mohit Leekha
正如我已经说过的单独的加密和压缩在两个模块上提供相同的输出。
我现在发现的是:
使用"测试WP7的压缩(Deflate算法),加密(AES算法)"作为输入字符串
压缩将字符串转换为字节byte[] inputData = strData.getBytes();然后使用deflater(压缩器)压缩这些字节(inputData),我们得到压缩字节,在我们使用base64
将压缩字节转换为字符串后,但在WP7平减器的情况下提供流,而不是字节当我们在两个平台上得到相同的输出时。
同样的3个步骤(1。转换为字节,2。加密3。使用base64转换为字符串)进行加密,并从两个平台获得相同的输出
问题从这里开始在java代码中,我们将输入字符串转换为字节,并对其进行压缩。压缩器提供字节数组,直接传递给加密模块ie。跳过压缩的第三步(将压缩字节转换为base64字符串)和加密的第一步(将字符串转换为字节)
当我们在WP7中这样做时,我们从压缩器得到一个流,我们将其转换为字节数组并传递给加密模块,这里我们得到的输出是不同的。
我的问题是我的java代码已经完成,不能修改。
我有
谢谢
我相信这是你的问题——或者至少是一个问题:
memoryStream = Deflate(getBytes);
byte[] objByte = new byte[memoryStream.Length];
memoryStream.Read(objByte, 0, (int)(memoryStream.Length));
你正在写内存流(在Deflate中),然后从它读取,而不将"光标"重置到开始。我怀疑,您也试图在MemoryStream
将被DeflaterOutputStream
处理后读取。
幸运的是,您根本不需要这样做。只使用:
byte[] objByte = memoryStream.ToArray();
即使流是关闭的,也可以工作。
顺便说一下,我注意到您仍然在Java代码中使用String.getBytes()
而没有指定编码。请不要那样做