Skip to content

Instantly share code, notes, and snippets.

@samdods
Last active January 10, 2025 12:26
Show Gist options
  • Save samdods/61d18908667d82d45982058c859a4d2c to your computer and use it in GitHub Desktop.
Save samdods/61d18908667d82d45982058c859a4d2c to your computer and use it in GitHub Desktop.
enc
import 'dart:convert';
void main() {
String mmst = 'security_token12345678';
String encrypted = encryptXOR('this is a sensitive API key or access token', mmst);
String encBase64 = base64.encode(encrypted.codeUnits);
print('encrypted and base64: $encBase64');
String decoded = String.fromCharCodes(base64.decode(encBase64));
String decrypted = decryptXOR(decoded, mmst);
print('decoded: $decoded');
print('decrypted: $decrypted');
}
String encryptXOR(String plaintext, String key) {
List<int> plainBytes = utf8.encode(plaintext);
List<int> keyBytes = utf8.encode(key);
List<int> cipherBytes = [];
for (int i = 0; i < plainBytes.length; i++) {
cipherBytes.add(plainBytes[i] ^ keyBytes[i % keyBytes.length]);
}
return utf8.decode(cipherBytes);
}
String decryptXOR(String ciphertext, String key) {
List<int> cipherBytes = utf8.encode(ciphertext);
List<int> keyBytes = utf8.encode(key);
List<int> plainBytes = [];
for (int i = 0; i < cipherBytes.length; i++) {
plainBytes.add(cipherBytes[i] ^ keyBytes[i % keyBytes.length]);
}
return utf8.decode(plainBytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment