使用高级加密标准算法(AES)在Typescript中加密字符串并在C#中解密 [英] Encrypt the string In Typescript And Decrypt In C# using Advanced Encryption Standard Algorithm(AES)

查看:1104
本文介绍了使用高级加密标准算法(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

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