C# java和Wp7中的压缩和加密不匹配

C# java和Wp7中的压缩和加密不匹配,c#,java,.net,windows-phone-7,encryption,C#,Java,.net,Windows Phone 7,Encryption,我们已经在java和WP7中实现了Deflate压缩和AES-128加密 当压缩和加密作为seprate模块实现时,它们在两个平台上提供相同的输出 后来我们尝试集成这两个模块(即先压缩输入字符串,然后加密压缩字符串),在集成之后,我们在两个平台上得到的输出并不相同 代码如下所示 我们用作输入的字符串是:“测试WP7的压缩(Deflate Algorothjm)、加密(AES算法)” Java输出:“ijsblymvmmad94kdhsrlt1xlc3lionc4mntk4kobuk+qkbeiv

我们已经在java和WP7中实现了Deflate压缩和AES-128加密

当压缩和加密作为seprate模块实现时,它们在两个平台上提供相同的输出

后来我们尝试集成这两个模块(即先压缩输入字符串,然后加密压缩字符串),在集成之后,我们在两个平台上得到的输出并不相同

代码如下所示

我们用作输入的字符串是:“测试WP7的压缩(Deflate Algorothjm)、加密(AES算法)”

Java输出:“ijsblymvmmad94kdhsrlt1xlc3lionc4mntk4kobuk+qkbeiv4x9em7shpxdtxo1ekljepzvcevcevtzctsmwc5/uGzr4yscgLOvsYvBL8Ku0FM=”

WP7的输出:“muMPnBPUG6Ad+Zolhte7Mi9YWTGMM9DottWtTzp19orUCF0i02MihdWis488Y33EgaAZJQHD3YMedor+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;
    }
}
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();
            }
        }

    }
}
WP7集成代码

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;
    }
}
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();
            }
        }

    }
}
谢谢和问候 莫希特·利卡


正如我已经说过的,Seprate加密和压缩在两个模块上提供相同的输出

到目前为止,我发现:

使用“测试WP7的压缩(Deflate Algorothjm)、加密(AES算法)”作为输入字符串

压缩 我们使用 byte[]inputData=strData.getBytes(); 使用deflater(compressor)压缩这些字节(inputData),然后使用base64将compressd字节转换为strin,得到压缩的字节

但在WP7的情况下,平减指数提供流,而不是字节 但当我们在两个平台上得到相同的输出时

加密 我们按照相同的3个步骤(1.转换为字节,2.加密3.使用base64转换为字符串)进行加密,并从两个平台获得相同的输出

集成 问题从这里开始 在java代码中,我们将输入字符串转换为字节,并对其进行压缩。 压缩程序提供字节ARAY,该字节直接传递给加密模块 即,跳过了压缩的第三步(将压缩字节转换为base64字符串)和加密的第一步(将srting转换为字节)

当我们在WP7中这样做时,我们从压缩器中得到一个流,我们将它转换为字节数组并传递给加密模块,这里我们得到的输出是不同的

我的问题是我的java代码已经定稿,无法修改

我有一个


谢谢

我相信这是你的问题——或者至少是一个问题:

您正在写入MemoryStream(在Deflate中),然后从中读取,而无需将“光标”重置为开始。我怀疑
DeflaterOutputStream
将处理
MemoryStream
之后,您也在尝试读取

幸运的是,你根本不必这么做。只需使用:

byte[] objByte = memoryStream.ToArray();
即使流已关闭,此操作仍将有效


顺便说一句,我注意到您仍然在Java代码中使用
String.getBytes()
,而没有指定编码。请不要那样做。

我认为这是你的问题,或者至少是一个问题:

您正在写入MemoryStream(在Deflate中),然后从中读取,而无需将“光标”重置为开始。我怀疑
DeflaterOutputStream
将处理
MemoryStream
之后,您也在尝试读取

幸运的是,你根本不必这么做。只需使用:

byte[] objByte = memoryStream.ToArray();
即使流已关闭,此操作仍将有效


顺便说一句,我注意到您仍然在Java代码中使用
String.getBytes()
,而没有指定编码。请不要这样做。

谢谢您的回复,但问题是javs代码不是我的,我无法更改。我必须相应地更改wp7代码以获得相同的输出。这已经成为一项主要任务,我们已经尝试过byte[]objByte=memoryStream.ToArray();但它不起作用,所以我们尝试下面的选项。memoryStream=Deflate(getBytes);在这两种情况下,我们得到了相同的输出。byte[]objByte=新字节[memoryStream.Length];memoryStream.Read(对象字节,0,(int)(memoryStream.Length));谢谢回复,但问题是javs代码不是我的,我不能更改,我必须相应地更改我的wp7代码以获得相同的输出。这已经成为一项主要任务,我们已经尝试过byte[]objByte=memoryStream.ToArray();但它不起作用,所以我们尝试下面的选项。memoryStream=Deflate(getBytes);在这两种情况下,我们得到了相同的输出。byte[]objByte=新字节[memoryStream.Length];memoryStream.Read(对象字节,0,(int)(memoryStream.Length));