最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Equivalent AES-256 Encryption in Rust (Migrating from Node.js crypto.createCipher) - Stack Overflow

programmeradmin0浏览0评论

I want to migrating an encryption function from Node.js to Rust and need guidance on ensuring the Rust implementation behaves the same way as the Node.js crypto.createCipher method. I want to make sure that encryption and decryption are interoperable between the two languages.

Node Code :

const crypto = require('crypto');

var encryption = function (doc) {
   let key = "QLFZQhdUmCgQa5cSBYlHFeqGYw0yPtWiqvCbdYIlQnq3jDMSaWnjR0FjeeyIU8rd";
   const cipher = crypto.createCipher("aes256",key);
   let encrypted = cipher.update(doc, 'utf8', 'hex');
   encrypted += cipher.final('hex');
   return encrypted;
};

Questions & Concerns:

  1. How to implement the same AES-256 encryption in Rust?
  2. Does crypto.createCipher('aes256', key) apply any implicit padding?

I tried implementing AES-256 decryption in Rust using the aes crate, but I get a key length mismatch error:

use aes::Aes256;
use aes::cipher::{KeyInit, generic_array::GenericArray};
use serde_json::{json, Value};

pub fn norm_aes_decryption(encrypted_data: String) -> Result<Value, String> {
let key = "QLFZQhdUmCgQa5cSBYlHFeqGYw0yPtWiqvCbdYIlQnq3jDMSaWnjR0FjeeyIU8rd";

let key_bytes = key.as_bytes();
let key = GenericArray::from_slice(key_bytes);
let cipher = Aes256::new(&key);

println!("{:?}", cipher);
Ok(json!({}))

}

Error Output: thread 'main' panicked at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/generic-array-0.14.7/src/lib.rs:572:9: assertion left == right failed left: 64 right: 32 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

发布评论

评论列表(0)

  1. 暂无评论