javascript - 使用 Cryptojs 时数组长度无效
问题描述
我正在尝试使用cryptojs将ac#代码转换为javascript,并在c#代码中使用TripleDESCryptoServiceProvider。除了加密部分之外,我可以在我的 javascript 代码中获得与 C# 完全相同的所有值。我收到“无效数组长度”错误这是整个错误消息:
"RangeError: Invalid array length
at WordArray.init.clamp (http://localhost:8100/auth-login-login-module.js:1392:27)
at WordArray.init.concat (http://localhost:8100/auth-login-login-module.js:1357:19)
at Object.pad (http://localhost:8100/auth-login-login-module.js:652:19)
at Object._doFinalize (http://localhost:8100/auth-login-login-module.js:729:26)
at Object.finalize (http://localhost:8100/auth-login-login-module.js:400:44)
at Object.encrypt (http://localhost:8100/auth-login-login-module.js:912:41)
at Object.encrypt (http://localhost:8100/auth-login-login-module.js:438:59)
at AuthService.encryptText (http://localhost:8100/auth-login-login-module.js:6745:83)
at LoginPage.ngOnInit (http://localhost:8100/auth-login-login-module.js:6939:26)
at checkAndUpdateDirectiveInline (http://localhost:8100/vendor.js:65455:19)"
请在 c# 和 javascript 上查看我的代码。
C#
public static string EncryptTxt(string key, string msg, CipherMode mode, Int16 KeyOffSet)
{
SHA512CryptoServiceProvider sha = new SHA512CryptoServiceProvider();
byte[] newKey = new byte[23];
using (var tdes = new TripleDESCryptoServiceProvider())
{
byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(key));
TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
byte[] keybyte = sha.ComputeHash(Encoding.UTF8.GetBytes(key));
byte[] newKeyx = new byte[24];
Array.Copy(keybyte, KeyOffSet, newKeyx, 0, newKeyx.Length);
TDESAlgorithm.Key = newKeyx;
TDESAlgorithm.Mode = mode;
TDESAlgorithm.Padding = PaddingMode.PKCS7;
byte[] DataToEncrypt = UTF8.GetBytes(msg);
try
{
ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
}
finally
{
TDESAlgorithm.Clear();
HashProvider.Clear();
}
return Convert.ToBase64String(Results);
}
javascript
encryptText = () => {
debugger;
const msg = 'xxx:juan:201910181809:12345678';
let key = crypto.enc.Utf8.parse('xxx');
key = crypto.MD5(key);
key.words.push(key.words[0], key.words[1]);
const iv = crypto.enc.Utf8.parse('xxx');
// MD5CryptoServiceProvider
const hashProvider = crypto.MD5(iv);
const TDESKey = this.wordArrayToByteArray(hashProvider, 8);
const keybyte = this.wordArrayToByteArray(crypto.SHA512(iv), 16);
const newKeyx = new Uint8Array(24);
const newkeybyte = keybyte.slice(10, 34);
Object.assign(newKeyx, newkeybyte);
const TDESAlgorithmKey = newkeybyte;
const DataToEncrypt = this.wordArrayToByteArray(crypto.enc.Utf8.parse(msg), 40);
const dteLength = DataToEncrypt.length;
const encrypted = crypto.TripleDES.encrypt(DataToEncrypt, key, {
keySize: dteLength,
mode: crypto.mode.ECB,
padding: crypto.pad.Pkcs7,
algo: TDESAlgorithmKey
});
const result = this.wordArrayToByteArray(encrypted.ciphertext, dteLength);
console.log(encrypted);
return encrypted;
}
wordToByteArray(word: any, length: any) {
const ba = [], xFF = 0xFF;
if (length > 0) {
// tslint:disable-next-line:no-bitwise
ba.push(word >>> 24);
}
if (length > 1) {
// tslint:disable-next-line:no-bitwise
ba.push((word >>> 16) & xFF);
}
if (length > 2) {
// tslint:disable-next-line:no-bitwise
ba.push((word >>> 8) & xFF);
}
if (length > 3) {
// tslint:disable-next-line:no-bitwise
ba.push(word & xFF);
}
return ba;
}
你能告诉我如何正确地做到这一点。对此,我真的非常感激!
解决方案
当您传递对象(其他数据类型而不是字符串)时会触发此错误。在传递参数之前将其转换为字符串。
推荐阅读
- python - 如何将图例添加到 X,Y 图
- python - LibreOffice/OpenOffice 能否以编程方式将密码添加到现有的 .docx/.xlsx/.pptx 文件?
- php - 为什么 CI 中不显示验证码?
- python - 刮掉一些子链接,然后回到主刮
- ruby-on-rails - 在 RSpec 中运行 Selenium + Headless Chrome 时如何禁用 cookie
- python - 线程执行的函数的return语句会自动杀死线程吗?
- docker - 如何在 docker 内公开在 localhost 上运行的应用程序?
- wordpress - 我在 facebook 上从 wp 嵌入帖子时遇到问题
- c++ - 不带括号的宏
- android - 在 RecyclerView 项内滚动 RecyclerView