https://github.com/hamed-rezaee/dart_cipher
https://github.com/hamed-rezaee/dart_cipher
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/hamed-rezaee/dart_cipher
- Owner: hamed-rezaee
- Created: 2023-04-07T01:54:21.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-07T03:22:06.000Z (over 2 years ago)
- Last Synced: 2025-01-03T19:32:08.254Z (9 months ago)
- Language: Dart
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Cipher
The `Cipher` class is a Dart implementation of a simple encryption algorithm. It provides methods to encrypt and decrypt strings using a key with a random salt value.
## Usage
### Encrypting a string
To encrypt a string using the `Cipher` class, you can call the `encrypt` method and pass in the message and key parameters. The method will return the encrypted message with a random salt value in the following format: `encodedSalt:encodedMessage`.
```dart
String encryptedMessage = Cipher.encrypt('secret message', 'my secret key');
print(encryptedMessage);
```### Decrypting a string
To decrypt a string using the `Cipher` class, you can call the `decrypt` method and pass in the ciphertext and key parameters. The method will return the original message.
```dart
String decryptedMessage = Cipher.decrypt('NlU5cHpPbGkxVnBzaXp3Y3E3M3MwZzNlcjM4RHpWQ2Q=:F4JxKj4kNC/7Wwl2tN/AC1TmTt/8moNerTHkq3I=', 'my secret key');
print(decryptedMessage);
```## How it works
The Cipher class generates a random 32-byte salt value and combines it with the key using the UTF-8 encoding. The resulting byte array is used as a key to encrypt or decrypt the input message. The algorithm uses a simple byte-by-byte XOR operation, combined with an addition or subtraction of the key byte value. The key bytes are cycled through for each byte of the input message. The resulting encrypted message is base64 encoded along with the salt value and returned as a string in the format `encodedSalt:encodedMessage`.