Crypto-JS on typescript: Type 'string' is not assignable to type 'WordArray'. ts(2322)

huangapple go评论74阅读模式
英文:

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 &#39;string&#39; is not assignable to type &#39;WordArray&#39;. ts(2322) on the iv. Here's my code

encryption.tsx

import { FC } from &quot;react&quot;;
import CryptoJS, { AES } from &quot;crypto-js&quot;;

const Encryption: FC = () =&gt; {
  const application = {
    key: &quot;key&quot;,
    iv: &quot;iv&quot;
  };

  const encrypt = (data: string) =&gt;
    AES.encrypt(data, application.key, {
      iv: application.iv, // here&#39;s the error
    }).toString();

  const decrypt = (data: string) =&gt;
    AES.decrypt(data, application.key, { iv: application.iv }).toString(
      CryptoJS.enc.Utf8
    );

  useEffect(()=&gt;{
     const data = &quot;hi&quot;;
     console.log({encrypted: encrypt(data), decrypted: decrypt(encrypt(data))});
  },[]);

  return &lt;&gt;&lt;/&gt;;
}

答案1

得分: 1

iv应具有特定格式。例如,CryptoJS.enc.Hex.parse("101112131415161718191a1b1c1d1e1f");

英文:

It's expecting for iv to have a specific format.

For example CryptoJS.enc.Hex.parse(&quot;101112131415161718191a1b1c1d1e1f&quot;);

huangapple
  • 本文由 发表于 2023年5月10日 14:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定