qq238472837
qq238472837
2017-10-30 04:49

Python程序转成java程序输出结果不对

20
  • python
  • java

Python程序如下:

import base64
import hashlib
def encode_64(string,key):
    key = hashlib.md5(key.encode('utf-8')).hexdigest()
    print key
    key_length = len(key)
    string = hashlib.md5((string+key).encode('utf-8')).hexdigest()[0:8]+string
    print string
    rndkey = []
    box = []
    for i in range(0,256):
        rndkey.append(ord(key[i%key_length]))
        box.append(i)
    j = 0
    for i in range(0,256):
        j = (j+box[i]+rndkey[i])%256
        tmp = box[i]
        box[i] = box[j]
        box[j] = tmp
    a = 0
    j = 0
    result = ""
    for i in range(len(string)):
        a = (a+1)%256
        j = (j+box[a])%256
        tmp = box[a]
        box[a] = box[j]
        box[j] = tmp
        result += chr(ord(string[i])^(box[(box[a]+box[j])%256]))
    result = base64.b64encode(result)
    return result.replace('=','')
string = encode_64("passw0rd","toyou")
print string

java程序如下:

 package nimp;

import java.security.MessageDigest;
import sun.misc.BASE64Encoder;

public class JM {

    private static MessageDigest md5 = null;
    static {
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 用于获取一个String的md5值
     * @param string
     * @return
     */
    public static String getMd5(String str) {
        byte[] bs = md5.digest(str.getBytes());
        StringBuilder sb = new StringBuilder(40);
        for(byte x:bs) {
            if((x & 0xff)>>4 == 0) {
                sb.append("0").append(Integer.toHexString(x & 0xff));
            } else {
                sb.append(Integer.toHexString(x & 0xff));
            }
        }
        return sb.toString();
    }

    public static String enc(String string) throws Exception{
        String result = "";

        String key = getMd5("toyou");
        System.out.println(key);
        int key_length = key.length();
        char[] keyc = key.toCharArray();
        string = getMd5(string+key).substring(0, 8) + string;
        System.out.println(string);
        int string_length = string.length();
        int[] rndkey = new int[256];
        int[] box = new int[256];
        int i,j;
        for(i=0;i<=255;i++){
            rndkey[i] = (int)keyc[i%key_length];
            box[i] = i;
        }
        i = 0;j = 0;
        for(;i<=255;i++){
            j = (j+box[i]+rndkey[i])%256;
            int x = box[i];
            box[i] = box[j];
            box[j] = x;
        }
        int a=0;
        i = 0;j = 0;
        int[] r = new int[string_length];
        char[] re = new char[string_length];
        System.out.println(string_length);
        for(;i<string_length;i++){
            a = (a+1)%256;
            j = (j+box[a])%256;
            int x = box[a];
            box[a] = box[j];
            box[j] = x;
            int y = ((int)string.toCharArray()[i])^(box[(box[a]+box[j])%256]);
            r[i] = y;
            System.out.println(y);
            result += (char)y;
            re[i] = (char)y;
            System.out.println(re[i]);
        }
        System.out.println(result);
        System.out.println(String.valueOf(re));
        result = new BASE64Encoder().encode(result.getBytes("utf-8"));
        result.replace("=", "");
        return result;
    }

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        System.out.println(enc("passw0rd"));
    }

}

我自己测了测是从ASCII转字符那开始两个程序不同的,求解决办法

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答