-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDesCbcStringEncryption.dart
163 lines (139 loc) · 5.68 KB
/
DesCbcStringEncryption.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import 'dart:convert';
import 'dart:typed_data';
import 'dart:math';
import 'package:dart_des/dart_des.dart';
main() {
/* add in pubspec.yaml:
dependencies:
dart_des: ^1.0.0
# https://pub.dev/packages/dart_des/
*/
printC("\n# # # SECURITY WARNING: This code is provided for achieve # # #");
printC("# # # compatibility between different programming languages. # # #");
printC("# # # It is not necessarily fully secure. # # #");
printC("# # # It uses the OUTDATED and UNSECURE algorithm DES. # # #");
printC("# # # It uses FIXED key and IV's that should NEVER used, # # #");
printC("# # # instead use random generated keys and IVs. # # #");
printC("# # # DO NOT USE THIS CODE IN PRODUCTION # # #\n");
final plaintext = 'The quick brown fox jumps over the lazy dog';
printC("plaintext: " + plaintext);
printC("\nDES CBC String encryption with fixed key & iv");
// fixed encryption key 8 bytes long
String desEncryptionKey = "12345678";
printC("desEncryptionKey: " + desEncryptionKey);
// encryption
printC("\n* * * Encryption * * *");
String desCiphertextBase64 = desCbcEncryptToBase64(desEncryptionKey, plaintext);
printC("ciphertext: " + desCiphertextBase64);
printC("output is (Base64) iv : (Base64) ciphertext");
printC("\n* * * Decryption * * *");
printC("ciphertext: " + desCiphertextBase64);
printC("input is (Base64) iv : (Base64) ciphertext");
String desDecryptedtext = desCbcDecryptFromBase64(desEncryptionKey, desCiphertextBase64);
printC("plaintext: " + desDecryptedtext);
printC("\nTriple DES CBC String encryption with fixed key (16 bytes) & iv");
// fixed encryption key 16 bytes long
String des2EncryptionKey = "1234567890123456";
printC("des2EncryptionKey: " + des2EncryptionKey);
// encryption
printC("\n* * * Encryption * * *");
String des2CiphertextBase64 = des3CbcEncryptToBase64(des2EncryptionKey, plaintext);
printC("ciphertext: " + des2CiphertextBase64);
printC("output is (Base64) iv : (Base64) ciphertext");
printC("\n* * * Decryption * * *");
printC("ciphertext: " + des2CiphertextBase64);
printC("input is (Base64) iv : (Base64) ciphertext");
String des2Decryptedtext = des3CbcDecryptFromBase64(des2EncryptionKey, des2CiphertextBase64);
printC("plaintext: " + des2Decryptedtext);
printC("\nTriple DES CBC String encryption with fixed key (24 bytes) & iv");
// fixed encryption key 24 bytes long
String des3EncryptionKey = "123456789012345678901234";
printC("des3EncryptionKey: " + des3EncryptionKey);
// encryption
printC("\n* * * Encryption * * *");
String des3CiphertextBase64 = des3CbcEncryptToBase64(des3EncryptionKey, plaintext);
printC("ciphertext: " + des3CiphertextBase64);
printC("output is (Base64) iv : (Base64) ciphertext");
printC("\n* * * Decryption * * *");
printC("ciphertext: " + des3CiphertextBase64);
printC("input is (Base64) iv : (Base64) ciphertext");
String des3Decryptedtext = des3CbcDecryptFromBase64(des3EncryptionKey, des3CiphertextBase64);
printC("plaintext: " + des3Decryptedtext);
}
String desCbcEncryptToBase64(String key, String plaintext) {
// don't use this in production
List<int> iv = generateFixedDesInitvector();
// List<int> iv = generateRandomDesInitvector();
DES desCBC = DES(
key: key.codeUnits,
mode: DESMode.CBC,
iv: iv,
paddingType: DESPaddingType.PKCS7);
List<int> encrypted = desCBC.encrypt(plaintext.codeUnits);
return base64Encoding(iv) + ":" + base64Encoding(encrypted);
}
String desCbcDecryptFromBase64(String key, String data) {
var parts = data.split(':');
var iv = base64Decoding(parts[0]);
var ciphertext = base64Decoding(parts[1]);
DES desCBC = DES(
key: key.codeUnits,
mode: DESMode.CBC,
iv: iv,
paddingType: DESPaddingType.PKCS7);
List<int> decrypted = desCBC.decrypt(ciphertext);
return utf8.decode(decrypted);
}
String des3CbcEncryptToBase64(String key, String plaintext) {
// don't use this in production
List<int> iv = generateFixedDesInitvector();
// List<int> iv = generateRandomDesInitvector();
DES3 desCBC = DES3(
key: key.codeUnits,
mode: DESMode.CBC,
iv: iv,
paddingType: DESPaddingType.PKCS7);
List<int> encrypted = desCBC.encrypt(plaintext.codeUnits);
return base64Encoding(iv) + ":" + base64Encoding(encrypted);
}
String des3CbcDecryptFromBase64(String key, String data) {
var parts = data.split(':');
var iv = base64Decoding(parts[0]);
var ciphertext = base64Decoding(parts[1]);
DES3 desCBC = DES3(
key: key.codeUnits,
mode: DESMode.CBC,
iv: iv,
paddingType: DESPaddingType.PKCS7);
List<int> decrypted = desCBC.decrypt(ciphertext);
return utf8.decode(decrypted);
}
List<int> generateRandomDesKey() {
var random = Random.secure();
// 8 bytes for des, 16 bytes for TDES 2 and 24 bytes for TDES 3
return List<int>.generate(8, (i) => random.nextInt(256));
}
List<int> generateRandomDesInitvector() {
var random = Random.secure();
return List<int>.generate(8, (i) => random.nextInt(256));
}
List<int> generateFixedDesInitvector() {
return createUint8ListFromString('12345678');
}
String base64Encoding(List<int> input){
return base64.encode(input);
}
List<int> base64Decoding(String input){
return base64.decode(input);
}
Uint8List createUint8ListFromString(String s) {
var ret = new Uint8List(s.length);
for (var i = 0; i < s.length; i++) {
ret[i] = s.codeUnitAt(i);
}
return ret;
}
void printC(String newString) {
// for compatibility reasons to FlutterEmptyConsole
print(newString);
}