加密/解密字符串 n JavaScript

问题描述 投票:0回答:3

我有一个字符串,它的长度是

0 <=  X <= 26
;

我需要

encrypt
函数来加密这个字符串以获得 X 字符长度的字符串:
encrypt("My String", "Passphrase", xLengthOutput)
->
"01234567890123456789012345"
;

我还需要

decrypt
函数,它将采用 X 字符长度的字符串并输出我的原始字符串
decrypt("01234567890123456789012345", "Passphrase")
->
"My String"
;

注意:这不是压缩,原始字符串总是小于或等于加密结果。

javascript encryption encryption-symmetric encryption-asymmetric node-crypto
3个回答
0
投票

您可以使用 Crypto API

Crypto.subtle
返回一个 SubtleCrypto 对象,提供对常见加密原语的访问,如散列、签名、加密或解密。

然后你可以使用

SubtleCryptography
中的所有方法,例如
encrypt
decrypt


0
投票

请注意,以下代码仅用于演示目的,在实际应用中应使用更复杂的加密算法和更安全的密钥管理方案。另外,由于Crypto API需要在HTTPS或本地环境下运行,所以上述代码无法在普通的HTTP环境下运行。

// Encryption function
function encrypt(str, key) {
  // Convert the key to a byte array
  var keyBytes = new TextEncoder().encode(key);

  // Convert the plaintext to a byte array
  var textBytes = new TextEncoder().encode(str);

  // Generate an AES key using the Crypto API
  crypto.subtle.importKey("raw", keyBytes, "AES-CBC", false, ["encrypt"]).then(function(key) {
    // Encrypt using AES algorithm
    crypto.subtle.encrypt({ name: "AES-CBC", iv: new Uint8Array(16) }, key, textBytes).then(function(encryptedBytes) {
      // Convert the encrypted byte array to a Base64 string
      var encryptedBase64 = btoa(String.fromCharCode.apply(null, new Uint8Array(encryptedBytes)));
      console.log("Encrypted string: ", encryptedBase64);
    });
  });
}

// Decryption function
function decrypt(encryptedBase64, key) {
  // Convert the key to a byte array
  var keyBytes = new TextEncoder().encode(key);

  // Convert the Base64 string to a byte array
  var encryptedBytes = new Uint8Array(atob(encryptedBase64).split("").map(function(c) { return c.charCodeAt(0); }));

  // Generate an AES key using the Crypto API
  crypto.subtle.importKey("raw", keyBytes, "AES-CBC", false, ["decrypt"]).then(function(key) {
    // Decrypt using AES algorithm
    crypto.subtle.decrypt({ name: "AES-CBC", iv: new Uint8Array(16) }, key, encryptedBytes).then(function(decryptedBytes) {
      // Convert the decrypted byte array to a string
      var decryptedText = new TextDecoder().decode(decryptedBytes);
      console.log("Decrypted string: ", decryptedText);
    });
  });
}

// Example code
var key = "MySecretKey12345"; // Key, can be any string of any length
var plaintext = "Hello World!"; // String to be encrypted

encrypt(plaintext, key);

var ciphertext = "q3Hx4zA4PTGZf41zZSSbEg=="; // Encrypted string, can be obtained from the encrypt function
decrypt(ciphertext, key);

-1
投票

您可以使用以下函数通过javascript加密和解密字符串,很简单,高级版本请使用crypto API。

var encrypt = function(str, key) {
  var result = "";
  for (var i = 0; i < str.length; i++) {
    var charCode = (str.charCodeAt(i) + key) % 256;
    result += String.fromCharCode(charCode);
  }
  return result;
}

var decrypt = function(str, key) {
  var result = "";
  for (var i = 0; i < str.length; i++) {
    var charCode = (str.charCodeAt(i) - key + 256) % 256;
    result += String.fromCharCode(charCode);
  }
  return result;
}
© www.soinside.com 2019 - 2024. All rights reserved.