最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何使用AES加密在JavaScript中进行加密?

IT培训 admin 9浏览 0评论

如何使用AES加密在JavaScript中进行加密?

我在JAVA中实现了AES加密的以下功能,如何在javascript或nodeJs中转换它们?

public const string EncryptKey = "some key"
public const string IV = "some value";

 public static string Encryption(string PlainText)
{
    AesCryptoServiceProvider tdes = new AesCryptoServiceProvider();
    tdes.Key = Encoding.ASCII.GetBytes(EncryptKey);
    tdes.IV = Encoding.ASCII.GetBytes(IV);
    byte[] buffer = Encoding.ASCII.GetBytes(PlainText);
    string Value = Convert.ToBase64String(tdes.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
    return Value.Replace('+', '~');
}

public static string Decryption(string CypherText)
{
    CypherText = CypherText.Replace("~", "+");
    byte[] buffer = Convert.FromBase64String(CypherText);
    AesCryptoServiceProvider des = new AesCryptoServiceProvider();
    des.Key = Encoding.ASCII.GetBytes(EncryptKey);
    des.IV = Encoding.ASCII.GetBytes(IV);
    return Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
}

我不确定如何在javscript中转换tdes.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length),我已经转换了密钥,IV和以字节为单位的文本,并且也有一种方法可以转换为base64 encode

function encrypt(str) {
    // INIT
    const myString = str; // Utf8-encoded string

    // PROCESS
    const encryptedWord = CryptoJS.enc.Utf8.parse(myString);
    const encrypted = CryptoJS.enc.Base64.stringify(encryptedWord); 
    return encrypted;
}

function decrypt(str) {
    // INIT
    const encrypted = str; // Base64 encrypted string

    // PROCESS
    const encryptedWord = CryptoJS.enc.Base64.parse(encrypted); 
    const decrypted = CryptoJS.enc.Utf8.stringify(encryptedWord); 
    return decrypted;
}

function s(x) { return x.charCodeAt(0); }

let EncryptKey = "some key";
const IV = "some value";
let plainText = "hello world";  
IV = key.split('').map(s);
let buffer = plainText.split('').map(s);
EncryptKey = key.split('').map(s);

let value = encrypt(); // how to convert tdes.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length) in javascript
回答如下:

请参考crypto包的nodejs文档

const crypto = require('crypto');

const encryptKey = "some key";
const initializationVector = "some value";


const encrypt(plainText)
{
  const cipher = crypto.createCipheriv('aes-192-cbc', encryptKey, initializationVector);
  const encrypted = cipher.update(plainText, 'utf8', 'hex');
  encrypted += cipher.final('hex');
  return encrypted;
}

const decrypt(cypherText)
{
    const decipher = crypto.createDecipheriv('aes-192-cbc', encryptKey, initializationVector);
    let decrypted = decipher.update(cypherText, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;
}

如何使用AES加密在JavaScript中进行加密?

我在JAVA中实现了AES加密的以下功能,如何在javascript或nodeJs中转换它们?

public const string EncryptKey = "some key"
public const string IV = "some value";

 public static string Encryption(string PlainText)
{
    AesCryptoServiceProvider tdes = new AesCryptoServiceProvider();
    tdes.Key = Encoding.ASCII.GetBytes(EncryptKey);
    tdes.IV = Encoding.ASCII.GetBytes(IV);
    byte[] buffer = Encoding.ASCII.GetBytes(PlainText);
    string Value = Convert.ToBase64String(tdes.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
    return Value.Replace('+', '~');
}

public static string Decryption(string CypherText)
{
    CypherText = CypherText.Replace("~", "+");
    byte[] buffer = Convert.FromBase64String(CypherText);
    AesCryptoServiceProvider des = new AesCryptoServiceProvider();
    des.Key = Encoding.ASCII.GetBytes(EncryptKey);
    des.IV = Encoding.ASCII.GetBytes(IV);
    return Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
}

我不确定如何在javscript中转换tdes.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length),我已经转换了密钥,IV和以字节为单位的文本,并且也有一种方法可以转换为base64 encode

function encrypt(str) {
    // INIT
    const myString = str; // Utf8-encoded string

    // PROCESS
    const encryptedWord = CryptoJS.enc.Utf8.parse(myString);
    const encrypted = CryptoJS.enc.Base64.stringify(encryptedWord); 
    return encrypted;
}

function decrypt(str) {
    // INIT
    const encrypted = str; // Base64 encrypted string

    // PROCESS
    const encryptedWord = CryptoJS.enc.Base64.parse(encrypted); 
    const decrypted = CryptoJS.enc.Utf8.stringify(encryptedWord); 
    return decrypted;
}

function s(x) { return x.charCodeAt(0); }

let EncryptKey = "some key";
const IV = "some value";
let plainText = "hello world";  
IV = key.split('').map(s);
let buffer = plainText.split('').map(s);
EncryptKey = key.split('').map(s);

let value = encrypt(); // how to convert tdes.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length) in javascript
回答如下:

请参考crypto包的nodejs文档

const crypto = require('crypto');

const encryptKey = "some key";
const initializationVector = "some value";


const encrypt(plainText)
{
  const cipher = crypto.createCipheriv('aes-192-cbc', encryptKey, initializationVector);
  const encrypted = cipher.update(plainText, 'utf8', 'hex');
  encrypted += cipher.final('hex');
  return encrypted;
}

const decrypt(cypherText)
{
    const decipher = crypto.createDecipheriv('aes-192-cbc', encryptKey, initializationVector);
    let decrypted = decipher.update(cypherText, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;
}
发布评论

评论列表 (0)

  1. 暂无评论