|
| 1 | +--- |
| 2 | +layout: implementation |
| 3 | +title: IPCrypt Dart Implementation |
| 4 | +description: Dart implementation of IPCrypt supporting all three encryption modes with native Dart SDK. |
| 5 | +permalink: /implementations/dart/ |
| 6 | +language: Dart |
| 7 | +repository: https://github.com/elliotwutingfeng/ipcrypt |
| 8 | +package_manager: pub.dev |
| 9 | +package_url: https://pub.dev/packages/ipcrypt |
| 10 | +examples: |
| 11 | + - title: Deterministic Encryption |
| 12 | + description: Encrypt an IP address using deterministic mode |
| 13 | + code: | |
| 14 | + import 'package:ipcrypt/ipcrypt.dart'; |
| 15 | +
|
| 16 | + void main() { |
| 17 | + // 16-byte key as hex string |
| 18 | + String key = '2b7e151628aed2a6abf7158809cf4f3c'; |
| 19 | + |
| 20 | + // Encrypt an IPv4 address |
| 21 | + String encryptedIp = ipCryptDeterministic.encrypt('192.0.2.1', key); |
| 22 | + print('Encrypted IP: $encryptedIp'); |
| 23 | + |
| 24 | + // Decrypt back to the original |
| 25 | + String decryptedIp = ipCryptDeterministic.decrypt(encryptedIp, key); |
| 26 | + print('Decrypted IP: $decryptedIp'); |
| 27 | + } |
| 28 | + - title: Non-Deterministic Encryption |
| 29 | + description: Encrypt an IP address using non-deterministic mode with KIASU-BC |
| 30 | + code: | |
| 31 | + import 'package:ipcrypt/ipcrypt.dart'; |
| 32 | + import 'dart:typed_data'; |
| 33 | +
|
| 34 | + void main() { |
| 35 | + // 16-byte key as hex string |
| 36 | + String key = '2b7e151628aed2a6abf7158809cf4f3c'; |
| 37 | + |
| 38 | + // 8-byte tweak for randomization |
| 39 | + Uint8List tweak = Uint8List.fromList([0x08, 0xe0, 0xc2, 0x89, 0xbf, 0xf2, 0x3b, 0x7c]); |
| 40 | + |
| 41 | + // Encrypt an IPv4 address |
| 42 | + Uint8List encryptedData = ipCryptNonDeterministic.encrypt('192.0.2.1', key, tweak); |
| 43 | + print('Encrypted data: ${encryptedData.map((b) => b.toRadixString(16).padLeft(2, '0')).join()}'); |
| 44 | + |
| 45 | + // Decrypt back to the original |
| 46 | + String decryptedIp = ipCryptNonDeterministic.decrypt(encryptedData, key); |
| 47 | + print('Decrypted IP: $decryptedIp'); |
| 48 | + } |
| 49 | + - title: Extended Non-Deterministic Encryption |
| 50 | + description: Encrypt an IP address using extended non-deterministic mode |
| 51 | + code: | |
| 52 | + import 'package:ipcrypt/ipcrypt.dart'; |
| 53 | + import 'dart:typed_data'; |
| 54 | +
|
| 55 | + void main() { |
| 56 | + // 32-byte key as hex string (two 16-byte keys) |
| 57 | + String key = '2b7e151628aed2a6abf7158809cf4f3c1032547698badcfeefcdab8967452301'; |
| 58 | + |
| 59 | + // 16-byte tweak for enhanced randomization |
| 60 | + Uint8List tweak = Uint8List.fromList([0x21, 0xbd, 0x18, 0x34, 0xbc, 0x08, 0x8c, 0xd2, 0xb4, 0xec, 0xbe, 0x30, 0xb7, 0x08, 0x98, 0xd7]); |
| 61 | + |
| 62 | + // Encrypt an IPv6 address |
| 63 | + Uint8List encryptedData = ipCryptExtendedNonDeterministic.encrypt('2001:db8::1', key, tweak); |
| 64 | + print('Encrypted data: ${encryptedData.map((b) => b.toRadixString(16).padLeft(2, '0')).join()}'); |
| 65 | + |
| 66 | + // Decrypt back to the original |
| 67 | + String decryptedIp = ipCryptExtendedNonDeterministic.decrypt(encryptedData, key); |
| 68 | + print('Decrypted IP: $decryptedIp'); |
| 69 | + } |
| 70 | +--- |
| 71 | + |
| 72 | +## IPCrypt Dart Implementation |
| 73 | + |
| 74 | +A Dart implementation of IPCrypt that provides IP address encryption and obfuscation methods following the IPCrypt specification. This implementation supports all three encryption modes and is designed for native Dart applications. |
| 75 | + |
| 76 | +## Installation |
| 77 | + |
| 78 | +Add IPCrypt to your `pubspec.yaml` file: |
| 79 | + |
| 80 | +```yaml |
| 81 | +dependencies: |
| 82 | + ipcrypt: ^latest_version |
| 83 | +``` |
| 84 | +
|
| 85 | +Then run: |
| 86 | +
|
| 87 | +```bash |
| 88 | +dart pub get |
| 89 | +``` |
| 90 | + |
| 91 | +## Requirements |
| 92 | + |
| 93 | +- Dart SDK 3.8 or higher |
| 94 | +- No external dependencies |
| 95 | + |
| 96 | +## Usage |
| 97 | + |
| 98 | +The Dart implementation provides three encryption methods as global functions: |
| 99 | + |
| 100 | +1. `ipCryptDeterministic` - Deterministic encryption using AES-128 |
| 101 | +2. `ipCryptNonDeterministic` - Non-deterministic encryption using KIASU-BC |
| 102 | +3. `ipCryptExtendedNonDeterministic` - Extended non-deterministic encryption |
| 103 | + |
| 104 | +### Deterministic Encryption |
| 105 | + |
| 106 | +```dart |
| 107 | +import 'package:ipcrypt/ipcrypt.dart'; |
| 108 | +
|
| 109 | +void main() { |
| 110 | + // 16-byte key as hex string |
| 111 | + String key = '2b7e151628aed2a6abf7158809cf4f3c'; |
| 112 | + |
| 113 | + // Encrypt an IP address |
| 114 | + String encryptedIp = ipCryptDeterministic.encrypt('192.0.2.1', key); |
| 115 | + print('Encrypted IP: $encryptedIp'); |
| 116 | + |
| 117 | + // Decrypt back to the original |
| 118 | + String decryptedIp = ipCryptDeterministic.decrypt(encryptedIp, key); |
| 119 | + print('Decrypted IP: $decryptedIp'); |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### Non-Deterministic Encryption |
| 124 | + |
| 125 | +```dart |
| 126 | +import 'package:ipcrypt/ipcrypt.dart'; |
| 127 | +import 'dart:typed_data'; |
| 128 | +
|
| 129 | +void main() { |
| 130 | + // 16-byte key as hex string |
| 131 | + String key = '2b7e151628aed2a6abf7158809cf4f3c'; |
| 132 | + |
| 133 | + // 8-byte tweak for randomization |
| 134 | + Uint8List tweak = Uint8List.fromList([0x08, 0xe0, 0xc2, 0x89, 0xbf, 0xf2, 0x3b, 0x7c]); |
| 135 | + |
| 136 | + // Encrypt an IP address |
| 137 | + Uint8List encryptedData = ipCryptNonDeterministic.encrypt('192.0.2.1', key, tweak); |
| 138 | + print('Encrypted data: ${encryptedData.map((b) => b.toRadixString(16).padLeft(2, '0')).join()}'); |
| 139 | + |
| 140 | + // Decrypt back to the original |
| 141 | + String decryptedIp = ipCryptNonDeterministic.decrypt(encryptedData, key); |
| 142 | + print('Decrypted IP: $decryptedIp'); |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +### Extended Non-Deterministic Encryption |
| 147 | + |
| 148 | +```dart |
| 149 | +import 'package:ipcrypt/ipcrypt.dart'; |
| 150 | +import 'dart:typed_data'; |
| 151 | +
|
| 152 | +void main() { |
| 153 | + // 32-byte key as hex string (two 16-byte keys) |
| 154 | + String key = '2b7e151628aed2a6abf7158809cf4f3c1032547698badcfeefcdab8967452301'; |
| 155 | + |
| 156 | + // 16-byte tweak for enhanced randomization |
| 157 | + Uint8List tweak = Uint8List.fromList([0x21, 0xbd, 0x18, 0x34, 0xbc, 0x08, 0x8c, 0xd2, 0xb4, 0xec, 0xbe, 0x30, 0xb7, 0x08, 0x98, 0xd7]); |
| 158 | + |
| 159 | + // Encrypt an IPv6 address |
| 160 | + Uint8List encryptedData = ipCryptExtendedNonDeterministic.encrypt('2001:db8::1', key, tweak); |
| 161 | + print('Encrypted data: ${encryptedData.map((b) => b.toRadixString(16).padLeft(2, '0')).join()}'); |
| 162 | + |
| 163 | + // Decrypt back to the original |
| 164 | + String decryptedIp = ipCryptExtendedNonDeterministic.decrypt(encryptedData, key); |
| 165 | + print('Decrypted IP: $decryptedIp'); |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +## API Reference |
| 170 | + |
| 171 | +### Deterministic Encryption |
| 172 | + |
| 173 | +```dart |
| 174 | +// Encrypt an IP address |
| 175 | +String ipCryptDeterministic.encrypt(String ipAddress, String key) |
| 176 | +
|
| 177 | +// Decrypt an encrypted IP address |
| 178 | +String ipCryptDeterministic.decrypt(String encryptedIp, String key) |
| 179 | +``` |
| 180 | + |
| 181 | +**Parameters:** |
| 182 | +- `ipAddress`: IPv4 or IPv6 address as a string |
| 183 | +- `encryptedIp`: Encrypted IP address as a string |
| 184 | +- `key`: 16-byte key as a hex string (32 characters) |
| 185 | + |
| 186 | +**Returns:** IP address as a string |
| 187 | + |
| 188 | +### Non-Deterministic Encryption |
| 189 | + |
| 190 | +```dart |
| 191 | +// Encrypt an IP address |
| 192 | +Uint8List ipCryptNonDeterministic.encrypt(String ipAddress, String key, Uint8List tweak) |
| 193 | +
|
| 194 | +// Decrypt encrypted data |
| 195 | +String ipCryptNonDeterministic.decrypt(Uint8List encryptedData, String key) |
| 196 | +``` |
| 197 | + |
| 198 | +**Parameters:** |
| 199 | +- `ipAddress`: IPv4 or IPv6 address as a string |
| 200 | +- `key`: 16-byte key as a hex string (32 characters) |
| 201 | +- `tweak`: 8-byte tweak as Uint8List |
| 202 | +- `encryptedData`: Encrypted data as Uint8List (24 bytes: 8-byte tweak + 16-byte ciphertext) |
| 203 | + |
| 204 | +**Returns:** Encrypted data as Uint8List or decrypted IP address as string |
| 205 | + |
| 206 | +### Extended Non-Deterministic Encryption |
| 207 | + |
| 208 | +```dart |
| 209 | +// Encrypt an IP address |
| 210 | +Uint8List ipCryptExtendedNonDeterministic.encrypt(String ipAddress, String key, Uint8List tweak) |
| 211 | +
|
| 212 | +// Decrypt encrypted data |
| 213 | +String ipCryptExtendedNonDeterministic.decrypt(Uint8List encryptedData, String key) |
| 214 | +``` |
| 215 | + |
| 216 | +**Parameters:** |
| 217 | +- `ipAddress`: IPv4 or IPv6 address as a string |
| 218 | +- `key`: 32-byte key as a hex string (64 characters) |
| 219 | +- `tweak`: 16-byte tweak as Uint8List |
| 220 | +- `encryptedData`: Encrypted data as Uint8List (32 bytes: 16-byte tweak + 16-byte ciphertext) |
| 221 | + |
| 222 | +**Returns:** Encrypted data as Uint8List or decrypted IP address as string |
| 223 | + |
| 224 | +## Implementation Details |
| 225 | + |
| 226 | +The Dart implementation includes: |
| 227 | + |
| 228 | +1. **IP Address Conversion**: Functions to convert between IP addresses and 16-byte representations |
| 229 | +2. **AES-128 Implementation**: Native Dart implementation for deterministic mode |
| 230 | +3. **KIASU-BC Implementation**: Implementation of the KIASU-BC tweakable block cipher |
| 231 | +4. **Extended Encryption**: Enhanced encryption with larger keys and tweaks |
| 232 | + |
| 233 | +## Supported Features |
| 234 | + |
| 235 | +- ✅ IPv4 address encryption/decryption |
| 236 | +- ✅ IPv6 address encryption/decryption |
| 237 | +- ✅ Deterministic encryption (AES-128) |
| 238 | +- ✅ Non-deterministic encryption (KIASU-BC) |
| 239 | +- ✅ Extended non-deterministic encryption |
| 240 | +- ✅ Custom tweak support |
| 241 | +- ✅ Native Dart implementation (no external dependencies) |
| 242 | + |
| 243 | +## Performance Considerations |
| 244 | + |
| 245 | +This Dart implementation prioritizes compatibility and ease of use. For high-performance applications requiring millions of operations per second, consider using the C, Rust, or Go implementations. |
| 246 | + |
| 247 | +The Dart implementation is well-suited for: |
| 248 | +- Mobile applications (Flutter) |
| 249 | +- Web applications |
| 250 | +- Server-side Dart applications |
| 251 | +- Development and testing environments |
| 252 | + |
| 253 | +## Package Information |
| 254 | + |
| 255 | +- **Package Name**: `ipcrypt` |
| 256 | +- **Platform**: [pub.dev](https://pub.dev/packages/ipcrypt) |
| 257 | +- **License**: ISC License |
| 258 | +- **Repository**: [GitHub](https://github.com/elliotwutingfeng/ipcrypt) |
| 259 | + |
| 260 | +## Example Project |
| 261 | + |
| 262 | +```dart |
| 263 | +import 'package:ipcrypt/ipcrypt.dart'; |
| 264 | +import 'dart:typed_data'; |
| 265 | +
|
| 266 | +void demonstrateIPCrypt() { |
| 267 | + // Test data |
| 268 | + String key16 = '2b7e151628aed2a6abf7158809cf4f3c'; |
| 269 | + String key32 = '2b7e151628aed2a6abf7158809cf4f3c1032547698badcfeefcdab8967452301'; |
| 270 | + String ipv4 = '192.0.2.1'; |
| 271 | + String ipv6 = '2001:db8::1'; |
| 272 | + |
| 273 | + print('=== IPCrypt Dart Demo ===\n'); |
| 274 | + |
| 275 | + // Deterministic encryption |
| 276 | + print('1. Deterministic Encryption:'); |
| 277 | + String encIPv4 = ipCryptDeterministic.encrypt(ipv4, key16); |
| 278 | + String decIPv4 = ipCryptDeterministic.decrypt(encIPv4, key16); |
| 279 | + print(' Original: $ipv4'); |
| 280 | + print(' Encrypted: $encIPv4'); |
| 281 | + print(' Decrypted: $decIPv4\n'); |
| 282 | + |
| 283 | + // Non-deterministic encryption |
| 284 | + print('2. Non-Deterministic Encryption:'); |
| 285 | + Uint8List tweak8 = Uint8List.fromList([0x08, 0xe0, 0xc2, 0x89, 0xbf, 0xf2, 0x3b, 0x7c]); |
| 286 | + Uint8List encData = ipCryptNonDeterministic.encrypt(ipv4, key16, tweak8); |
| 287 | + String decIPv4ND = ipCryptNonDeterministic.decrypt(encData, key16); |
| 288 | + print(' Original: $ipv4'); |
| 289 | + print(' Encrypted: ${encData.map((b) => b.toRadixString(16).padLeft(2, '0')).join()}'); |
| 290 | + print(' Decrypted: $decIPv4ND\n'); |
| 291 | + |
| 292 | + // Extended non-deterministic encryption |
| 293 | + print('3. Extended Non-Deterministic Encryption:'); |
| 294 | + Uint8List tweak16 = Uint8List.fromList([0x21, 0xbd, 0x18, 0x34, 0xbc, 0x08, 0x8c, 0xd2, 0xb4, 0xec, 0xbe, 0x30, 0xb7, 0x08, 0x98, 0xd7]); |
| 295 | + Uint8List encDataExt = ipCryptExtendedNonDeterministic.encrypt(ipv6, key32, tweak16); |
| 296 | + String decIPv6Ext = ipCryptExtendedNonDeterministic.decrypt(encDataExt, key32); |
| 297 | + print(' Original: $ipv6'); |
| 298 | + print(' Encrypted: ${encDataExt.map((b) => b.toRadixString(16).padLeft(2, '0')).join()}'); |
| 299 | + print(' Decrypted: $decIPv6Ext'); |
| 300 | +} |
| 301 | +
|
| 302 | +void main() { |
| 303 | + demonstrateIPCrypt(); |
| 304 | +} |
| 305 | +``` |
| 306 | + |
| 307 | +## License |
| 308 | + |
| 309 | +The Dart implementation is licensed under the ISC License. |
0 commit comments