-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from javad-zobeidi/dev
Fixed bugs and added tags to the template
- Loading branch information
Showing
11 changed files
with
202 additions
and
174 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,53 @@ | ||
import 'dart:convert'; | ||
import 'dart:math'; | ||
import 'dart:typed_data'; | ||
import 'package:encrypt/encrypt.dart'; | ||
|
||
class VaniaEncryption { | ||
/// Encrypts the given [plainText] using the given [passphrase]. | ||
static IV iv = IV(Uint8List(16)); | ||
|
||
/// Encrypts the given [plainText] using the provided [passphrase]. | ||
/// | ||
/// The method generates a random salt and derives a key using the given | ||
/// [passphrase] and the generated salt. The derived key is then used to | ||
/// encrypt the given [plainText] using a simple XOR operation. | ||
/// This method first encodes the [plainText] using Base64 and UTF-8 encoding. | ||
/// Then, it creates a cryptographic key from the [passphrase] and uses the | ||
/// AES encryption algorithm to encrypt the text with a predefined initialization | ||
/// vector (IV). The result is an encrypted string returned in Base64 format. | ||
/// | ||
/// The encrypted bytes are then combined with the salt bytes and encoded | ||
/// using Base64. | ||
/// Parameters: | ||
/// - [plainText]: The text to be encrypted. | ||
/// - [passphrase]: The passphrase used to generate the encryption key. | ||
/// | ||
/// The resulting Base64 encoded string can be decrypted using the | ||
/// [decryptString] method. | ||
/// Returns: | ||
/// A Base64 encoded string representing the encrypted text. | ||
static String encryptString(String plainText, String passphrase) { | ||
final saltBytes = | ||
List<int>.generate(16, (_) => Random.secure().nextInt(256)); | ||
|
||
final plainBytes = utf8.encode(plainText); | ||
|
||
final keyBytes = _deriveKey(passphrase, saltBytes, plainBytes.length); | ||
|
||
final encryptedBytes = List<int>.generate( | ||
plainBytes.length, | ||
(i) => plainBytes[i] ^ keyBytes[i], | ||
); | ||
|
||
final combinedBytes = <int>[]; | ||
combinedBytes.addAll(saltBytes); | ||
combinedBytes.addAll(encryptedBytes); | ||
|
||
return base64.encode(combinedBytes); | ||
plainText = base64.encode(utf8.encode(plainText)); | ||
Key key = Key.fromUtf8(passphrase.substring(0, 32)); | ||
Encrypter encrypter = Encrypter(AES(key)); | ||
Encrypted encrypted = encrypter.encrypt(plainText, iv: iv); | ||
return encrypted.base64; | ||
} | ||
|
||
/// Decrypts the given [encryptedText] using the given [passphrase]. | ||
/// Decrypts the given [encryptedText] using the provided [passphrase]. | ||
/// | ||
/// The method first decodes the given [encryptedText] from Base64 and | ||
/// splits the decoded bytes into a salt and the encrypted bytes. | ||
/// This method first creates a cryptographic key from the [passphrase]. | ||
/// It then uses the AES encryption algorithm to decrypt the [encryptedText] | ||
/// with a predefined initialization vector (IV). The decrypted text is | ||
/// decoded from Base64 and UTF-8 encoding to return the original plain text. | ||
/// | ||
/// The method then derives a key using the given [passphrase] and the | ||
/// salt bytes. The derived key is then used to decrypt the encrypted | ||
/// bytes using a simple XOR operation. | ||
/// Parameters: | ||
/// - [encryptedText]: The text to be decrypted, in Base64 format. | ||
/// - [passphrase]: The passphrase used to generate the decryption key. | ||
/// | ||
/// The decrypted bytes are then decoded from UTF-8 and returned as a string. | ||
/// Returns: | ||
/// The original plain text if decryption is successful, or an empty | ||
/// string if decryption fails. | ||
static String decryptString(String encryptedText, String passphrase) { | ||
final allBytes = base64.decode(encryptedText); | ||
|
||
final saltBytes = allBytes.sublist(0, 16); | ||
|
||
final encryptedBytes = allBytes.sublist(16); | ||
|
||
final keyBytes = _deriveKey(passphrase, saltBytes, encryptedBytes.length); | ||
|
||
final plainBytes = List<int>.generate( | ||
encryptedBytes.length, | ||
(i) => encryptedBytes[i] ^ keyBytes[i], | ||
); | ||
|
||
return utf8.decode(plainBytes); | ||
} | ||
|
||
/// Derives a key from the given [passphrase] and [saltBytes] with the given [length]. | ||
/// | ||
/// The method works by first encoding the [passphrase] into bytes and then | ||
/// combining the bytes with the given [saltBytes]. The combined bytes are then | ||
/// repeated until their length is at least [length]. The resulting bytes are | ||
/// then trimmed to [length] bytes and returned as the derived key. | ||
static List<int> _deriveKey( | ||
String passphrase, List<int> saltBytes, int length) { | ||
final passBytes = utf8.encode(passphrase); | ||
|
||
final combined = <int>[]; | ||
combined.addAll(passBytes); | ||
combined.addAll(saltBytes); | ||
|
||
final repeated = <int>[]; | ||
while (repeated.length < length) { | ||
repeated.addAll(combined); | ||
try { | ||
Key key = Key.fromUtf8(passphrase.substring(0, 32)); | ||
Encrypter encrypter = Encrypter(AES(key)); | ||
String decrypted = encrypter.decrypt64(encryptedText, iv: iv); | ||
return utf8.decode(base64.decode(decrypted)); | ||
} catch (error) { | ||
return ''; | ||
} | ||
|
||
return repeated.sublist(0, length); | ||
} | ||
} |
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
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
Oops, something went wrong.