1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script> <style> #root { width: 60%; margin: auto; }
.header { width: 360px; margin: auto; }
.main { display: flex; justify-content: left; padding-top: 20px; }
.area { width: 40%; padding-right: 80px; }
.area textarea { width: 100%; height: 120px; } </style> </head> <body> <div id="root"> <div class="header"> <input type="text" placeholder="请输入密钥" v-model.lazy.trim="key" @blur="setKey"> <button @click="encryption">加密</button> <button @click="decrypt">解密</button> </div> <div class="main"> <div class="area"> <textarea placeholder="请输入明文/密文" v-model.lazy.trim="put"></textarea> </div> <div class="area"> <textarea placeholder="加密/解密的结果" v-model.lazy.trim="out"></textarea> </div> </div> </div> <script> Vue.config.productionTip = false;
new Vue({ el: "#root", data: { key: '', put: '', out: '', }, computed: { table() { const obj = {} let c1 = '', c2 = '', r = '' for (let i = 97; i < 123; i++) { c1 = String.fromCharCode(i) obj[c1] = {} for (let n = 97; n < 123; n++) { let res = n + i - 97 c2 = String.fromCharCode(n) r = String.fromCharCode(res > 122 ? res - 26 : res) obj[c1][c2] = r } } return obj } }, methods: { setKey() { if (!/^[a-z|A-Z]+$/.test(this.key)) { this.key = '' } }, encryption() { let {put, key} = this key = key.toLowerCase() let out = [] let n = 0 const l = key.length - 1 if (put && key) { for (let i = 0; i < put.length; i++) { if (/[a-z]/.test(put[i])) { out.push(this.table[key[n]][put[i]]) } else if (/[A-Z]/.test(put[i])) { let char = this.table[key[n]][put[i].toLowerCase()] out.push(char.toUpperCase()) } else { out.push(put[i]) } n === l ? n = 0 : n++ } this.out = out.join('') } }, decrypt() { let {put, key} = this key = key.toLowerCase() let out = [] let n = 0 const l = key.length - 1 if (put && key) { for (let i = 0; i < put.length; i++) { if (/[a-z]/.test(put[i])) { out.push(this.decryptUtil(key[n], put[i])) } else if (/[A-Z]/.test(put[i])) { let char = this.decryptUtil(key[n], put[i].toLowerCase()) out.push(char.toUpperCase()) } else { out.push(put[i]) } n === l ? n = 0 : n++ } this.out = out.join('') } }, decryptUtil(key, value) { for (let i = 97; i < 123; i++) { if (this.table[key][String.fromCharCode(i)] === value) { return String.fromCharCode(i) } } } } }); </script> </body> </html>
|