RustCrypto / block-modes

Collection of generic block mode algorithms written in pure Rust
64 stars 13 forks source link

how to do aes encryption/decryption with ctr model? #53

Closed l-7-l closed 1 year ago

l-7-l commented 1 year ago

The following code implements AES encryption and decryption using crypto-js, but I don't know how to implement it in Rust.

Sorry for my English


function parseKey(aesKey) {
  return {
    iv: CryptoJS.enc.Hex.parse(padEnd('', 32, '0')),
    key: CryptoJS.enc.Base64.parse(aesKey),
  };
}

function aesEncrypt(data, aesKey) {
  const {
    iv,
    key,
  } = parseKey(aesKey);
  const cipherText = CryptoJS.AES.encrypt(JSON.stringify(data), key, {
    iv,
  }).toString();

  return cipherText;
}

// 解密
function aesDecrypt(data, aesKey) {
  const {
    iv,
    key,
  } = parseKey(aesKey);
  const bytes = CryptoJS.AES.decrypt(data, key, {
    iv,
  });
  const decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));

  return decryptedData;
}

stackoverflow link

newpavlov commented 1 year ago

The ctr crate docs should be a good starting point. For decoding Base64 you can use the base64ct crate.

For further help I recommend asking on the URLO forum.