javascript - 如何使用 javascript 将 textarea 转换为显示其他内容(例如 base64 解密)?

标签 javascript html encryption base64

我正在尝试用 HTML 创建解密页面。我可以获取输入并将其显示为输出,但我对如何更改中间的文本感到困惑。到目前为止,这是我的代码: http://jsfiddle.net/RZwmX/1

HTML:

<textarea name="CipherText" id="CipherText" placeholder="Enter ciphertext here:"></textarea>
<div id="prevCom"></div>

Javascript:

wpcomment.onkeyup = wpcomment.onkeypress = function(){
        wpcomment=btoa(wpcomment);
    document.getElementById('prevCom').innerHTML = this.value;
}

//////////////////////////////////////
//from: https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript
// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"

我从 https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript 获取代码。 Base64 只是一个例子。我想知道的是我该如何进行过渡。在我的 JSFiddle 中,JavaScript 第 3 行是我实现转换的想法,但这不起作用。

最佳答案

我会做这样的事情:

const input = document.querySelector('#encoded-input')
const output = document.querySelector('#decoded-output')

input.oninput = e => {
  try {
    output.innerText = atob(e.target.value)
  } catch (error) {
    output.innerText = e.target.value
      ? 'Please enter a valid Base64 encoded value.'
      : ''
  }
}
<textarea id="encoded-input"></textarea>
<div id="decoded-output"></div>

关于javascript - 如何使用 javascript 将 textarea 转换为显示其他内容(例如 base64 解密)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61087675/

相关文章:

javascript - Angular : $scope and template breaking after using a $resource

javascript - ':active' class not working neither 'routerLinkActive' is working

javascript - 如何在同一个下拉菜单中调用不同的div

html - 中心内容之外的固定元素

python - dask.dataframe 的 to_parquet 支持服务器端加密吗?

javascript - babel-loader 错误 - TypeError : val is not a function

javascript - JavaScript 中 onclick 属性的变量范围

javascript - 当其他 div 具有相同的类时,如何要求 jquery 应用于一个 div

javascript - Node.js AES 解密 iOS 加密的 NSString

java - 图片加密,为什么不能解密?