@@ -3,16 +3,22 @@ import { AES, RC4, Rabbit, TripleDES, enc } from 'crypto-js';
3
3
import { computedCatch } from ' @/composable/computed/catchedComputed' ;
4
4
5
5
const algos = { AES , TripleDES , Rabbit , RC4 };
6
+ type KeyEncoding = ' Text' | ' Hex' ;
6
7
7
8
const cypherInput = ref (' Lorem ipsum dolor sit amet' );
8
9
const cypherAlgo = ref <keyof typeof algos >(' AES' );
9
10
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 (' ' ) }).toString (), {
13
+ defaultValue: ' ' ,
14
+ defaultErrorMessage: ' Unable to cypher your text' ,
15
+ });
11
16
12
17
const decryptInput = ref (' U2FsdGVkX1/EC3+6P5dbbkZ3e1kQ5o2yzuU0NHTjmrKnLBEwreV489Kr0DIB+uBs' );
13
18
const decryptAlgo = ref <keyof typeof algos >(' AES' );
14
19
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 (' ' ) }).toString (enc .Utf8 ), {
16
22
defaultValue: ' ' ,
17
23
defaultErrorMessage: ' Unable to decrypt your text' ,
18
24
});
@@ -31,13 +37,32 @@ const [decryptOutput, decryptError] = computedCatch(() => algos[decryptAlgo.valu
31
37
<div flex flex-1 flex-col gap-2 >
32
38
<c-input-text v-model:value =" cypherSecret" label =" Your secret key:" clearable raw-text />
33
39
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
+
34
56
<c-select
35
57
v-model:value =" cypherAlgo"
36
58
label =" Encryption algorithm:"
37
59
:options =" Object.keys(algos).map((label) => ({ label, value: label }))"
38
60
/>
39
61
</div >
40
62
</div >
63
+ <c-alert v-if =" cypherError" type =" error" mt-12 title =" Error while cyphering" >
64
+ {{ cypherError }}
65
+ </c-alert >
41
66
<c-input-text
42
67
label =" Your text encrypted:"
43
68
:value =" cypherOutput"
@@ -58,6 +83,22 @@ const [decryptOutput, decryptError] = computedCatch(() => algos[decryptAlgo.valu
58
83
<div flex flex-1 flex-col gap-2 >
59
84
<c-input-text v-model:value =" decryptSecret" label =" Your secret key:" clearable raw-text />
60
85
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
+
61
102
<c-select
62
103
v-model:value =" decryptAlgo"
63
104
label =" Encryption algorithm:"
0 commit comments