使用CryptoJS将64位数字字符串转换为单词数组 [英] Converting a 64 bit number string to word array using CryptoJS

查看:419
本文介绍了使用CryptoJS将64位数字字符串转换为单词数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道带有整数数据的字符串是否可以正确转换为CryptoJS单词数组? 例子.我可以像在175950736337895418(int值)中创建单词数组一样,将"175950736337895418"转换成单词数组.

I want to know if a string with integer data can be converted to a CryptoJS word array correctly? Example. Can I convert "175950736337895418" into a word array the same way I can create a word array out of 175950736337895418 (int value).

我有一些将整数值转换为单词数组的代码

I have some code that converts integer values to word array

 // Converts integer to byte array
 function getInt64Bytes( x ){
    var bytes = [];
    for(var i = 7;i>=0;i--){
        bytes[i] = x & 0xff;
        x = x>>8;
    }
    return bytes;
}

//converts the byte array to hex string
function bytesToHexStr(bytes) {
    for (var hex = [], i = 0; i < bytes.length; i++) {
        hex.push((bytes[i] >>> 4).toString(16));
        hex.push((bytes[i] & 0xF).toString(16));
    }
    return hex.join("");
}

// Main function to convert integer values to word array
function intToWords(counter){
    var bytes = getInt64Bytes(counter);
    var hexstr = bytesToHexStr(bytes);
    var words = CryptoJS.enc.Hex.parse(hexstr);
    return words;
}

即使此代码也无法正常工作,因为非常大的整数(超过2 ^ 53-1的javascript限制)会四舍五入.因此,我想要一个可以将整数值作为字符串并将其正确转换为单词数组的解决方案.

Even this code doesn't work correctly as very large integer numbers (exceeding javascript limit of numbers 2^53 - 1) get rounded off. Hence I wanted a solution that could take the integer value as string and convert it to a word array correctly.

PS.我需要这个字数组来使用以下代码计算HMAC值

PS. I need this word array to calculate the HMAC value using the following code

CryptoJS.HmacSHA512(intToWords(counter), CryptoJS.enc.Hex.parse(key))

推荐答案

您想要的是解析字符串中的大数字.由于这对于RSA是必需的,因此您可以使用Tom Wu的JSBN来获得该功能.确保包含jsbn.js jsbn2.js.然后,您可以像这样使用它:

What you want is to parse big numbers from strings. Since this is necessary for RSA, you can use Tom Wu's JSBN to get that functionality. Be sure to include jsbn.js and jsbn2.js. Then you can use it like this:

function intToWords(num, lengthInBytes) {
    var bigInt = new BigInteger();
    bigInt.fromString(num, 10); // radix is 10
    var hexNum = bigInt.toString(16); // radix is 16
    
    if (lengthInBytes && lengthInBytes * 2 >= hexNum.length) {
        hexNum = Array(lengthInBytes * 2 - hexNum.length + 1).join("0") + hexNum;
    }

    return CryptoJS.enc.Hex.parse(hexNum);
}

var num = "175950736337895418";
numWords = intToWords(num);

document.querySelector("#hexInt").innerHTML = "hexNum: " + numWords.toString();
document.querySelector("#hexIntShort").innerHTML = "hexNumShort: " + intToWords("15646513", 8).toString();

var key = CryptoJS.enc.Hex.parse("11223344ff");

document.querySelector("#result").innerHTML = "hexHMAC: " + 
        CryptoJS.HmacSHA512(numWords, key).toString();

<script src="https://cdn.rawgit.com/jasondavies/jsbn/master/jsbn.js"></script>
<script src="https://cdn.rawgit.com/jasondavies/jsbn/master/jsbn2.js"></script>
<script src="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/rollups/hmac-sha512.js"></script>
<div id="hexInt"></div>
<div id="hexIntShort"></div>
<div id="result"></div>

如果需要特定长度的结果,则可以将所需字节数作为第二个参数传递.

If you need the result in a specific length, then you can pass the number of required bytes as the second argument.

这篇关于使用CryptoJS将64位数字字符串转换为单词数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆