TextEncrypter.vue 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <template>
  2. <div>
  3. <p>Text Encrypter</p>
  4. <form @submit.prevent="encryptText(text, rsaPubKey);">
  5. <input type="text" v-model="text" />
  6. <button>Submit</button>
  7. </form>
  8. <pre>{{rsaPubKey !== undefined ? "has key" : "no key" }}</pre>
  9. <pre>{{encryptedText}}</pre>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name: "TextEncrypter",
  15. props: ["rsaPubKey"],
  16. data() {
  17. return {
  18. text: '',
  19. encryptedText: ''
  20. }
  21. },
  22. methods: {
  23. encryptText: function(toEncrypt, pubkey) {
  24. const crypto = require("crypto");
  25. const constants = require("constants");
  26. const bufferToEncrypt = new Buffer(toEncrypt);
  27. const padding = constants.RSA_PKCS1_PADDING;
  28. const encrypted = crypto.publicEncrypt(
  29. {
  30. key: pubkey,
  31. padding: padding,
  32. },
  33. bufferToEncrypt);
  34. this.encryptedText = encrypted.toString("base64");
  35. },
  36. },
  37. }
  38. </script>