英文:
Crypto-JS on typescript: Type 'string' is not assignable to type 'WordArray'. ts(2322)
问题
I keep encountering a problem when using Crypto-JS with Typescript. The error is that Type 'string' is not assignable to type 'WordArray'. ts(2322)
on the iv. Here's my code
encryption.tsx
import { FC } from "react";
import CryptoJS, { AES } from "crypto-js";
const Encryption: FC = () => {
const application = {
key: "key",
iv: "iv"
};
const encrypt = (data: string) =>
AES.encrypt(data, application.key, {
iv: application.iv, // here's the error
}).toString();
const decrypt = (data: string) =>
AES.decrypt(data, application.key, { iv: application.iv }).toString(
CryptoJS.enc.Utf8
);
useEffect(()=>{
const data = "hi";
console.log({encrypted: encrypt(data), decrypted: decrypt(encrypt(data))});
},[]);
return <></>;
}
英文:
I keep encountering a problem when using Crypto-JS with Typescript. The error is that Type 'string' is not assignable to type 'WordArray'. ts(2322)
on the iv. Here's my code
encryption.tsx
import { FC } from "react";
import CryptoJS, { AES } from "crypto-js";
const Encryption: FC = () => {
const application = {
key: "key",
iv: "iv"
};
const encrypt = (data: string) =>
AES.encrypt(data, application.key, {
iv: application.iv, // here's the error
}).toString();
const decrypt = (data: string) =>
AES.decrypt(data, application.key, { iv: application.iv }).toString(
CryptoJS.enc.Utf8
);
useEffect(()=>{
const data = "hi";
console.log({encrypted: encrypt(data), decrypted: decrypt(encrypt(data))});
},[]);
return <></>;
}
答案1
得分: 1
iv
应具有特定格式。例如,CryptoJS.enc.Hex.parse("101112131415161718191a1b1c1d1e1f");
英文:
It's expecting for iv
to have a specific format.
For example CryptoJS.enc.Hex.parse("101112131415161718191a1b1c1d1e1f");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论