zipbase64同一个明文有两个密文?

密文(其实也不算密文,就是zip之后的base64串)是对手系统给过来的报文demo里面的,我拿出来用自己的方法能解出来明文,但是用同样的方法加出来的密文发过去之后对方死活解不出来。所以我又把相同的明文又加了一边密发现我自己的加密密文跟对方demo里面的密文不一样,连长度都差了很多,但是明文是一样的。刚开始我用eclipse,发现我eclipse的编码格式是GBK,所以又改成用idea,把编码格式改成UTF-8之后还是老样子,仍然加出来的密文不一样。因为base64我没有深入研究过,以为是不同设备加出来的密文有可能不一样,又叫同事用他的电脑加出来一份密文,发现我俩加出来的密文是一摸一样的,所以有没有研究过base64的大佬来解释一下是怎么回事。
上代码

package ZipUtil;

import org.apache.axiom.om.util.Base64;

public class main {
	public static void main(String args[]) throws Exception {
		String data = "UEsDBBQACAgIAL10b00AAAAAAAAAAAAAAAADAAAAUlBDsylILErMLbazySsuKk7KsHu+a/PzFX1Pdu16sb7tacfqp/3b3+/pebq/9eneqTb6UDU2+lBNAFBLBwgqiVDyNwAAAD0AAABQSwECFAAUAAgICAC9dG9NKolQ8jcAAAA9AAAAAwAAAAAAAAAAAAAAAAAAAAAAUlBDUEsFBgAAAAABAAEAMQAAAGgAAAAAAA==";
		System.out.println("密文:"+ data);
		byte[] data_ys = Base64.decode(data);
		String data_zm = ZipUtil.decompress(data_ys, "UTF-8");
		System.out.println("明文:"+ data_zm);
		data_ys = ZipUtil.compress(data_zm);
		data_zm = Base64.encode(data_ys);
		System.out.println("密文:"+ data_zm);
		data_ys = Base64.decode(data_zm);
		data_zm = ZipUtil.decompress(data_ys, "UTF-8");
		System.out.println("明文:"+ data_zm);


	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
package ZipUtil;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
    /**
     * 压缩字符串为 byte[] 储存可以使用new sun.misc.BASE64Encoder().encodeBuffer(byte[] b)方法
     * 保存为字符串
     *
     * @param str 压缩前的文本
     * @return
     */
    public static final byte[] compress(String str) {
        if (str == null) {
            return null;
        }
        byte[] compressed;
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;
        try {
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("0"));
            zout.write(str.getBytes());
            zout.closeEntry();
            compressed = out.toByteArray();
        } catch (IOException e) {
            compressed = null;
        } finally {
            if (zout != null) {
                try {
                    zout.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return compressed;
    }

    /**
     * 将压缩后的 byte[] 数据解压缩
     *
     * @param compressed 压缩后的 byte[] 数据
     * @return 解压后的字符串
     */
    public static final String decompress(byte[] compressed, String encode) {
        if (compressed == null) {
            return null;
        }
        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed;
        try {
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressed);
            zin = new ZipInputStream(in);
            ZipEntry entry = zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString(encode);
        } catch (IOException e) {
            decompressed = null;
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return decompressed;
    }

    //解压byte[]返回byte[]
    public static final byte[] decompress2(byte[] compressed) {
        if (compressed == null) {
            return null;
        }
        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        byte[] decompressed;
        try {
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressed);
            zin = new ZipInputStream(in);
            ZipEntry entry = zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toByteArray();
        } catch (IOException e) {
            decompressed = null;
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return decompressed;
    }

    //压缩byte[] 返回byte[]
    public static final byte[] compress2(byte[] str) {
        if (str == null) {
            return null;
        }
        byte[] compressed;
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;
        try {
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("0"));
            zout.write(str);
            zout.closeEntry();
            compressed = out.toByteArray();
        } catch (IOException e) {
            compressed = null;
        } finally {
            if (zout != null) {
                try {
                    zout.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return compressed;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179

运行结果:
密文:UEsDBBQACAgIAL10b00AAAAAAAAAAAAAAAADAAAAUlBDsylILErMLbazySsuKk7KsHu+a/PzFX1Pdu16sb7tacfqp/3b3+/pebq/9eneqTb6UDU2+lBNAFBLBwgqiVDyNwAAAD0AAABQSwECFAAUAAgICAC9dG9NKolQ8jcAAAA9AAAAAwAAAAAAAAAAAAAAAAAAAAAAUlBDUEsFBgAAAAABAAEAMQAAAGgAAAAAAA==
明文:纳税人识别号,必录
密文:UEsDBBQACAgIAABJQVIAAAAAAAAAAAAAAAABAAAAMLMpSCxKzC22s8krLipOyrB7vmvz8xV9T3bterG+7WnH6qf929/v6Xm6v/Xp3qk2+lA1NvpQTQBQSwcIKolQ8jcAAAA9AAAA
明文:纳税人识别号,必录

对于BASE64,同样的明文,编码后的结果肯定是相同的。

        String data = "UEsDBBQACAgIAL10b00AAAAAAAAAAAAAAAADAAAAUlBDsylILErMLbazySsuKk7KsHu+a/PzFX1Pdu16sb7tacfqp/3b3+/pebq/9eneqTb6UDU2+lBNAFBLBwgqiVDyNwAAAD0AAABQSwECFAAUAAgICAC9dG9NKolQ8jcAAAA9AAAAAwAAAAAAAAAAAAAAAAAAAAAAUlBDUEsFBgAAAAABAAEAMQAAAGgAAAAAAA==";
        System.out.println("密文:"+ data);
        byte[] data_ys = Base64.decode(data);
        System.out.println(PubUnit.bytesToHexString(data_ys));
        byte[] data_zm = Base64.encode(data_ys);
        System.out.println(new String(data_zm));

你可以试试,肯定是一样的。
你描述中的问题,肯定是出在加密和解密这个阶段,也就是说,有可能明文相同,加密结果不同。这个是很常见的,例如所有的非对称加密,都有这个现象。 你可以做一个测试,用你的代码,对同一个明文多次加密(不是BASE64编码),看看结果是否相同。