@@ -24,3 +24,118 @@ function keyFinder(str){ // str is used to get the input of encrypted string
2424}
2525return 0 ; // return 0 if found nothing
2626}
27+
28+ /* this sub-function is used to assist the keyfinder to find the key */
29+ function caesarCipherEncodeAndDecodeEngine ( inStr , numShifted )
30+ {
31+ let shiftNum = numShifted ;
32+ let charCode = 0 ;
33+ let outStr = "" ;
34+ let shftedcharCode = 0 ;
35+ let result = 0 ;
36+
37+ for ( let i = 0 ; i < inStr . length ; i ++ ) {
38+
39+ charCode = inStr [ i ] . charCodeAt ( ) ;
40+ shftedcharCode = charCode + shiftNum ;
41+ result = charCode ;
42+
43+ if ( ( charCode >= 48 && charCode <= 57 ) )
44+ {
45+ if ( shftedcharCode < 48 ) {
46+
47+ let diff = Math . abs ( 48 - 1 - shftedcharCode ) % 10 ;
48+
49+ while ( diff >= 10 ) {
50+ diff = diff % 10 ;
51+ }
52+ document . getElementById ( "diffID" ) . innerHTML = diff ;
53+
54+ shftedcharCode = 57 - diff ;
55+
56+ result = shftedcharCode ;
57+ }
58+
59+ else if ( shftedcharCode >= 48 && shftedcharCode <= 57 ) {
60+ result = shftedcharCode ;
61+ }
62+
63+ else if ( shftedcharCode > 57 ) {
64+
65+ let diff = Math . abs ( 57 + 1 - shftedcharCode ) % 10 ;
66+
67+ while ( diff >= 10 ) {
68+ diff = diff % 10 ;
69+ }
70+ document . getElementById ( "diffID" ) . innerHTML = diff ;
71+
72+ shftedcharCode = 48 + diff ;
73+
74+ result = shftedcharCode ;
75+ }
76+
77+
78+ }
79+
80+ else if ( ( charCode >= 65 && charCode <= 90 ) )
81+ {
82+
83+ if ( shftedcharCode <= 64 ) {
84+
85+ let diff = Math . abs ( 65 - 1 - shftedcharCode ) % 26 ;
86+
87+ while ( ( diff % 26 ) >= 26 ) {
88+ diff = diff % 26 ;
89+ }
90+ shftedcharCode = 90 - diff ;
91+ result = shftedcharCode ;
92+ }
93+
94+ else if ( shftedcharCode >= 65 && shftedcharCode <= 90 ) {
95+ result = shftedcharCode ;
96+ }
97+
98+ else if ( shftedcharCode > 90 ) {
99+ let diff = Math . abs ( shftedcharCode - 1 - 90 ) % 26 ;
100+
101+ while ( ( diff % 26 ) >= 26 ) {
102+ diff = diff % 26 ;
103+ }
104+ shftedcharCode = 65 + diff ;
105+ result = shftedcharCode ;
106+ }
107+
108+ }
109+
110+ else if ( ( charCode >= 97 && charCode <= 122 ) )
111+ {
112+ if ( shftedcharCode <= 96 ) {
113+
114+ let diff = Math . abs ( 97 - 1 - shftedcharCode ) % 26 ;
115+
116+ while ( ( diff % 26 ) >= 26 ) {
117+ diff = diff % 26 ;
118+ }
119+ shftedcharCode = 122 - diff ;
120+ result = shftedcharCode ;
121+ }
122+
123+ else if ( shftedcharCode >= 97 && shftedcharCode <= 122 ) {
124+ result = shftedcharCode ;
125+ }
126+
127+ else if ( shftedcharCode > 122 ) {
128+ let diff = Math . abs ( shftedcharCode - 1 - 122 ) % 26 ;
129+
130+ while ( ( diff % 26 ) >= 26 ) {
131+ diff = diff % 26 ;
132+ }
133+ shftedcharCode = 97 + diff ;
134+ result = shftedcharCode ;
135+ }
136+
137+ }
138+ outStr = outStr + String . fromCharCode ( parseInt ( result ) ) ;
139+ }
140+ return outStr ;
141+ }
0 commit comments