This is a project done in HTML5, CSS3, and JS that encrypts and decrypts your text by providing a text that you want to encrypt and a key which will generate that encryption.
» You can do git clone
and clone the repo and open the index.html
file to work.
The encryption and decryption is done in JS.
For the characters to be used to generate the text, it will be ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,?!'_-&@#$%*()/:<>|+=[SPACE-CHARACTER]
The encryptText
function starts by intializing an empty string called encryptedText
. This string will be used to store the encrypted version of the input text.
The function then enters a for
loop that iterates over each character in the input text. For each character in text
argument, the function performs the following steps:
- It retrieves the current character from the input text and stores it in a variable called
textChar
. - It retrieves the corresponding character from the key, using the
%
(modulo) operator to ensure that the key is repeated if it is shorter than the input text. This character is stored in a variable calledkeyChar
. - It finds the index of both
textChar
andkeyChar
in an array calledcharacters
. These indices are stored in variables calledtextIndex
andkeyIndex
, respectively. - If
textIndex
is not equal to -1, this means that the current character from the input text was found in thecharacters
array. In this case, the function calculates a new index by addingtextIndex
andkeyIndex
, and then taking the result modulo the length of thecharacters
array. This new index is used to retrieve a new character from thecharacters
array, which is then appended to theencryptedText
string.
After all the characters in the input text have been processed, the function returns the final encrypted text.
The decryptText
function takes in two arguments: encryptedText
and key
. The encryptedText
argument is the text that needs to be decrypted, and the key
argument is the key used to decrypt the text.
The function starts by initializing an empty string called decryptedText
. This string will be used to store the decrypted version of the input text.
The function the enters a for
loop that iterates over each character in the input encrypted text. For each character, the function performs the following step:
- It retrieves the current character from the input encrypted text and stores it in a variable called
encryptedChar
. - It retrieves the corresponding character from the key, using the modulo operator to ensure that the key is repeated if it is shorter than the input encrypted text. This character is stored in a variable called
keyChar
. - It finds the index of both
encryptedChar
andkeyChar
in an array calledcharacters
. These indices are stored in variables calledencryptedIndex
andkeyIndex
, respectively. - If
encryptedIndex
is equal to -1, this means that the current character from the input encrypted text was not found in thecharacters
array. In this case, the function simply appends this character to thedecryptedText
string. - If
encryptedIndex
is not equal to -1, this means that the current character from the input encrypted text was found in thecharacters
array. In this case, the function calculates a new index by subtractingkeyIndex
fromencryptedIndex
. If this new index is less than 0, it is incremented by th length of thecharacters
array. This new index is less than 0, it is incremented by the length of thecharacters
array. This new index is used to to retrieve a new character from thecharacters
array, which is then appended to thedecryptedText
string.
After all characters in the input encrypted text have been processed, the function returns the final decrypted text.