C program to encrypt and decrypt
And another code to decrypt the same data of a textual file. What an Encryption and Decryption Means? Encryption of a data means converting the data from original form into a coded form. The coded form of original data can not be read by an unauthorized person.
Note - Authorized person knows the decryption key. Decryption key is a password or formula that is used to convert cyphertext to plaintext. Note - Encrypted data is known as cyphertext, whereas decrypted data orignal data is known as plaintext.
Therefore in simple language, converting data from plaintext to cyphertext is known as data encryption. Whereas converting data from cyphertext to plaintext is known as data decryption. Now let's move on to the program given below, to encrypt the data of this newly created file and then further decrypt the same data.
And then encrypt the content present inside the file as shown in the program given below. Now enter the name of file say codescracker. Here is the final snapshot of sample run:. Now if you open the file codescracker , its data content gets encrypted. Here is the snapshot of the opened file, codescracker. As you can see from the above snapshot, the data of the file, codescracker.
The decryption key is the formula used while encrypting the file, that is, adding to each and every character. So to decrypt, we have to subtract from each and every character. The file, tmp. The fstream library allows to work with files. It is defined in fstream header file. The open function receives one or two argument. The first argument is required, that is the name of file. The second argument is file opening mode. So the statement given below:.
At the time of reading, create some algorithm to encrypt the content of file. And place the content in a temporary file character by character. Finally copy the content of temporary file to original file as shown in the program given below:. Menu in Code::Blocks. Save the file into the same directory, where the file codescracker. Now build and run the above program after saving it. Because, we have used w as file opening mode for the file temp. And if file does not exists, then a new file gets created with same name, that is temp.
Now here is the sample run of above program:. Now supply the name of file as codescracker. Here is the second snapshot of the same sample run:. Now your file codescracker. Here is the snapshot of the folder c programs. You will see that a new file text.
0コメント