<template><div><h1>RSA 分块加解密 Demo</h1><textarea v-model="text" placeholder="输入要加密的文本"></textarea><button @click="encryptText">加密</button><p>加密后的文本(Base64 编码):{{ encryptedText }}</p><button @click="decryptText">解密</button><p>解密后的文本:{{ decryptedText }}</p></div>
</template><script>
import JSEncrypt from 'jsencrypt';export default {data() {return {text: 'RSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 DemoRSA 分块加解密 Demo',encryptedText: '',decryptedText: '',publicKey: `MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArgbzDkO1fpSJQTFI4U8eCJKNSOsYUfsxNF/Xsy/cShDpCAYQpKGFaexXinAw29TGykF9yX/ZkqL4Vnz3/f1PU/WVvfhJ5CG48NJh1HCiyE8SJDy5bNULs2krYtvoh05rypJX9g7h+ugzfu1QTpxloYjmonwENpaFv2lwTugcD2hjoCxo+UJUumlP/Z8hZ62MNtWDRgU9mgwKK6zOnIiw1nS/MYP+Kde9N5N+9aVE+ILvA8IDXXstPC28AkjSyeoSVlZyGIWDQMxi3ePDo1HOepffsW4utp74xDCH60ImwgpZmeoIH8Sa9g5Oyv0aRLkxMOLjZqbtMOczAReAJvrzbwIDAQAB`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5ibUsl7DZHZM4/rnQbCd6wKBgGp2KKOIuVV97BRoH/mMtnOriWbWaAw941nYJfZYFMsToUZJ1k+BiVDblmuMgxuDeohVfwAJO9A46Tuz/ojZn7BVopW1OXZ2EJ87p7xZ1NZ4ZYo4VVUZNICPLeYyQoIq3vGh042XtwjHFtz7xFv4vM1VbxCL/fcSDJaUnsLVopwhAoGBALIrnjp33O6G/krX5OBMRgRo+41R1YUm5WjI/G5xy2UIoqlvm8vFomwzEQKbWJJUoF3l4P5nqXfzBhX/EZjdrrx0kKnWxQJipqUojMHhdkYE9mubjL3tZS/238j68u+iqVu2eHjE34l1AR+QYgpiQym8B8+Ncwzt6yrmK0sM/bgfAoGBAKnAVUkmXuUaFJ7l0QkcVVAa5Uyzs4922WqBbBSQOOdVMVKzB0BvRpazUM/5geurAgpCV4Cs1w267D6AY5+gh+6eM5fCwzvttNdf3O2MyLz1w2z7AlPQhWTUJYkQ1FpvT0i8DV51NtcMM/pSZsFtizMIYd7jPfKz/QG5SafHiAqe`};},methods: {encryptLongText(text, publicKey) {const encrypt = new JSEncrypt({ default_key_size: 2048 });encrypt.setPublicKey(publicKey);//utf-8编码let bytes = new TextEncoder('utf-8').encode(text);//字节数组转base64const base64EncodedText = btoa(String.fromCharCode(...bytes));//分块 245字节let chunks = [];let offset = 0;while (offset < base64EncodedText.length) {let end = Math.min(offset + 245, base64EncodedText.length);chunks.push(base64EncodedText.slice(offset, end));offset = end;}//每块 rsa加密 return {string} the encrypted string encoded in base64let encryptedChunks = chunks.map(chunk => encrypt.encrypt(chunk));//base64 转 字符串 //atob == Base64格式转字符串编码encryptedChunks = encryptedChunks.map(chunk => atob(chunk));//拼接起来 字符串 转 最终base64return btoa(encryptedChunks.join(""));},decryptLongText(encryptedText, privateKey) {const decrypt = new JSEncrypt({ default_key_size: 2048 });decrypt.setPrivateKey(privateKey);//因为加密串为base64编码,需要将base64解码 //atob == Base64格式转字符串编码const base64DecodedText = atob(encryptedText);//分块 256字节let chunks = [];let offset = 0;while (offset < base64DecodedText.length) {let end = Math.min(offset + 256, base64DecodedText.length);//btoa == 字符串编码为Base64格式chunks.push(btoa(base64DecodedText.slice(offset, end)));offset = end;}//对每一块分别进行rsa解密let decryptedChunks = chunks.map(chunk => decrypt.decrypt(chunk));//将Base64编码的字符串转换回字节数组const decryptedBytes = Uint8Array.from(atob(decryptedChunks.join("")), c => c.charCodeAt(0));//使用TextDecoder将字节数组转换回UTF-8编码的字符串const decoder = new TextDecoder('utf-8');const decryptedText = decoder.decode(decryptedBytes);//转原始字符串return decryptedText;},encryptText() {this.encryptedText = this.encryptLongText(this.text, this.publicKey);this.decryptedText = '';},decryptText() {this.decryptedText = this.decryptLongText(this.encryptedText, this.privateKey);}}
};
</script><style scoped>
textarea {width: 100%;height: 100px;margin-bottom: 10px;
}button {margin-right: 5px;
}
</style>