Guest User

Untitled

a guest
Jan 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. using Android.App;
  2. using Android.Widget;
  3. using Android.OS;
  4. using Javax.Crypto.Spec;
  5. using Java.Lang;
  6. using Java.IO;
  7. using Javax.Crypto;
  8. using System.Text;
  9.  
  10. namespace EncryTest
  11. {
  12. [Activity(Label = "EncryTest", MainLauncher = true)]
  13. public class MainActivity : Activity
  14. {
  15. long stopTime, startTime;
  16. private string sKey = "0123456789abcdef";//key,
  17. private string ivParameter = "1020304050607080";
  18. protected override void OnCreate(Bundle savedInstanceState)
  19. {
  20. base.OnCreate(savedInstanceState);
  21.  
  22. // Set our view from the "main" layout resource
  23. SetContentView(Resource.Layout.Main);
  24.  
  25. encrypt("videoplayback.mp4");
  26. decrypt("videoplayback.mp4");
  27. }
  28.  
  29. public void encrypt(string filename)
  30. {
  31.  
  32. // Here you read the cleartext.
  33. try
  34. {
  35. File extStore = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryMovies);
  36. startTime = System.DateTime.Now.Millisecond;
  37. Android.Util.Log.Error("Encryption Started", extStore + "/" + filename);
  38.  
  39. // This stream write the encrypted text. This stream will be wrapped by
  40. // another stream.
  41. createFile(filename, extStore);
  42. System.IO.FileStream fs=System.IO.File.OpenRead(extStore + "/" + filename);
  43. FileOutputStream fos = new FileOutputStream(extStore + "/" + filename + ".aes", false);
  44.  
  45. // Length is 16 byte
  46. Cipher cipher = Cipher.GetInstance("AES/CBC/PKCS5Padding");
  47. byte[] raw = System.Text.Encoding.Default.GetBytes(sKey);
  48. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  49. IvParameterSpec iv = new IvParameterSpec(System.Text.Encoding.Default.GetBytes(ivParameter));//
  50. cipher.Init(CipherMode.EncryptMode, skeySpec, iv);
  51.  
  52. // Wrap the output stream
  53. CipherInputStream cis = new CipherInputStream(fs, cipher);
  54. // Write bytes
  55. int b;
  56. byte[] d = new byte[1024 * 1024];
  57. while ((b = cis.Read(d)) != -1)
  58. {
  59. fos.Write(d, 0, b);
  60. }
  61. // Flush and close streams.
  62. fos.Flush();
  63. fos.Close();
  64. cis.Close();
  65. stopTime = System.DateTime.Now.Millisecond;
  66. Android.Util.Log.Error("Encryption Ended", extStore + "/5mbtest/" + filename + ".aes");
  67. Android.Util.Log.Error("Time Elapsed", ((stopTime - startTime) / 1000.0) + "");
  68. }
  69. catch (Exception e)
  70. {
  71. Android.Util.Log.Error("lv",e.Message);
  72. }
  73.  
  74. }
  75.  
  76.  
  77. private void createFile(string filename, File extStore)
  78. {
  79. File file = new File(extStore + "/" + filename + ".aes");
  80.  
  81. if (filename.IndexOf(".") != -1)
  82. {
  83. try
  84. {
  85. file.CreateNewFile();
  86. }
  87. catch (IOException e)
  88. {
  89. // TODO Auto-generated catch block
  90. Android.Util.Log.Error("lv",e.Message);
  91. }
  92. Android.Util.Log.Error("lv","file created");
  93. }
  94. else
  95. {
  96. file.Mkdir();
  97. Android.Util.Log.Error("lv","folder created");
  98. }
  99.  
  100. file.Mkdirs();
  101. }
  102. public void decrypt(string filename)
  103. {
  104. try
  105. {
  106.  
  107. File extStore = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryMovies);
  108. Android.Util.Log.Error("Decryption Started", extStore + "");
  109. FileInputStream fis = new FileInputStream(extStore + "/" + filename + ".aes");
  110.  
  111. createFile(filename, extStore);
  112. FileOutputStream fos = new FileOutputStream(extStore + "/" + "decrypted" + filename, false);
  113. System.IO.FileStream fs = System.IO.File.OpenWrite(extStore + "/" + "decrypted" + filename);
  114. // Create cipher
  115.  
  116. Cipher cipher = Cipher.GetInstance("AES/CBC/PKCS5Padding");
  117. byte[] raw = System.Text.Encoding.Default.GetBytes(sKey);
  118. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  119. IvParameterSpec iv = new IvParameterSpec(System.Text.Encoding.Default.GetBytes(ivParameter));//
  120. cipher.Init(CipherMode.DecryptMode, skeySpec, iv);
  121.  
  122. startTime = System.DateTime.Now.Millisecond;
  123. CipherOutputStream cos = new CipherOutputStream(fs, cipher);
  124. int b;
  125. byte[] d = new byte[1024 * 1024];
  126. while ((b = fis.Read(d)) != -1)
  127. {
  128. cos.Write(d, 0, b);
  129. }
  130.  
  131. stopTime = System.DateTime.Now.Millisecond;
  132.  
  133. Android.Util.Log.Error("Decryption Ended", extStore + "/" + "decrypted" + filename);
  134. Android.Util.Log.Error("Time Elapsed", ((stopTime - startTime) / 1000.0) + "");
  135.  
  136. cos.Flush();
  137. cos.Close();
  138. fis.Close();
  139. }
  140. catch (Exception e)
  141. {
  142. Android.Util.Log.Error("lv", e.Message);
  143. }
  144. }
  145. }
  146. }
Add Comment
Please, Sign In to add comment