Created
January 20, 2021 01:47
-
-
Save donma/1abc4b75b754d43abe843ca8f4424f4a to your computer and use it in GitHub Desktop.
javascript.xor.encryption.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // string 轉 base64 | |
| function EncodeB64(str) { | |
| return window.btoa(unescape(encodeURIComponent(str))); | |
| } | |
| // 還原base64 string | |
| function DecodeB64(str) { | |
| return decodeURIComponent(escape(window.atob(str))); | |
| } | |
| //Xor 加密 | |
| function XorEncrypt(data, salt) { | |
| data = EncodeB64(data); | |
| var salts = Array.from(salt); | |
| var output = []; | |
| for (var i = 0; i < data.length; i++) { | |
| var charCode = data.charCodeAt(i) ^ salts[i % salts.length].charCodeAt(0); | |
| output.push(String.fromCharCode(charCode)); | |
| } | |
| var result = output.join(""); | |
| return EncodeB64(result); | |
| } | |
| //Xor 解密 | |
| function XorDecrypt(data, salt) { | |
| data = DecodeB64(data); | |
| var salts = Array.from(salt); | |
| var output = []; | |
| for (var i = 0; i < data.length; i++) { | |
| var charCode = data.charCodeAt(i) ^ salts[i % salts.length].charCodeAt(0); | |
| output.push(String.fromCharCode(charCode)); | |
| } | |
| var res = output.join(""); | |
| return DecodeB64(res); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment