Skip to content

Commit d8a4970

Browse files
committed
add keyencoding support for encryption
1 parent b430bae commit d8a4970

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/tools/encryption/encryption.vue

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ import { AES, RC4, Rabbit, TripleDES, enc } from 'crypto-js';
33
import { computedCatch } from '@/composable/computed/catchedComputed';
44
55
const algos = { AES, TripleDES, Rabbit, RC4 };
6+
type KeyEncoding = 'Text' | 'Hex';
67
78
const cypherInput = ref('Lorem ipsum dolor sit amet');
89
const cypherAlgo = ref<keyof typeof algos>('AES');
910
const cypherSecret = ref('my secret key');
10-
const cypherOutput = computed(() => algos[cypherAlgo.value].encrypt(cypherInput.value, cypherSecret.value).toString());
11+
const cypherSecretEncoding = ref<KeyEncoding>('Text');
12+
const [cypherOutput, cypherError] = computedCatch(() => algos[cypherAlgo.value].encrypt(cypherInput.value, cypherSecretEncoding.value === 'Text' ? cypherSecret.value : enc.Hex.parse(cypherSecret.value), { iv: enc.Hex.parse('00000000000000000000000000000000') }).toString(), {
13+
defaultValue: '',
14+
defaultErrorMessage: 'Unable to cypher your text',
15+
});
1116
1217
const decryptInput = ref('U2FsdGVkX1/EC3+6P5dbbkZ3e1kQ5o2yzuU0NHTjmrKnLBEwreV489Kr0DIB+uBs');
1318
const decryptAlgo = ref<keyof typeof algos>('AES');
1419
const decryptSecret = ref('my secret key');
15-
const [decryptOutput, decryptError] = computedCatch(() => algos[decryptAlgo.value].decrypt(decryptInput.value, decryptSecret.value).toString(enc.Utf8), {
20+
const decryptSecretEncoding = ref<KeyEncoding>('Text');
21+
const [decryptOutput, decryptError] = computedCatch(() => algos[decryptAlgo.value].decrypt(decryptInput.value, decryptSecretEncoding.value === 'Text' ? decryptSecret.value : enc.Hex.parse(decryptSecret.value), { iv: enc.Hex.parse('00000000000000000000000000000000') }).toString(enc.Utf8), {
1622
defaultValue: '',
1723
defaultErrorMessage: 'Unable to decrypt your text',
1824
});
@@ -31,13 +37,32 @@ const [decryptOutput, decryptError] = computedCatch(() => algos[decryptAlgo.valu
3137
<div flex flex-1 flex-col gap-2>
3238
<c-input-text v-model:value="cypherSecret" label="Your secret key:" clearable raw-text />
3339

40+
<c-select
41+
v-model:value="cypherSecretEncoding" label="Key encoding"
42+
flex-1
43+
placeholder="Select the key encoding..."
44+
:options="[
45+
{
46+
label: 'Plain Text',
47+
value: 'Text',
48+
},
49+
{
50+
label: 'Hexadecimal Text',
51+
value: 'Hex',
52+
},
53+
]"
54+
/>
55+
3456
<c-select
3557
v-model:value="cypherAlgo"
3658
label="Encryption algorithm:"
3759
:options="Object.keys(algos).map((label) => ({ label, value: label }))"
3860
/>
3961
</div>
4062
</div>
63+
<c-alert v-if="cypherError" type="error" mt-12 title="Error while cyphering">
64+
{{ cypherError }}
65+
</c-alert>
4166
<c-input-text
4267
label="Your text encrypted:"
4368
:value="cypherOutput"
@@ -58,6 +83,22 @@ const [decryptOutput, decryptError] = computedCatch(() => algos[decryptAlgo.valu
5883
<div flex flex-1 flex-col gap-2>
5984
<c-input-text v-model:value="decryptSecret" label="Your secret key:" clearable raw-text />
6085

86+
<c-select
87+
v-model:value="decryptSecretEncoding" label="Key encoding"
88+
flex-1
89+
placeholder="Select the key encoding..."
90+
:options="[
91+
{
92+
label: 'Plain Text',
93+
value: 'Text',
94+
},
95+
{
96+
label: 'Hexadecimal Text',
97+
value: 'Hex',
98+
},
99+
]"
100+
/>
101+
61102
<c-select
62103
v-model:value="decryptAlgo"
63104
label="Encryption algorithm:"

0 commit comments

Comments
 (0)