【发布时间】:2010-07-19 20:28:57
【问题描述】:
我需要通过 POST 向服务器发送电子邮件(字符串)和密码(字节 [] 中的 MD5 哈希)。
下面我如何获得我的 MD5 哈希,其中“密码”是一个字符串(用户输入的内容):
byte[] passMD5 = Crypto.encodeStringMD5(password);
以及功能:
public static byte[] encodeStringMD5(String s) throws Exception {
byte[] bytes = s.getBytes();
MD5Digest digest = new MD5Digest();
digest.update(bytes, 0, bytes.length);
int length = digest.getDigestLength();
byte[] md5 = new byte[length];
digest.getDigest(md5, 0, true);
return md5;
}
所以“passMD5”应该是我的字符串值“password”的 MD5 哈希值,对吧?
然后我需要通过 HTTP POST 将信息发送到 URL 并读取结果 (XML)。其余代码见下文:
readURL(urlTemplate, email, passMD5);
其中 urlTemplate 是像“http://www.domain.com/myfile.aspx?action=login&enc=1”这样的字符串,通过电子邮件发送字符串并以字节为单位为 MD5 散列密码。
下面的readURL:
private void readURL(String url, String email, byte[] pass) throws IOException {
HttpConnection conn = null;
InputStream in = null;
OutputStream os = null;
byte dataBytes[];
try {
URLEncodedPostData data = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
data.append("email", email);
data.append("pass", pass.toString());
dataBytes = data.getBytes();
conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("Content-Type", data.getContentType());
conn.setRequestProperty("Content-Length", Integer.toString(dataBytes.length));
os = conn.openOutputStream();
os.write(dataBytes);
os.flush();
os.close();
in = conn.openInputStream();
verifyLogin(getLoginContent(in));
} catch (IOException e) {
} catch (IllegalArgumentException e) {
} finally {
ConnectionUtil.close(conn, in);
ConnectionUtil.close(conn, os);
}
}
所以现在将密码的 MD 哈希转换为字符串,以便添加到只接受字符串参数的 data.append() 函数中...... 我认为正因为如此,我没有发送好的 MD5 哈希,这会产生问题。
在 ASP.NET C# 的服务器端,我有这个:
byte[] PasswordHash;
if (enc == 0) {
MD5 MD5Hasher = MD5.Create();
PasswordHash = MD5Hasher.ComputeHash(Encoding.Unicode.GetBytes(Password));
} else {
PasswordHash = Encoding.Unicode.GetBytes(Password);
}
因此,当我询问此 URL“http://www.domain.com/myfile.aspx?action=login&enc=0”并按原样提供密码(所以是字符串,而不是字节 [] 而不是 MD5 哈希)并执行时
data.append("pass", password);
然后就可以了。
我只是在创建 MD5 哈希或 HTTP POST 或两者都有问题... 请帮帮我!
【问题讨论】:
-
在 encodeStringMD5 中尝试将 byte[] 变成 BigInteger,然后使用 toString(16)
标签: blackberry hash md5 http-post