Last active
February 3, 2025 11:03
-
-
Save Pelumiii1/d62e5810602ba56df46e842c1c2268b1 to your computer and use it in GitHub Desktop.
Data Encryption and Decryption
This file contains hidden or 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 React, { useState } from "react"; | |
| import { Button, StyleSheet, Text, TextInput, View } from "react-native"; | |
| import { encrypt, decrypt } from "./EncryptionUtils"; | |
| const App = () => { | |
| const [plainText, setPlainText] = useState<string>(""); | |
| const [secretKey, setSecretKey] = useState<string>(""); | |
| const [encryptedText, setEncryptedText] = useState<string>(""); | |
| const [decryptedText, setDecryptedText] = useState<string>(""); | |
| function handleEncrypt() { | |
| const encrypted = encrypt(plainText, secretKey); | |
| setEncryptedText(encrypted); | |
| } | |
| function handleDecrypt() { | |
| const decrypted = decrypt(encryptedText, secretKey); | |
| setDecryptedText(decrypted); | |
| } | |
| return ( | |
| <View style={styles.container}> | |
| <Text | |
| style={{ | |
| textAlign: "center", | |
| marginBottom: 10, | |
| fontWeight: 600, | |
| fontSize: 20, | |
| }} | |
| > | |
| Encryption App | |
| </Text> | |
| <Text>Plain Text:</Text> | |
| <TextInput | |
| style={styles.input} | |
| placeholder="Enter Plaintext" | |
| onChangeText={setPlainText} | |
| value={plainText} | |
| /> | |
| <Text>Secret Key:</Text> | |
| <TextInput | |
| style={styles.input} | |
| placeholder="Enter Secret key" | |
| value={secretKey} | |
| onChangeText={setSecretKey} | |
| /> | |
| <Button title="Encrypt" onPress={handleEncrypt} /> | |
| <Text style={styles.output}>Encrypted Text: {encryptedText}</Text> | |
| <View style={{ marginBottom: 10 }} /> | |
| <Button title="Decrypt" onPress={handleDecrypt} /> | |
| <Text style={styles.output}>Decrypted Text: {decryptedText}</Text> | |
| </View> | |
| ); | |
| }; | |
| export default App; | |
| const styles = StyleSheet.create({ | |
| container: { | |
| flex: 1, | |
| justifyContent: "center", | |
| padding: 16, | |
| }, | |
| input: { | |
| height: 40, | |
| borderColor: "gray", | |
| borderWidth: 1, | |
| marginBottom: 12, | |
| paddingHorizontal: 8, | |
| }, | |
| output: { | |
| marginTop: 12, | |
| fontSize: 16, | |
| }, | |
| }); |
This file contains hidden or 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
| // EncryptionUtils.js | |
| // Function to encrypt the plaintext | |
| export const encrypt = (plaintext: string, key: string): string => { | |
| let encryptedText = ""; | |
| for (let i = 0; i < plaintext.length; i++) { | |
| const char = plaintext[i]; | |
| if (char.match(/[a-z]/i)) { | |
| const code = plaintext.charCodeAt(i); | |
| let shift = key.length; // Use key length as the shift value | |
| if (code >= 65 && code <= 90) { | |
| encryptedText += String.fromCharCode(((code - 65 + shift) % 26) + 65); | |
| } else if (code >= 97 && code <= 122) { | |
| encryptedText += String.fromCharCode(((code - 97 + shift) % 26) + 97); | |
| } | |
| } else { | |
| encryptedText += char; // Non-alphabetic characters remain unchanged | |
| } | |
| } | |
| return encryptedText; | |
| }; | |
| // Function to decrypt the encrypted text | |
| export const decrypt = (encryptedText: string, key: string): string => { | |
| let decryptedText = ""; | |
| for (let i = 0; i < encryptedText.length; i++) { | |
| const char = encryptedText[i]; | |
| if (char.match(/[a-z]/i)) { | |
| const code = encryptedText.charCodeAt(i); | |
| let shift = key.length; // Use key length as the shift value | |
| if (code >= 65 && code <= 90) { | |
| decryptedText += String.fromCharCode( | |
| ((code - 65 - shift + 26) % 26) + 65 | |
| ); | |
| } else if (code >= 97 && code <= 122) { | |
| decryptedText += String.fromCharCode( | |
| ((code - 97 - shift + 26) % 26) + 97 | |
| ); | |
| } | |
| } else { | |
| decryptedText += char; // Non-alphabetic characters remain unchanged | |
| } | |
| } | |
| return decryptedText; | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Set Up:
Run the App:
Example Inputs and Outputs
Plaintext: HelloWorld
Key: secret
Encrypted Text: KhoorZruog
Decrypted Text: HelloWorld