Java 到 JavaScript(加密相关)

发布于 2024-08-27 10:33:19 字数 1942 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情别恋 2024-09-03 10:33:19

http://pajhome.org.uk/crypt/md5/ <-- 获取来自那里的 Md5 函数(Google 上“javascript md5”的第一页)
http://www.webtoolkit.info/javascript-base64.html <- - 获取base64 en/de代码(谷歌搜索'javascript base64编码')

function getBase64Code(input)
{
  base64 = "";
  txt = input
  text = [];
  text[0] = (byte)239; // These three lines I am stuck on
  text[1] = (byte)187; // These three lines I am stuck on
  text[2] = (byte)191; // These three lines I am stuck on

  for(int i=0; i<txt.length; i++)
  {
    text[i+3] = txt[i];
  }
  digest = hex_md5(text);
  base64 = Base64(digest);
  return base64;
}

刚刚意识到......如果您想要做的只是在base64中endocde来传输数据,请查看第二个链接。可以按原样做你想做的事。

更新:

您应该能够执行类似的操作,假设这些额外的字节可以替换三个字符。

encoded = Base64.encode(hexmd5(padding_data + data));

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')

function getBase64Code(input)
{
  base64 = "";
  txt = input
  text = [];
  text[0] = (byte)239; // These three lines I am stuck on
  text[1] = (byte)187; // These three lines I am stuck on
  text[2] = (byte)191; // These three lines I am stuck on

  for(int i=0; i<txt.length; i++)
  {
    text[i+3] = txt[i];
  }
  digest = hex_md5(text);
  base64 = Base64(digest);
  return base64;
}

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.

encoded = Base64.encode(hexmd5(padding_data + data));
绿萝 2024-09-03 10:33:19

正确的代码是

function CalculateCredentialsSecret(type, user, pwd) {

var days = days_between(new Date(), new Date(2000, 1, 1)) + 30;
var str = type.toUpperCase() + user.toUpperCase() + pwd.toUpperCase() + days;

var padding_data = String.fromCharCode(239, 187, 191);

var md5 = rstr_md5(padding_data+str);
var b64 = base64Encode(md5);

return encodeURIComponent(b64);
}

correct code is

function CalculateCredentialsSecret(type, user, pwd) {

var days = days_between(new Date(), new Date(2000, 1, 1)) + 30;
var str = type.toUpperCase() + user.toUpperCase() + pwd.toUpperCase() + days;

var padding_data = String.fromCharCode(239, 187, 191);

var md5 = rstr_md5(padding_data+str);
var b64 = base64Encode(md5);

return encodeURIComponent(b64);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文