Java 到 JavaScript(加密相关)
我在 Javascript 中获取相同的字符串时遇到困难,我认为我做错了什么...
Java 代码:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.GregorianCalendar;
import sun.misc.BASE64Encoder;
private static String getBase64Code(String input) throws
UnsupportedEncodingException, NoSuchAlgorithmException {
String base64 = "";
byte[] txt = input.getBytes("UTF8");
byte[] text = new byte[txt.length+3];
text[0] = (byte)239;
text[1] = (byte)187;
text[2] = (byte)191;
for(int i=0; i<txt.length; i++)
text[i+3] = txt[i];
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(text);
byte digest[] = md.digest();
BASE64Encoder encoder = new BASE64Encoder();
base64 = encoder.encode(digest);
return base64;
}
我正在尝试使用 Paul 的 MD5 脚本 以及 Farhadi Base 64 编码脚本
,但我的测试完全失败:(
我的代码:
function CalculateCredentialsSecret(type, user, pwd) {
var days = days_between(new Date(), new Date(2000, 1, 1));
var str = type.toUpperCase() + user.toUpperCase() + pwd.toUpperCase() + days;
var padding_data = String.fromCharCode(239) +
String.fromCharCode(187) +
String.fromCharCode(191);
var md5 = hex_md5(padding_data + str);
var b64 = base64Encode(md5);
return encodeURIComponent(b64);
}
有谁知道如何将这个 Java 方法转换为 Javascript 方法?
谢谢
测试(今天(2010 年 9 月 29 日),2000 年 1 月 1 日后3740 天)
var secret = CalculateCredentialsSecret('AAA', 'BBB', 'CCC');
// secret SHOULD be: S3GYAfGWlmrhuoNsIJF94w==
I'm having difficulties to get the same string in Javascript and I'm thinking that I'm doing something wrong...
Java code:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.GregorianCalendar;
import sun.misc.BASE64Encoder;
private static String getBase64Code(String input) throws
UnsupportedEncodingException, NoSuchAlgorithmException {
String base64 = "";
byte[] txt = input.getBytes("UTF8");
byte[] text = new byte[txt.length+3];
text[0] = (byte)239;
text[1] = (byte)187;
text[2] = (byte)191;
for(int i=0; i<txt.length; i++)
text[i+3] = txt[i];
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(text);
byte digest[] = md.digest();
BASE64Encoder encoder = new BASE64Encoder();
base64 = encoder.encode(digest);
return base64;
}
I'm trying this using Paul's MD5 script as well Farhadi Base 64 Encode script
but my tests fail completely :(
my code:
function CalculateCredentialsSecret(type, user, pwd) {
var days = days_between(new Date(), new Date(2000, 1, 1));
var str = type.toUpperCase() + user.toUpperCase() + pwd.toUpperCase() + days;
var padding_data = String.fromCharCode(239) +
String.fromCharCode(187) +
String.fromCharCode(191);
var md5 = hex_md5(padding_data + str);
var b64 = base64Encode(md5);
return encodeURIComponent(b64);
}
Does anyone know how can I convert this Java method into a Javascript one?
Thank you
Tests (for today (29-09-2010), 3740 days after January 1st, 2000)
var secret = CalculateCredentialsSecret('AAA', 'BBB', 'CCC');
// secret SHOULD be: S3GYAfGWlmrhuoNsIJF94w==
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://pajhome.org.uk/crypt/md5/ <-- 获取来自那里的 Md5 函数(Google 上“javascript md5”的第一页)
http://www.webtoolkit.info/javascript-base64.html <- - 获取base64 en/de代码(谷歌搜索'javascript base64编码')
刚刚意识到......如果您想要做的只是在base64中endocde来传输数据,请查看第二个链接。可以按原样做你想做的事。
更新:
您应该能够执行类似的操作,假设这些额外的字节可以替换三个字符。
http://pajhome.org.uk/crypt/md5/ <-- get Md5 function from there (first page on google for 'javascript md5')
http://www.webtoolkit.info/javascript-base64.html <-- get base64 en/de code (googled 'javascript base64 encode')
Just realised... if all you want to do is endocde in base64 for transporting the data, look at the second link. may do what you want as it is.
UPDATE:
you should be able to do something like this then, assume those extra bytes could replaced a three characters.
正确的代码是
correct code is