qq238472837 2017-10-30 04:49 采纳率: 0%
浏览 1737
已采纳

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条回答 默认 最新

  • chapple_cnm 2017-10-31 08:05
    关注
    // String enc(String string)这里的编码改一下
    result = new BASE64Encoder().encode(result.getBytes("ISO8859-1")); 
    

    Python IDLE的默认编码一般是None,即ASCII编码,对应Java里的“ISO8859-1"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00