Guest User

Untitled

a guest
Jun 24th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.51 KB | None | 0 0
  1. package com.meo.shrimp.meo;
  2.  
  3. import android.Manifest;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.os.Environment;
  10. import android.support.annotation.NonNull;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.nbsp.materialfilepicker.MaterialFilePicker;
  18. import com.nbsp.materialfilepicker.ui.FilePickerActivity;
  19.  
  20. import org.encryptor4j.Encryptor;
  21. import org.encryptor4j.factory.KeyFactory;
  22.  
  23. import java.io.BufferedReader;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.FileOutputStream;
  28. import java.io.FileWriter;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.io.InputStreamReader;
  32. import java.io.OutputStream;
  33. import java.nio.ByteBuffer;
  34. import java.nio.channels.FileChannel;
  35. import java.util.regex.Pattern;
  36.  
  37. import javax.crypto.Cipher;
  38. import javax.crypto.SecretKey;
  39. import javax.crypto.spec.IvParameterSpec;
  40. import javax.crypto.spec.SecretKeySpec;
  41.  
  42.  
  43. public class cryptActivity extends AppCompatActivity {
  44.  
  45. String key = "Bar12345Bar12345"; // 128 bit key
  46. String initVector = "RandomInitVector"; // 16 bytes IV
  47.  
  48. Button selectBtn;
  49. Button cryptBtn;
  50. Button decryptBtn;
  51. TextView pathView;
  52. TextView textView;
  53.  
  54. String path;
  55. String buffer;
  56.  
  57. @Override
  58. protected void onCreate(Bundle savedInstanceState) {
  59. super.onCreate(savedInstanceState);
  60. setContentView(R.layout.activity_crypt);
  61. setTitle("AES Crypt/Decrypt");
  62.  
  63. selectBtn = (Button) findViewById(R.id.selectBtn);
  64. cryptBtn = (Button) findViewById(R.id.cryptBtn);
  65. decryptBtn = (Button) findViewById(R.id.decryptBtn);
  66. pathView = (TextView) findViewById(R.id.pathView);
  67. textView = (TextView) findViewById(R.id.textView);
  68.  
  69. if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
  70. != PackageManager.PERMISSION_GRANTED){
  71. requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
  72. }
  73.  
  74. cryptBtn.setOnClickListener(new View.OnClickListener() {
  75. @Override
  76. public void onClick(View v) {
  77. try {
  78. newEncrypt(path, key, initVector);
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. });
  84.  
  85. decryptBtn.setOnClickListener(new View.OnClickListener() {
  86. @Override
  87. public void onClick(View v) {
  88. try {
  89. newDecrypt(path, key, initVector);
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. });
  95.  
  96. }
  97.  
  98. public void encrypt() throws Exception{
  99. if(path.length() !=0) {
  100. String out = encrypt(key, initVector, readFile(path));
  101.  
  102. Toast.makeText(this, "зашифровано", Toast.LENGTH_SHORT).show();
  103. createFile(this, "encrypted.txt", out);
  104. }else{
  105. Toast.makeText(this, "файл не выбран", Toast.LENGTH_SHORT).show();
  106. }
  107. }
  108.  
  109. public void decrypt() throws Exception{
  110. if(path.length() !=0) {
  111. String out = decrypt(key, initVector, readFile(path));
  112.  
  113. Toast.makeText(this, out, Toast.LENGTH_SHORT).show();
  114. createFile(this, "decrypted.txt", out);
  115. }else{
  116. Toast.makeText(this, "файл не выбран", Toast.LENGTH_SHORT).show();
  117. }
  118. }
  119.  
  120. public void newEncrypt(String fPath, String nKey, String niv) throws Exception{
  121.  
  122. SecretKeySpec secretKeySpec = new SecretKeySpec(nKey.getBytes("UTF-8"), "AES");
  123.  
  124. Encryptor encryptor = new Encryptor(secretKeySpec, "AES/CBC/PKCS5PADDING", 16);
  125.  
  126. InputStream is = null;
  127. OutputStream os = null;
  128. try {
  129. is = new FileInputStream(fPath);
  130. os = encryptor.wrapOutputStream(new FileOutputStream("encrypted.txt"));
  131. byte[] buffer = new byte[4096];
  132. int nRead;
  133. while((nRead = is.read(buffer)) != -1) {
  134. os.write(buffer, 0, nRead);
  135. }
  136. os.flush();
  137. Toast.makeText(this, "зашифровано", Toast.LENGTH_SHORT).show();
  138. } finally {
  139. if(is != null) {
  140. is.close();
  141. }
  142. if(os != null) {
  143. os.close();
  144. }
  145. }
  146. }
  147.  
  148. public void newDecrypt(String fPath, String nKey, String niv) throws Exception{
  149. SecretKeySpec secretKeySpec = new SecretKeySpec(nKey.getBytes("UTF-8"), "AES");
  150.  
  151. Encryptor encryptor = new Encryptor(secretKeySpec, "AES/CBC/PKCS5PADDING", 16);
  152.  
  153. InputStream is = null;
  154. OutputStream os = null;
  155.  
  156. try {
  157. is = encryptor.wrapInputStream(new FileInputStream(fPath));
  158. os = new FileOutputStream("decrypted.txt");
  159. byte[] buffer = new byte[4096];
  160. int nRead;
  161. while((nRead = is.read(buffer)) != -1) {
  162. os.write(buffer, 0, nRead);
  163. }
  164. os.flush();
  165. Toast.makeText(this, "расшифровано", Toast.LENGTH_SHORT).show();
  166. } finally {
  167. if(is != null) {
  168. is.close();
  169. }
  170. if(os != null) {
  171. os.close();
  172. }
  173. }
  174. }
  175.  
  176. public void select(View v){
  177. new MaterialFilePicker()
  178. .withActivity(cryptActivity.this)
  179. .withRequestCode(1000)
  180. .withFilter(Pattern.compile(".*\\.txt$")) // Filtering files and directories by file name using regexp
  181. //.withFilterDirectories(true) // Set directories filterable (false by default)
  182. .withHiddenFiles(true) // Show hidden files and folders
  183. .start();
  184. }
  185.  
  186.  
  187. public static String readFile (String filePath) throws Exception {
  188. File fl = new File(filePath);
  189. FileInputStream fin = new FileInputStream(fl);
  190. String ret = convertStreamToString(fin);
  191. //Make sure you close all streams.
  192. fin.close();
  193. return ret;
  194. }
  195.  
  196. public static String convertStreamToString(InputStream is) throws Exception {
  197. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  198. StringBuilder sb = new StringBuilder();
  199. String line = null;
  200. while ((line = reader.readLine()) != null) {
  201. sb.append(line).append("\n");
  202. }
  203. reader.close();
  204. return sb.toString();
  205. }
  206.  
  207.  
  208. public static String encrypt(String key, String initVector, String value) {
  209. try {
  210. IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
  211. SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
  212.  
  213. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
  214. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  215.  
  216. byte[] encrypted = cipher.doFinal(value.getBytes());
  217. String str = new String(encrypted);
  218.  
  219. return str;
  220. } catch (Exception ex) {
  221. ex.printStackTrace();
  222. }
  223.  
  224. return null;
  225. }
  226.  
  227. public static String decrypt(String key, String initVector, String encrypted) {
  228. try {
  229. IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
  230. SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
  231.  
  232. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
  233. cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  234.  
  235. byte[] decrypted = cipher.doFinal(encrypted.getBytes());
  236. String str = new String(decrypted);
  237.  
  238. return str;
  239. } catch (Exception ex) {
  240. ex.printStackTrace();
  241. }
  242.  
  243. return null;
  244. }
  245.  
  246. public void createFile(Context context, String sFileName, String sBody) {
  247. try {
  248. File root = new File(Environment.getExternalStorageDirectory(), ".aNotes");
  249. if (!root.exists()) {
  250. root.mkdirs();
  251. }
  252. File gpxfile = new File(root, sFileName);
  253. FileWriter writer = new FileWriter(gpxfile);
  254. writer.append(sBody);
  255. writer.flush();
  256. writer.close();
  257. } catch (IOException e) {
  258. e.printStackTrace();
  259. }
  260. }
  261.  
  262. @Override
  263. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  264. super.onActivityResult(requestCode, resultCode, data);
  265.  
  266. if (requestCode == 1000 && resultCode == RESULT_OK) {
  267. String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
  268. path = filePath;
  269. pathView.setText(path);
  270. try {
  271. textView.setText(readFile(filePath));
  272. } catch (Exception e) {
  273. e.printStackTrace();
  274. }
  275. }
  276. }
  277.  
  278. @Override
  279. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  280. switch (requestCode) {
  281. case 1001:{
  282. if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
  283. Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show();
  284. }else{
  285. Toast.makeText(this, "Permission not granted!", Toast.LENGTH_SHORT).show();
  286. finish();
  287. }
  288. }
  289. }
  290. }
  291. }
Add Comment
Please, Sign In to add comment