Skip to content

Commit 97a2a11

Browse files
committed
feat(new tool): RSA Encryption
Fix CorentinTh#945
1 parent 9eac9cb commit 97a2a11

File tree

7 files changed

+252
-8
lines changed

7 files changed

+252
-8
lines changed

components.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ declare module '@vue/runtime-core' {
158158
RomanNumeralConverter: typeof import('./src/tools/roman-numeral-converter/roman-numeral-converter.vue')['default']
159159
RouterLink: typeof import('vue-router')['RouterLink']
160160
RouterView: typeof import('vue-router')['RouterView']
161+
RsaEncryption: typeof import('./src/tools/rsa-encryption/rsa-encryption.vue')['default']
161162
RsaKeyPairGenerator: typeof import('./src/tools/rsa-key-pair-generator/rsa-key-pair-generator.vue')['default']
163+
SafelinkDecoder: typeof import('./src/tools/safelink-decoder/safelink-decoder.vue')['default']
162164
SlugifyString: typeof import('./src/tools/slugify-string/slugify-string.vue')['default']
163165
SpanCopyable: typeof import('./src/components/SpanCopyable.vue')['default']
164166
SqlPrettify: typeof import('./src/tools/sql-prettify/sql-prettify.vue')['default']

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"highlight.js": "^11.7.0",
6565
"iarna-toml-esm": "^3.0.5",
6666
"ibantools": "^4.3.3",
67+
"js-base64": "^3.7.7",
6768
"json5": "^2.2.3",
6869
"jwt-decode": "^3.1.2",
6970
"libphonenumber-js": "^1.10.28",

pnpm-lock.yaml

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/composable/computed/catchedComputed.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type Ref, ref, watchEffect } from 'vue';
22

3-
export { computedCatch };
3+
export { computedCatch, computedCatchAsync };
44

55
function computedCatch<T, D>(getter: () => T, { defaultValue }: { defaultValue: D; defaultErrorMessage?: string }): [Ref<T | D>, Ref<string | undefined>];
66
function computedCatch<T, D>(getter: () => T, { defaultValue, defaultErrorMessage = 'Unknown error' }: { defaultValue?: D; defaultErrorMessage?: string } = {}) {
@@ -20,3 +20,22 @@ function computedCatch<T, D>(getter: () => T, { defaultValue, defaultErrorMessag
2020

2121
return [value, error] as const;
2222
}
23+
24+
function computedCatchAsync<T, D>(getterAsync: () => Promise<T>, { defaultValue }: { defaultValue: D; defaultErrorMessage?: string }): [Ref<T | D>, Ref<string | undefined>];
25+
function computedCatchAsync<T, D>(getterAsync: () => Promise<T>, { defaultValue, defaultErrorMessage = 'Unknown error' }: { defaultValue?: D; defaultErrorMessage?: string } = {}) {
26+
const error = ref<string | undefined>();
27+
const value = ref<T | D | undefined>();
28+
29+
watchEffect(async () => {
30+
try {
31+
error.value = undefined;
32+
value.value = await getterAsync();
33+
}
34+
catch (err) {
35+
error.value = err instanceof Error ? err.message : err?.toString() ?? defaultErrorMessage;
36+
value.value = defaultValue;
37+
}
38+
});
39+
40+
return [value, error] as const;
41+
}

src/tools/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer';
66

77
import { tool as textToUnicode } from './text-to-unicode';
88
import { tool as safelinkDecoder } from './safelink-decoder';
9+
import { tool as rsaEncryption } from './rsa-encryption';
910
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
1011
import { tool as numeronymGenerator } from './numeronym-generator';
1112
import { tool as macAddressGenerator } from './mac-address-generator';
@@ -85,7 +86,20 @@ import { tool as yamlViewer } from './yaml-viewer';
8586
export const toolsByCategory: ToolCategory[] = [
8687
{
8788
name: 'Crypto',
88-
components: [tokenGenerator, hashText, bcrypt, uuidGenerator, ulidGenerator, cypher, bip39, hmacGenerator, rsaKeyPairGenerator, passwordStrengthAnalyser, pdfSignatureChecker],
89+
components: [
90+
tokenGenerator,
91+
hashText,
92+
bcrypt,
93+
uuidGenerator,
94+
ulidGenerator,
95+
cypher,
96+
bip39,
97+
hmacGenerator,
98+
rsaKeyPairGenerator,
99+
passwordStrengthAnalyser,
100+
pdfSignatureChecker,
101+
rsaEncryption,
102+
],
89103
},
90104
{
91105
name: 'Converter',

src/tools/rsa-encryption/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Lock } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'RSA encryption',
6+
path: '/rsa-encryption',
7+
description: 'Encrypt and decrypt text clear text using RSA Keys.',
8+
keywords: ['rsa', 'encryption', 'cypher', 'encipher', 'crypt', 'decrypt'],
9+
component: () => import('./rsa-encryption.vue'),
10+
icon: Lock,
11+
createdAt: new Date('2024-04-20'),
12+
});
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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

Comments
 (0)