|
| 1 | +<script setup lang="ts"> |
| 2 | +import { md, pki } from 'node-forge'; |
| 3 | +import { Base64 } from 'js-base64'; |
| 4 | +import { computedCatchAsync } from '@/composable/computed/catchedComputed'; |
| 5 | +import TextareaCopyable from '@/components/TextareaCopyable.vue'; |
| 6 | +
|
| 7 | +const schemes = [ |
| 8 | + { value: 'RSAES-PKCS1-V1_5', label: 'RSAES PKCS#1 v1.5' }, |
| 9 | + { value: 'RSAES-OAEP', label: 'RSAES-OAEP' }, |
| 10 | + { value: 'RSAES-OAEP/SHA-256', label: 'RSAES-OAEP/SHA-256' }, |
| 11 | + { value: 'RSAES-OAEP/SHA-256/MGF1-SHA-1', label: 'RSAES-OAEP/SHA-256/MGF1-SHA-1 (RSA/ECB/OAEPWithSHA-256AndMGF1Padding)' }, |
| 12 | +]; |
| 13 | +
|
| 14 | +const cryptInput = ref(''); |
| 15 | +const cryptScheme = ref('RSAES-PKCS1-V1_5'); |
| 16 | +const cryptPublicKey = ref(''); |
| 17 | +const [cryptOutput, cryptError] = computedCatchAsync(async () => { |
| 18 | + const publicKeyPEM = cryptPublicKey.value; |
| 19 | + const text = cryptInput.value; |
| 20 | + const scheme = cryptScheme.value; |
| 21 | +
|
| 22 | + const publicKey = pki.publicKeyFromPem(publicKeyPEM); |
| 23 | +
|
| 24 | + let encrypted; |
| 25 | + if (scheme === 'RSAES-PKCS1-V1_5') { |
| 26 | + // encrypt data with a public key using RSAES PKCS#1 v1.5 |
| 27 | + encrypted = publicKey.encrypt(text, 'RSAES-PKCS1-V1_5'); |
| 28 | + } |
| 29 | + else if (scheme === 'RSAES-OAEP') { |
| 30 | + // encrypt data with a public key using RSAES-OAEP |
| 31 | + encrypted = publicKey.encrypt(text, 'RSA-OAEP'); |
| 32 | + } |
| 33 | + else if (scheme === 'RSAES-OAEP/SHA-256') { |
| 34 | + // encrypt data with a public key using RSAES-OAEP/SHA-256 |
| 35 | + encrypted = publicKey.encrypt(text, 'RSA-OAEP', { |
| 36 | + md: md.sha256.create(), |
| 37 | + }); |
| 38 | + } |
| 39 | + else if (scheme === 'RSAES-OAEP/SHA-256/MGF1-SHA-1') { |
| 40 | + // encrypt data with a public key using RSAES-OAEP/SHA-256/MGF1-SHA-1 |
| 41 | + // compatible with Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding |
| 42 | + encrypted = publicKey.encrypt(text, 'RSA-OAEP', { |
| 43 | + md: md.sha256.create(), |
| 44 | + mgf1: { |
| 45 | + md: md.sha1.create(), |
| 46 | + }, |
| 47 | + }); |
| 48 | + } |
| 49 | + return Base64.encode(encrypted || ''); |
| 50 | +}, { |
| 51 | + defaultValue: '', |
| 52 | + defaultErrorMessage: 'Unable to encrypt your text', |
| 53 | +}); |
| 54 | +
|
| 55 | +const decryptInput = ref(''); |
| 56 | +const decryptScheme = ref('RSAES-PKCS1-V1_5'); |
| 57 | +const decryptPrivateKey = ref(''); |
| 58 | +const decryptPrivateKeyPassphrase = ref(''); |
| 59 | +const [decryptOutput, decryptError] = computedCatchAsync(async () => { |
| 60 | + const privateKeyPEM = decryptPrivateKey.value; |
| 61 | + const passphrase = decryptPrivateKeyPassphrase.value; |
| 62 | + const encrypted = Base64.decode(decryptInput.value); |
| 63 | + const scheme = decryptScheme.value; |
| 64 | +
|
| 65 | + const privateKey = pki.decryptRsaPrivateKey(privateKeyPEM, passphrase); |
| 66 | +
|
| 67 | + let decrypted; |
| 68 | + if (scheme === 'RSAES-PKCS1-V1_5') { |
| 69 | + // decrypt data with a private key using RSAES PKCS#1 v1.5 |
| 70 | + decrypted = privateKey.decrypt(encrypted, 'RSAES-PKCS1-V1_5'); |
| 71 | + } |
| 72 | + else if (scheme === 'RSAES-OAEP') { |
| 73 | + // decrypt data with a private key using RSAES-OAEP |
| 74 | + decrypted = privateKey.decrypt(encrypted, 'RSA-OAEP'); |
| 75 | + } |
| 76 | + else if (scheme === 'RSAES-OAEP/SHA-256') { |
| 77 | + // decrypt data with a private key using RSAES-OAEP/SHA-256 |
| 78 | + decrypted = privateKey.decrypt(encrypted, 'RSA-OAEP', { |
| 79 | + md: md.sha256.create(), |
| 80 | + }); |
| 81 | + } |
| 82 | + else if (scheme === 'RSAES-OAEP/SHA-256/MGF1-SHA-1') { |
| 83 | + // decrypt data with a private key using RSAES-OAEP/SHA-256/MGF1-SHA-1 |
| 84 | + // compatible with Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding |
| 85 | + decrypted = privateKey.decrypt(encrypted, 'RSA-OAEP', { |
| 86 | + md: md.sha256.create(), |
| 87 | + mgf1: { |
| 88 | + md: md.sha1.create(), |
| 89 | + }, |
| 90 | + }); |
| 91 | + } |
| 92 | + return decrypted; |
| 93 | +}, { |
| 94 | + defaultValue: '', |
| 95 | + defaultErrorMessage: 'Unable to encrypt your text', |
| 96 | +}); |
| 97 | +</script> |
| 98 | + |
| 99 | +<template> |
| 100 | + <div> |
| 101 | + <c-card title="Encrypt"> |
| 102 | + <div> |
| 103 | + <c-input-text |
| 104 | + v-model:value="cryptInput" |
| 105 | + label="Your text:" |
| 106 | + placeholder="The string to encrypt" |
| 107 | + rows="4" |
| 108 | + multiline raw-text monospace autosize flex-1 |
| 109 | + /> |
| 110 | + <c-select |
| 111 | + v-model:value="cryptScheme" |
| 112 | + label="Scheme:" |
| 113 | + :options="schemes" |
| 114 | + placeholder="Select the encryption scheme" |
| 115 | + /> |
| 116 | + <div flex flex-1 flex-col gap-2> |
| 117 | + <c-input-text |
| 118 | + v-model:value="cryptPublicKey" |
| 119 | + label="Target public key:" |
| 120 | + placeholder="Target public key" |
| 121 | + rows="5" |
| 122 | + multiline raw-text monospace autosize flex-1 |
| 123 | + /> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + |
| 127 | + <c-alert v-if="cryptError && cryptPublicKey !== ''" type="error" mt-12 title="Error while encrypting"> |
| 128 | + {{ cryptError }} |
| 129 | + </c-alert> |
| 130 | + |
| 131 | + <n-form-item label="Your text encrypted:" mt-3> |
| 132 | + <TextareaCopyable |
| 133 | + :value="cryptOutput || ''" |
| 134 | + rows="3" |
| 135 | + placeholder="Your string encrypted" |
| 136 | + multiline monospace readonly autosize mt-5 |
| 137 | + /> |
| 138 | + </n-form-item> |
| 139 | + </c-card> |
| 140 | + |
| 141 | + <c-card title="Decrypt"> |
| 142 | + <div> |
| 143 | + <c-input-text |
| 144 | + v-model:value="decryptInput" |
| 145 | + label="Your PGP Message to decrypt:" |
| 146 | + placeholder="The string to decrypt" |
| 147 | + rows="4" |
| 148 | + multiline raw-text monospace autosize flex-1 |
| 149 | + /> |
| 150 | + |
| 151 | + <c-select |
| 152 | + v-model:value="decryptScheme" |
| 153 | + label="Scheme:" |
| 154 | + :options="schemes" |
| 155 | + placeholder="Select the encryption scheme" |
| 156 | + /> |
| 157 | + |
| 158 | + <div flex flex-1 flex-col gap-2> |
| 159 | + <c-input-text |
| 160 | + v-model:value="decryptPrivateKey" |
| 161 | + label="Your private key:" |
| 162 | + placeholder="The private key to use to decrypt message" |
| 163 | + rows="5" |
| 164 | + multiline raw-text monospace autosize flex-1 |
| 165 | + /> |
| 166 | + |
| 167 | + <c-input-text |
| 168 | + v-model:value="decryptPrivateKeyPassphrase" |
| 169 | + label="Your private key password:" clearable raw-text |
| 170 | + /> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + |
| 174 | + <c-alert v-if="decryptError && decryptPrivateKey !== ''" type="error" mt-12 title="Error while decrypting"> |
| 175 | + {{ decryptError }} |
| 176 | + </c-alert> |
| 177 | + |
| 178 | + <n-form-item label="Your text decrypted:" mt-3> |
| 179 | + <TextareaCopyable |
| 180 | + :value="decryptOutput || ''" |
| 181 | + rows="3" |
| 182 | + placeholder="Your string decrypted" |
| 183 | + multiline monospace readonly autosize mt-5 |
| 184 | + /> |
| 185 | + </n-form-item> |
| 186 | + </c-card> |
| 187 | + </div> |
| 188 | +</template> |
0 commit comments