Skip to content

Commit cfeef64

Browse files
committed
Merge branch 'feat/rsa-encryption' into chore/all-my-stuffs
# Conflicts: # package.json # pnpm-lock.yaml # src/tools/index.ts
2 parents fb56bbf + 97a2a11 commit cfeef64

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
"is-cidr": "^5.0.3",
222222
"is-ip": "^5.0.1",
223223
"isbn3": "^1.1.44",
224+
"js-base64": "^3.7.7",
224225
"json5": "^2.2.3",
225226
"jsonpath": "^1.1.1",
226227
"jsonar-mod": "^1.9.0",

pnpm-lock.yaml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ import { tool as option43Generator } from './option43-generator';
130130
import { tool as pgpEncryption } from './pgp-encryption';
131131
import { tool as pgpKeygen } from './pgp-keygen';
132132
import { tool as portNumbers } from './port-numbers';
133+
import { tool as rsaEncryption } from './rsa-encryption';
133134
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
134135
import { tool as numeronymGenerator } from './numeronym-generator';
135136
import { tool as macAddressGenerator } from './mac-address-generator';
@@ -279,6 +280,9 @@ export const toolsByCategory: ToolCategory[] = [
279280
passwordStrengthAnalyser,
280281
pdfSignatureChecker,
281282
pgpKeygen,
283+
passwordStrengthAnalyser,
284+
pdfSignatureChecker,
285+
rsaEncryption,
282286
],
283287
},
284288
{

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)