Last active
February 7, 2025 03:52
-
-
Save Jlvck/8698355085c9dfec46862be23ab0afa1 to your computer and use it in GitHub Desktop.
Stage 2 task for HNG Internship
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
class Encryptor { | |
final String key; | |
Encryptor({required this.key}); | |
//Encrypt the plain text | |
String encrypt(String plainInputText) { | |
List<int> textInputBytes = | |
utf8.encode(plainInputText); //Encode Text input to utf8 | |
List<int> keyBytes = utf8.encode(key); //Encode Key to utf8 | |
List<int> encryptedBytes = _xor(textInputBytes, keyBytes); //XOR operation | |
return base64.encode(encryptedBytes); | |
} | |
//Decrypt the encrypted text | |
String decrypt(String encryptedInputText) { | |
List<int> encryptedBytes = | |
base64.decode(encryptedInputText); //Decode encrypted text to base64 | |
List<int> keyBytes = utf8.encode(key); //Encode Key to utf8 | |
List<int> decryptedBytes = _xor(encryptedBytes, keyBytes); //XOR operation | |
return utf8.decode(decryptedBytes); | |
} | |
// XOR operation | |
List<int> _xor(List<int> text, List<int> key) { | |
int keyLength = key.length; | |
return List.generate( | |
text.length, | |
(i) => | |
text[i] ^ | |
key[i % | |
keyLength], //ensures the key is repeated to match the length of the text | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import './encryptor.dart'; | |
void main(List<String> args) { | |
//Encryption Example | |
String plainTextExample1 = 'Jonathan is a Manchester United Fan'; | |
final key1 = 'Football'; | |
final encryptor1 = Encryptor(key: key1); | |
String encryptedTextExample1 = encryptor1.encrypt(plainTextExample1); | |
String decryptedTextExample1 = encryptor1.decrypt(encryptedTextExample1); | |
print('Example 1. Plain Text: $plainTextExample1, Key: $key1'); | |
print('Example 1. Encrypted Text: $encryptedTextExample1'); | |
print('Example 1. Decrypted Text: $decryptedTextExample1'); | |
print('-----------------------------------'); | |
//DecryptionExample | |
String encryptedTextExample2 = | |
'GzomRyxORTYGDxQNSUFzHRJHEUhUcxYEFBEAWD1UFQ8AAEY8Bg0D'; | |
final key2 = 'Stage 1'; | |
final encryptor2 = Encryptor(key: key2); | |
String decryptedTextExample2 = encryptor2.decrypt(encryptedTextExample2); | |
print('Example 2. Encrypted Text: $encryptedTextExample2, Key: $key2'); | |
print('Example 2. Decrypted Text: $decryptedTextExample2'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example Input & Output 1 (Encryption) | |
Input Text: Jonathan is a Manchester United Fan, Input Key: Football | |
Output: DAABFRYJDQJmBhxUA0EhDSgMBxERFQkeZjoBHRYECEwADgE= | |
Example Input & Outut 2 (Decryption) | |
Input Text:GzomRyxORTYGDxQNSUFzHRJHEUhUcxYEFBEAWD1UFQ8AAEY8Bg0D, Key: Stage 1 | |
Output: HNG Internship is the best in the world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import './encryptor.dart'; | |
void main(List<String> args) { | |
print( | |
'Welcome to Ogudu Jonathan Stage 1 Task (Encryption & Decryption Program)'); | |
while (true) { | |
stdout.write( | |
'Enter D to initiate Decryption, E to initiate Encryption or Q to Quit: '); | |
String? plainText = stdin.readLineSync(); | |
if (plainText == 'E' || plainText == 'e') { | |
stdout.write('Enter the text to encrypt: '); | |
String? textToEncrypt = stdin.readLineSync(); | |
stdout.write('Enter the key: '); | |
String? key = stdin.readLineSync(); | |
if (textToEncrypt != null && key != null) { | |
final encryptor = Encryptor(key: key); | |
String encryptedText = encryptor.encrypt(textToEncrypt); | |
print('Encrypted Text: $encryptedText'); | |
} | |
} else if (plainText == 'D' || plainText == 'd') { | |
stdout.write('Enter the text to decrypt: '); | |
String? textToDecrypt = stdin.readLineSync(); | |
stdout.write('Enter the key: '); | |
String? key = stdin.readLineSync(); | |
if (textToDecrypt != null && key != null) { | |
final encryptor = Encryptor(key: key); | |
String decryptedText = encryptor.decrypt(textToDecrypt); | |
print('Decrypted Text: $decryptedText'); | |
} | |
} else if (plainText == 'Q' || plainText == 'q') { | |
print('Goodbye!'); | |
break; | |
} else { | |
print('Invalid Input'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Intructions on how to use encryptor class (Using examples.dart as reference point); | |
1. import encryptor class with the below command. | |
import './encryptor.dart'; | |
2. Create a String variable for Input-Text(plain/encrypted) and Key. | |
String plainTextExample1 = 'Jonathan is a Manchester United Fan'; | |
final key1 = 'Football'; | |
3. Initiate an instance for Encryptor Class using the key variable. | |
final encryptor1 = Encryptor(key: key1); | |
4. The encryptor class has a method for encryption & decryption as illustrated below. | |
encryptor1.encrypt(plainTextExample1); // For Encryption | |
encryptor2.decrypt(encryptedTextExample2); // for Decrytion | |
5. Using the Encryption or Decryption method, the returned String values can be assigned to variables and used where intended. | |
String encryptedTextExample1 = encryptor1.encrypt(plainTextExample1); | |
or | |
print(encryptor1.encrypt(plainTextExample1)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment