可以正确编码 ISO-8859-1 MD5 字节



1.字符串中有一些数据:

String data = "some......";

2.并使用MD5将其隐蔽为字节:

byte [] result = MD5.toMD5(data);

3.现在我将其编码为字符串:

String encodeString = new String(result,"ISO-8895-1");

4.然后将其解码为字节:

byte [] decodeBytes = encodeString.getBytes("ISO-8859-1");

我的问题是:decodeBytes会等于result吗?

我的困惑是result是否会有Zero,是否会导致步骤3截断

如果有任何问题让decodeBytes等于result,如果我在Step1中限制字符串的数据类型,例如只允许字母和数字,是否可以避免该问题?

如果 ISO-8859-1(即 8 位字符代码),则没有理由不将字节值解码为字符。尽管不包括 65 个代码点(用于控制字符),但 String 方法处理这些代码点时,就好像 ISO/IEC 6429 中定义的代码点是该字符集的一部分一样。

往返字节值 0 到 255 可以完美工作,也可以使用 byte[]。

byte[] bs = new byte[256];
String encode() throws Exception {
    return new String( bs, "ISO-8859-1" );
}
byte[] decode( String s ) throws Exception{
    return s.getBytes( "ISO-8859-1" );
}
 void set(){
    for( int i = 0; i < bs.length; ++i ){
        bs[i] = (byte)i;
    }
}
boolean cmp( byte[] x ){
    for( int i = 0; i < bs.length; ++i ){
        if( bs[i] != x[i] ){
            System.out.println( i + ": " + bs[i] + " != " + x[i] );
            return false;
         }
    }
    return true;
}
void round() throws Exception{
    String s = encode();
    if( s.length() != 256 ) throw new IllegalStateException();
        byte[] res = decode( s );
        if( ! cmp( res ) ) System.out.println( "false" );
    }
}

最新更新