-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCryptoHelper.cs
196 lines (165 loc) · 7.5 KB
/
CryptoHelper.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
using System;
using System.Drawing;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace CryptoExplorer
{
public static class CryptoHelper
{
private static string _masterKeyStr = String.Empty;
private static string _ivStr=String.Empty;
private static byte[] _encryptedBytes = null;
private static int _bytes;
public static byte[] imageToByteArray(this System.Drawing.Bitmap image)
{
using (var ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
}
public static Image byteArrayToImage(this byte[] byteArrayIn)
{
using (var ms = new MemoryStream(byteArrayIn))
{
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}
/// <summary>
/// Reads an image from filepath and encrypts its pixel content (This does not encrypt the image header!)
/// Refer MSDN article on BitmapData class for details about reading & writing pixes in an image.
/// Link: https://msdn.microsoft.com/en-us/library/system.drawing.imaging.bitmapdata.aspx#Examples
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static Bitmap EncryptImage(this string filePath, CipherMode cipherMode) {
try
{
Bitmap bmp = new Bitmap(Bitmap.FromFile(filePath)); // Pick image from file path
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] contentBytes = new byte[bytes];
// Copy the content bytes into the array (This does not copy the image header!).
System.Runtime.InteropServices.Marshal.Copy(ptr, contentBytes, 0, bytes);
byte[] modifiedBytes = null;
// Modify the bytes
switch (cipherMode) {
case CipherMode.CBC:
modifiedBytes = CryptoHelper.Encrypt(contentBytes, CipherMode.CBC);
break;
case CipherMode.ECB:
modifiedBytes = CryptoHelper.Encrypt(contentBytes, CipherMode.ECB);
break;
default: break;
}
_encryptedBytes = modifiedBytes; // Store the encrypted bytes in memory, for later use in decryption routine.
_bytes = bytes;
// Copy the modified values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(modifiedBytes, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
return bmp;
}
catch (Exception ex)
{
throw ex;
}
}
public static Image DecryptImage(this Bitmap bmp, CipherMode cipherMode)
{
try
{
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Fetch the bytes to modify from the content stored during encryption
int bytes = _bytes;
byte[] contentBytes = _encryptedBytes;
// Copy the content bytes into the array (This does not copy the image header!).
// System.Runtime.InteropServices.Marshal.Copy(ptr, contentBytes, 0, bytes);
// Modify the bytes
byte[] modifiedBytes = null;
// Modify the bytes
switch (cipherMode)
{
case CipherMode.CBC:
modifiedBytes = CryptoHelper.Decrypt(contentBytes, CipherMode.CBC);
break;
case CipherMode.ECB:
modifiedBytes = CryptoHelper.Decrypt(contentBytes, CipherMode.ECB);
break;
default: break;
}
// Copy the modified values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(modifiedBytes, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
return bmp;
}
catch (Exception ex)
{
throw ex;
}
}
public static byte[] Encrypt(byte[] plainTextAsBytes, CipherMode cipherMode)
{
using (RijndaelManaged myRijndael = new RijndaelManaged { Mode = cipherMode, Padding=PaddingMode.PKCS7 })
{
myRijndael.GenerateKey();
myRijndael.GenerateIV();
// store key and IV for decryption
_masterKeyStr = Convert.ToBase64String(myRijndael.Key);
_ivStr = Convert.ToBase64String(myRijndael.IV);
ICryptoTransform encryptor = myRijndael.CreateEncryptor(myRijndael.Key, myRijndael.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(plainTextAsBytes, 0, plainTextAsBytes.Length);
csEncrypt.FlushFinalBlock();
return msEncrypt.ToArray();
}
}
}
}
public static byte[] Decrypt(byte[] cipherTextAsBytes, CipherMode cipherMode)
{
using (RijndaelManaged myRijndael = new RijndaelManaged { Mode = cipherMode, Padding = PaddingMode.PKCS7 })
{
myRijndael.Key = Convert.FromBase64String(_masterKeyStr);
myRijndael.IV = Convert.FromBase64String(_ivStr);
ICryptoTransform decryptor = myRijndael.CreateDecryptor(myRijndael.Key, myRijndael.IV);
var outputStream = new MemoryStream();
var cryptoStream = new CryptoStream(
outputStream,
decryptor,
CryptoStreamMode.Write);
cryptoStream.Write(cipherTextAsBytes, 0, cipherTextAsBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] plainTextAsBytes = outputStream.ToArray();
return plainTextAsBytes;
}
}
private static byte[] DeriveKey(string purpose, byte[] masterKey, int keySize)
{
var kdf = new Rfc2898DeriveBytes
(masterKey,
Encoding.Unicode.GetBytes(purpose),
10);
return kdf.GetBytes(keySize);
}
}
}