-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryptionELIservice.cs
377 lines (326 loc) · 14.9 KB
/
EncryptionELIservice.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using Org.BouncyCastle.Bcpg;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Security;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace EncryptionDecryptionClassLibrary
{
public static class EncryptionDecryptionService
{
static readonly string PASSWORD_HASH = "fsdf#4345VDXVVXc";
static readonly string SALT_KEY = "KLltyL1k34S41lr0";
static readonly string VI_KEY = "zUe@cz8cz%j9HWE3y!";
const int BUFFER_SIZE = 1 << 16; // should always be power of 2
public static string DecryptText(string encryptedText)
{
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
byte[] keyBytes = new Rfc2898DeriveBytes(PASSWORD_HASH, Encoding.ASCII.GetBytes(SALT_KEY)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };
var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VI_KEY));
var memoryStream = new MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
}
public static string EncryptText(string plainText)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] keyBytes = new Rfc2898DeriveBytes(PASSWORD_HASH, Encoding.ASCII.GetBytes(SALT_KEY)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VI_KEY));
byte[] cipherTextBytes;
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
cryptoStream.Close();
}
memoryStream.Close();
}
return Convert.ToBase64String(cipherTextBytes);
}
public static string publicKey(Stream inputStream)
{
string output;
using (StreamReader reader = new StreamReader(inputStream, Encoding.Default, true))
{
output = reader.ReadToEnd();
}
return output;
}
public static void GetString(Stream inputStream, string decriptedFile)
{
using (StreamReader reader = new StreamReader(inputStream, Encoding.Default, true))
{
String strDataLine = "";
using (StreamWriter streamWriter = new StreamWriter(decriptedFile, true, Encoding.UTF8))
{
strDataLine = reader.ReadLine();
if (strDataLine != null)
{
while ((strDataLine = reader.ReadLine()) != null)
{
streamWriter.WriteLine(strDataLine);
}
}
}
}
}
private static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyId, char[] pass)
{
PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyId);
if (pgpSecKey == null)
{
return null;
}
return pgpSecKey.ExtractPrivateKey(pass);
}
#region Decrypt methods
/// <summary>
/// Decrypt Pgp Data
/// </summary>
/// <param name="inputStream">Public Input Stream</param>
/// <param name="privateKeyStream">Private Key Stream</param>
/// <param name="passPhrase">Pass Phrase</param>
/// <returns>Decrypt</returns>
public static void DecryptPgpData(string decriptedFile, Stream inputStream, Stream privateKeyStream, string passPhrase)
{
PgpObjectFactory pgpFactory = new PgpObjectFactory(PgpUtilities.GetDecoderStream(inputStream));
// find secret key
PgpSecretKeyRingBundle pgpKeyRing = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream));
PgpObject pgp = null;
if (pgpFactory != null)
{
pgp = pgpFactory.NextPgpObject();
}
// the first object might be a PGP marker packet.
PgpEncryptedDataList encryptedData = null;
if (pgp is PgpEncryptedDataList)
{
encryptedData = (PgpEncryptedDataList)pgp;
}
else
{
encryptedData = (PgpEncryptedDataList)pgpFactory.NextPgpObject();
}
if (encryptedData == null)
throw new PgpException("Encrypted data is NULL.");
PgpPrivateKey privateKey = null;
PgpPublicKeyEncryptedData pubKeyData = null;
foreach (PgpPublicKeyEncryptedData pubKeyDataItem in encryptedData.GetEncryptedDataObjects())
{
privateKey = FindSecretKey(pgpKeyRing, pubKeyDataItem.KeyId, passPhrase.ToCharArray());
if (privateKey != null)
{
pubKeyData = pubKeyDataItem;
break;
}
}
if (privateKey == null)
throw new ArgumentException("Secret key for message not found.");
PgpObjectFactory plainFact = null;
using (Stream clear = pubKeyData.GetDataStream(privateKey))
{
plainFact = new PgpObjectFactory(clear);
}
PgpObject message = plainFact.NextPgpObject();
if (message is PgpCompressedData)
{
PgpCompressedData compressedData = (PgpCompressedData)message;
PgpObjectFactory pgpCompressedFactory = null;
using (Stream compDataIn = compressedData.GetDataStream())
{
pgpCompressedFactory = new PgpObjectFactory(compDataIn);
}
message = pgpCompressedFactory.NextPgpObject();
PgpLiteralData literalData = null;
if (message is PgpOnePassSignatureList)
message = pgpCompressedFactory.NextPgpObject();
literalData = (PgpLiteralData)message;
using (Stream unc = literalData.GetInputStream())
{
GetString(unc, decriptedFile);
}
}
else if (message is PgpLiteralData)
{
PgpLiteralData literalData = (PgpLiteralData)message;
using (Stream unc = literalData.GetInputStream())
{
GetString(unc, decriptedFile);
}
}
else if (message is PgpOnePassSignatureList)
{
throw new PgpException("Encrypted message contains a signed message - not literal data.");
}
else
{
throw new PgpException("Message is not a simple encrypted file - type unknown.");
}
}
/// <summary>
/// Decrypt File
/// </summary>
/// <param name="encryptedStream">Encrypted Stream</param>
/// <param name="privateKey">Private Key</param>
/// <param name="passPhrase">Pass Phrase</param>
/// <returns>Decrypt</returns>
public static void DecryptDataFile(string decriptedFile, FileStream encryptedStream, string privateKey, string passPhrase)
{
Stream pgpFile = null;
using (pgpFile = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(privateKey)))
DecryptPgpData(decriptedFile, encryptedStream, pgpFile, passPhrase);
}
#endregion
/// <summary>
/// Read public key
/// </summary>
/// <param name="fileName">Public key file name</param>
/// <returns>PGP public key</returns>
public static PgpPublicKey ReadPublicKey(string fileName, bool readFromFile = false)
{
if (readFromFile)
{
using (Stream keyIn = File.OpenRead(fileName))
{
return ReadPublicKey(publicKey(keyIn));
}
}
else
{
byte[] byteArray = Encoding.UTF8.GetBytes(fileName);
MemoryStream streamTemp = new MemoryStream(byteArray);
return ReadPublicKey(streamTemp);
}
}
/// <summary>
/// A simple routine that opens a key ring file and loads the first available key suitable for encryption
/// </summary>
/// <param name="input">Input stream</param>
/// <returns>PGP public key</returns>
private static PgpPublicKey ReadPublicKey(Stream input)
{
PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(input));
// We just loop through the collection till we find a key suitable for encryption, in the real
// world you would probably want to be a bit smarter about this.
foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
{
foreach (PgpPublicKey key in keyRing.GetPublicKeys())
{
if (key.IsEncryptionKey)
return key;
}
}
throw new ArgumentException("Can't find encryption key in key ring.");
}
#region Encrypt methods
/// <summary>
/// Encrypt input file
/// </summary>
/// <param name="inputFileName">Input file name</param>
/// <param name="outputFileName">Output file name</param>
/// <param name="publicKeyFileName">Public key PgpPublicKey</param>
/// <param name="armor">Armored output</param>
/// <param name="withIntegrityCheck">Integrity check flag</param>
public static void EncryptFile(string inputFileName, string outputFileName, PgpPublicKey encKey, bool armor, bool withIntegrityCheck)
{
using (Stream output = File.Create(outputFileName))
{
EncryptFile(output, inputFileName, encKey, armor, withIntegrityCheck);
}
}
/// <summary>
/// Encrypt input file
/// </summary>
/// <param name="inputFileName">Input file name</param>
/// <param name="outputFileName">Output file name</param>
/// <param name="publicKeyFileName">Public key file name</param>
/// <param name="armor">Armored output</param>
/// <param name="withIntegrityCheck">Integrity check flag</param>
public static void EncryptFile(string inputFileName, string outputFileName, string publicKeyFileName, bool armor, bool withIntegrityCheck)
{
PgpPublicKey encKey = ReadPublicKey(publicKeyFileName);
using (Stream output = File.Create(outputFileName))
{
EncryptFile(output, inputFileName, encKey, armor, withIntegrityCheck);
}
}
/// <summary>
/// Encrypt input file
/// </summary>
/// <param name="outputStream">Output file stream</param>
/// <param name="inputFileName">Input file name</param>
/// <param name="publicKey">PGP public key</param>
/// <param name="armor">Armored output flag</param>
/// <param name="withIntegrityCheck">Integrity check flag</param>
private static void EncryptFile(Stream outputStream, string inputFileName, PgpPublicKey publicKey, bool armor, bool withIntegrityCheck)
{
if (armor)
outputStream = new ArmoredOutputStream(outputStream);
// byte[] bytes = CompressFile(inputFileName, CompressionAlgorithmTag.Zip);
FileInfo compresedFile = CompressFile(inputFileName);
PgpEncryptedDataGenerator encGen = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom());
encGen.AddMethod(publicKey);
//Stream cOut = encGen.Open(outputStream, bytes.Length);
//cOut.Write(bytes, 0, bytes.Length);
//cOut.Close();
using (FileStream inputStream = compresedFile.OpenRead())
{
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = inputStream.Read(buf, 0, buf.Length)) > 0)
{
outputStream.Write(buf, 0, len);
}
}
if (compresedFile.Exists)
compresedFile.Delete();
if (armor)
outputStream.Close();
}
#endregion
/// <summary>
/// Compress file
/// </summary>
/// <param name="fileName">File name</param>
/// <param name="compressedFileName">Algorithm tag</param>
/// <returns>Byte array</returns>
private static FileInfo CompressFile(string inputFileName)
{
string outPutFileName = inputFileName + DateTime.Now.ToString("yyyyMMddHHMMss");
FileInfo outputFileInfo = new FileInfo(outPutFileName);
// Create new file.
using (FileStream outputStream = outputFileInfo.Create())
{
PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip);
PgpUtilities.WriteFileToLiteralData(comData.Open(outputStream), PgpLiteralData.Binary, new FileInfo(inputFileName));
comData.Close();
}
return outputFileInfo;
}
/// <summary>
/// Compress file
/// </summary>
/// <param name="fileName">File name</param>
/// <param name="algorithm">Algorithm tag</param>
/// <returns>Byte array</returns>
private static byte[] CompressFile(string fileName, CompressionAlgorithmTag algorithm)
{
MemoryStream bOut = new MemoryStream();
PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm);
// PgpUtilities.WriteFileToLiteralData(comData.Open(bOut), PgpLiteralData.Utf8, new FileInfo(fileName));
PgpUtilities.WriteFileToLiteralData(comData.Open(bOut), PgpLiteralData.Binary, new FileInfo(fileName));
comData.Close();
return bOut.ToArray();
}
}
}