怎么把下面一段c#代码转化成java代码?
string_input_charset=“utf-8”;//编码格式privatestringSign(stringprestr,stringkey){StringBu...
string _input_charset = “utf-8”; //编码格式
private string Sign(string prestr,string key)
{
StringBuilder sb=new StringBuilder(32);
prestr = prestr+key;
MD5 md5 = new MD5CrypttoServiceProvider();
byte[] t= md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
for(int i=0;i<t.Length;i++)
{
sb.Append(t[i].ToString(“x”).PadLeft(2,’0’));
}
return sb.ToString();
} 展开
private string Sign(string prestr,string key)
{
StringBuilder sb=new StringBuilder(32);
prestr = prestr+key;
MD5 md5 = new MD5CrypttoServiceProvider();
byte[] t= md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
for(int i=0;i<t.Length;i++)
{
sb.Append(t[i].ToString(“x”).PadLeft(2,’0’));
}
return sb.ToString();
} 展开
1个回答
2015-08-24
展开全部
import java.security.*;
public class Yugi {
public static final String CHARSET = "UTF-8";
private String sign(String prestr, String key) {
prestr += key;
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] t = prestr.getBytes(CHARSET);
return md5.digest(t);
}
}
追问
程序有错误,可以给个完整的吗
追答
package com;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Yugi
{
public static final String CHARSET = "UTF-8";
private String sign(String prestr)
{
StringBuffer buffer = new StringBuffer(32);
try
{
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] t = md5.digest(prestr.getBytes(CHARSET));
for(int i = 0; i < t.length; i++)
{
buffer.append(Byte.toString(t[i]));
}
return buffer.toString();
}
catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
return prestr;
}
public static void main(String[] args)
{
Yugi yugi = new Yugi();
System.out.println(yugi.sign("123456"));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询