使用高级加密标准算法(AES)在Typescript中加密字符串并在C#中解密

使用高级加密标准算法(AES)在Typescript中加密字符串并在C#中解密

问题描述:

我很难在Typescript中实现加密并在C#中实现解密。在这里发布问题之前,我先用Google进行了搜索,然后找到了一些链接,但这些链接与JavaScript相关,而不是与打字稿无关。

I am having struggle to implement the encryption in typescript and decryption in C#. Before posting question here, I did Google it and find some links but those links are related to JavaScript not a typescript.

使用JavaScript加密并使用AES算法在C#中解密

使用angular2中的cryptojs库对文本进行加密

如何在Angular 2中导入非核心npm模块,例如(使用加密库)?

我遵循了上面的链接,以在当前应用程序中实现加密/解密概念。

I followed the above links, for implementing the encryption/decryption concept in my current application.

这是我在 myservice.ts

    //import { CryptoJS } from 'node_modules/crypto-js/crypto-js.js';
    //import 'crypto-js';
    import * as CryptoJS from 'crypto-js';


    var key = CryptoJS.enc.Utf8.parse('7061737323313233');
    var iv = CryptoJS.enc.Utf8.parse('7061737323313233');
    var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse("It works"), key,
        {
            keySize: 128 / 8,
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });

    var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
        keySize: 128 / 8,
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });

    console.log('Encrypted :' + encrypted);
    console.log('Key :' + encrypted.key);
    console.log('Salt :' + encrypted.salt);
    console.log('iv :' + encrypted.iv);
    console.log('Decrypted : ' + decrypted);
    console.log('utf8 = ' + decrypted.toString(CryptoJS.enc.Utf8));

在我在 myservice.ts 中添加以上代码行之前,我在 package.json 文件中将依赖项添加为 crypto-js: ^ 3.1.9-1

Before I added the above lines of code in myservice.ts, I added the dependency as "crypto-js": "^3.1.9-1" in package.json file.

在package.json中添加以上依赖项之后,我就成功地恢复了软件包。但是CryptoJS仍然在myservice.ts中显示错误,例如找不到名称为CryptoJS

After added the above dependency in package.json, then I was restored the packages successfully. But still also CryptoJS shows error in myservice.ts like can not found name as CryptoJS.

您能告诉我如何导入CryptoJS吗?

Can you please tell me how to import the CryptoJS from node modules and also tell me how to encrypt the string in typescript and decrypt the same string in C# using Advanced Security Algorithm (AES)?

Pradeep

我遇到了类似的问题。我正在使用Angular 4 / Angular-Cli 1.0.0。对我有用的东西:

I had a similar issue. I'm using Angular 4/Angular-Cli 1.0.0. What worked for me:

npm install crypto-js --save
npm install @types/crypto-js --save

在这两个命令之后,请参考下面的 crypto-js

After these two commands, reference the crypto-js library in the angular-cli.json file, in the "scripts" array. In my case:

"scripts": [
    "../node_modules/crypto-js/crypto-js.js"
  ]

您会注意到,在 node_modules / @类型目录,您将拥有一个crypto-js子目录。因此,请在代码中使用node_modules/@types/crypto-js/index.d.ts 文件/handbook/triple-slash-directives.html rel = noreferrer>三斜杠指令,因此,编译器知道键入文件对于编译该模块文件是必需的:

You'll notice that in the node_modules/@types directory, you'll have a crypto-js subdirectory. So put a reference to the node_modules/@types/crypto-js/index.d.ts file in your code, using a triple-slash directive, so the compliler knows that the typings file is necessary to compile that module file:

/// <reference path="relative_path_to_cypto_folder/index.d.ts" />

或者,您也可以使用 types属性代替 path,因为在node_modules / @ types内部的类型定义:

Alternatively, you can also use "types" attribute instead of "path", since you're referencing a typings definition inside node_modules/@types:

/// <reference types="crypto-js" />

之后,您可以按原样使用代码:

After that you can use your code exactly as it is:

/// <reference types="crypto-js" />

import * as CryptoJS from 'crypto-js';


var key = CryptoJS.enc.Utf8.parse('7061737323313233');
var iv = CryptoJS.enc.Utf8.parse('7061737323313233');
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse("It works"), key,
    {
        keySize: 128 / 8,
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });

var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
    keySize: 128 / 8,
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
});

console.log('Encrypted :' + encrypted);
console.log('Key :' + encrypted.key);
console.log('Salt :' + encrypted.salt);
console.log('iv :' + encrypted.iv);
console.log('Decrypted : ' + decrypted);
console.log('utf8 = ' + decrypted.toString(CryptoJS.enc.Utf8));