-
Notifications
You must be signed in to change notification settings - Fork 203
/
Encryption.cs
342 lines (299 loc) · 13.9 KB
/
Encryption.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
#region Copyright Syncfusion Inc. 2001-2024.
// Copyright Syncfusion Inc. 2001-2024. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using Syncfusion.Pdf.Barcode;
using System.IO;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Security;
using Syncfusion.Drawing;
using System.Reflection;
using Syncfusion.Pdf.Interactive;
namespace SampleBrowser
{
public partial class Encryption : SamplePage
{
private Context m_context;
private Spinner m_rc4Key, m_aesKey, m_encryptionKey;
private Spinner m_algorithms;
private LinearLayout advancedLinear1;
public override View GetSampleContent(Context con)
{
int numerHeight = getDimensionPixelSize(con, Resource.Dimension.numeric_txt_ht);
int width = con.Resources.DisplayMetrics.WidthPixels - 40;
LinearLayout linear = new LinearLayout(con);
linear.SetBackgroundColor(Android.Graphics.Color.White);
linear.Orientation = Orientation.Vertical;
linear.SetPadding(10, 10, 10, 10);
TextView space = new TextView(con);
space.TextSize = 10;
linear.AddView(space);
TextView text2 = new TextView(con);
text2.TextSize = 17;
text2.TextAlignment = TextAlignment.Center;
text2.Text = "This sample demonstrates how to create secure PDF document.";
text2.SetTextColor(Android.Graphics.Color.ParseColor("#262626"));
text2.SetPadding(5, 5, 5, 5);
linear.AddView(text2);
TextView space1 = new TextView(con);
space1.TextSize = 10;
linear.AddView(space1);
advancedLinear1 = new LinearLayout(con);
TextView space3 = new TextView(con);
space3.TextSize = 17;
space3.TextAlignment = TextAlignment.Center;
space3.Text = "Algorithms";
space3.SetTextColor(Android.Graphics.Color.ParseColor("#262626"));
space3.LayoutParameters = new ViewGroup.LayoutParams(width / 2, numerHeight);
advancedLinear1.AddView(space3);
m_context = con;
string[] list1 = new string[] { "RC4", "AES" };
ArrayAdapter<String> array1 = new ArrayAdapter<String>(con, Android.Resource.Layout.SimpleSpinnerItem, list1);
m_algorithms = new Spinner(con);
m_algorithms.Adapter = array1;
m_algorithms.SetSelection(1);
advancedLinear1.AddView(m_algorithms);
linear.AddView(advancedLinear1);
TextView space4 = new TextView(con);
space4.TextSize = 10;
linear.AddView(space4);
LinearLayout subLinear = new LinearLayout(con);
TextView space2 = new TextView(con);
space2.TextSize = 17;
space2.TextAlignment = TextAlignment.Center;
space2.Text = "Key Size";
space2.SetTextColor(Android.Graphics.Color.ParseColor("#262626"));
space2.LayoutParameters = new ViewGroup.LayoutParams(width / 2, numerHeight);
subLinear.AddView(space2);
string[] list = { "40 Bit", "128 Bit" };
ArrayAdapter<String> array = new ArrayAdapter<String>(con, Android.Resource.Layout.SimpleSpinnerItem, list);
m_rc4Key = new Spinner(con);
m_rc4Key.Adapter = array;
m_rc4Key.SetSelection(1);
subLinear.AddView(m_rc4Key);
//linear.AddView(subLinear);
LinearLayout subLinear2 = new LinearLayout(con);
TextView sbspace = new TextView(con);
sbspace.TextSize = 17;
sbspace.TextAlignment = TextAlignment.Center;
sbspace.Text = "Key Size";
sbspace.SetTextColor(Android.Graphics.Color.ParseColor("#262626"));
sbspace.LayoutParameters = new ViewGroup.LayoutParams(width / 2, numerHeight);
subLinear2.AddView(sbspace);
string[] list2 = { "128 Bit", "256 Bit", "256 Revision 6" };
ArrayAdapter<String> array2 = new ArrayAdapter<String>(con, Android.Resource.Layout.SimpleSpinnerItem, list2);
m_aesKey = new Spinner(con);
m_aesKey.Adapter = array2;
m_aesKey.SetSelection(1);
subLinear2.AddView(m_aesKey);
LinearLayout subLinear5 = new LinearLayout(con);
TextView sbspace2 = new TextView(con);
sbspace2.TextSize = 17;
sbspace2.TextAlignment = TextAlignment.Center;
sbspace2.Text = "Encryption Options";
sbspace2.SetTextColor(Android.Graphics.Color.ParseColor("#262626"));
sbspace2.LayoutParameters = new ViewGroup.LayoutParams(width / 2, numerHeight);
subLinear5.AddView(sbspace2);
string[] encryptionList = new string[] { "Encrypt all contents", "Encrypt all contents except metadata", "Encrypt only attachments" };
ArrayAdapter<String> array3 = new ArrayAdapter<String>(con, Android.Resource.Layout.SimpleSpinnerItem, encryptionList);
m_encryptionKey = new Spinner(con);
m_encryptionKey.Adapter = array3;
m_encryptionKey.SetSelection(0);
subLinear5.AddView(m_encryptionKey);
//linear.AddView(subLinear5);
LinearLayout subLinear3 = new LinearLayout(con);
TextView space5 = new TextView(con);
space5.TextSize = 10;
subLinear3.AddView(space5);
TextView text4 = new TextView(con);
text4.TextSize = 17;
text4.TextAlignment = TextAlignment.Center;
text4.Text = "User Password : password";
text4.SetTextColor(Android.Graphics.Color.ParseColor("#262626"));
text4.SetPadding(5, 5, 5, 5);
subLinear3.AddView(text4);
linear.AddView(subLinear3);
LinearLayout subLinear4 = new LinearLayout(con);
TextView text5 = new TextView(con);
text5.TextSize = 17;
text5.TextAlignment = TextAlignment.Center;
text5.Text = "Owner Password : syncfusion";
text5.SetTextColor(Android.Graphics.Color.ParseColor("#262626"));
text5.SetPadding(5, 5, 5, 5);
subLinear4.AddView(text5);
linear.AddView(subLinear4);
Button button1 = new Button(con);
button1.Text = "Generate PDF";
button1.Click += OnButtonClicked;
linear.AddView(button1);
m_algorithms.ItemSelected += (object sender, AdapterView.ItemSelectedEventArgs e) =>
{
String selectedItem = array1.GetItem(e.Position);
if (selectedItem.Equals("AES"))
{
linear.RemoveView(button1);
linear.RemoveView(subLinear3);
linear.RemoveView(subLinear4);
linear.RemoveView(subLinear);
linear.AddView(subLinear2);
linear.AddView(subLinear5);
linear.AddView(subLinear3);
linear.AddView(subLinear4);
linear.AddView(button1);
}
else
{
linear.RemoveView(button1);
linear.RemoveView(subLinear3);
linear.RemoveView(subLinear4);
linear.RemoveView(subLinear2);
linear.RemoveView(subLinear5);
m_encryptionKey.SetSelection(0);
linear.AddView(subLinear);
linear.AddView(subLinear3);
linear.AddView(subLinear4);
linear.AddView(button1);
}
};
m_encryptionKey.ItemSelected += (object sender, AdapterView.ItemSelectedEventArgs e) =>
{
String selectedItem = array3.GetItem(e.Position);
if (selectedItem.Equals("Encrypt only attachments"))
{
linear.RemoveView(button1);
linear.RemoveView(subLinear4);
linear.RemoveView(subLinear3);
linear.RemoveView(subLinear5);
linear.RemoveView(subLinear2);
linear.AddView(subLinear2);
linear.AddView(subLinear5);
linear.AddView(subLinear3);
linear.AddView(button1);
}
else
{
linear.RemoveView(button1);
linear.RemoveView(subLinear4);
linear.RemoveView(subLinear3);
linear.RemoveView(subLinear5);
linear.RemoveView(subLinear2);
linear.AddView(subLinear2);
linear.AddView(subLinear5);
linear.AddView(subLinear3);
linear.AddView(subLinear4);
linear.AddView(button1);
}
};
return linear;
}
private int getDimensionPixelSize(Context con, int id)
{
return con.Resources.GetDimensionPixelSize(id);
}
void OnButtonClicked(object sender, EventArgs e)
{
//Create new PDF document.
PdfDocument document = new PdfDocument();
//Add page to the PDF document.
PdfPage page = document.Pages.Add();
PdfGraphics graphics = page.Graphics;
//Create font object.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Bold);
PdfBrush brush = PdfBrushes.Black;
//Document security
PdfSecurity security = document.Security;
if (this.m_algorithms.SelectedItemPosition == 0)
{ security.Algorithm = PdfEncryptionAlgorithm.RC4;
if (this.m_rc4Key.SelectedItemPosition == 0)
{
security.KeySize = PdfEncryptionKeySize.Key40Bit;
}
else if (this.m_rc4Key.SelectedItemPosition == 1)
{
security.KeySize = PdfEncryptionKeySize.Key128Bit;
}
}
else if (this.m_algorithms.SelectedItemPosition == 1)
{
security.Algorithm = PdfEncryptionAlgorithm.AES;
if (this.m_aesKey.SelectedItemPosition == 0)
{
security.KeySize = PdfEncryptionKeySize.Key128Bit;
}
else if (this.m_aesKey.SelectedItemPosition == 1)
{
security.KeySize = PdfEncryptionKeySize.Key256Bit;
}
else if (this.m_aesKey.SelectedItemPosition == 2)
{
security.KeySize = PdfEncryptionKeySize.Key256BitRevision6;
}
}
if (this.m_encryptionKey.SelectedItemPosition == 0)
{
security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContents;
}
else if (this.m_encryptionKey.SelectedItemPosition == 1)
{
security.EncryptionOptions = PdfEncryptionOptions.EncryptAllContentsExceptMetadata;
}
else if (this.m_encryptionKey.SelectedItemPosition == 2)
{
//Read the file
Stream file = typeof(Encryption).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Samples.PDF.Assets.Products.xml");
//Creates an attachment
PdfAttachment attachment = new PdfAttachment("Products.xml", file);
attachment.ModificationDate = DateTime.Now;
attachment.Description = "About Syncfusion";
attachment.MimeType = "application/txt";
//Adds the attachment to the document
document.Attachments.Add(attachment);
security.EncryptionOptions = PdfEncryptionOptions.EncryptOnlyAttachments;
}
security.OwnerPassword = "syncfusion";
security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FullQualityPrint;
security.UserPassword = "password";
string text = "Security options:\n\n" + String.Format("KeySize: {0}\n\nEncryption Algorithm: {4}\n\nOwner Password: {1}\n\nPermissions: {2}\n\n" +
"UserPassword: {3}", security.KeySize, security.OwnerPassword, security.Permissions, security.UserPassword, security.Algorithm);
if (this.m_algorithms.SelectedItemPosition == 1)
{
if (this.m_aesKey.SelectedItemPosition == 2)
{
text += String.Format("\n\nRevision: {0}", "Revision 6");
}
else if (this.m_aesKey.SelectedItemPosition == 1)
{
text += String.Format("\n\nRevision: {0}", "Revision 5");
}
}
// Draw String.
graphics.DrawString("Document is Encrypted with following settings", font, brush, Syncfusion.Drawing.PointF.Empty);
font = new PdfStandardFont(PdfFontFamily.TimesRoman, 11f, PdfFontStyle.Bold);
graphics.DrawString(text, font, brush, new Syncfusion.Drawing.PointF(0, 40));
MemoryStream stream = new MemoryStream();
//Save the PDF document.
document.Save(stream);
document.Close();
if (stream != null)
{
SaveAndroid androidSave = new SaveAndroid();
androidSave.Save("Securepdf.pdf", "application/pdf", stream, m_context);
}
}
}
}