Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.meo.shrimp.meo;
- import android.Manifest;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.nbsp.materialfilepicker.MaterialFilePicker;
- import com.nbsp.materialfilepicker.ui.FilePickerActivity;
- import org.encryptor4j.Encryptor;
- import org.encryptor4j.factory.KeyFactory;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel;
- import java.util.regex.Pattern;
- import javax.crypto.Cipher;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- public class cryptActivity extends AppCompatActivity {
- String key = "Bar12345Bar12345"; // 128 bit key
- String initVector = "RandomInitVector"; // 16 bytes IV
- Button selectBtn;
- Button cryptBtn;
- Button decryptBtn;
- TextView pathView;
- TextView textView;
- String path;
- String buffer;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_crypt);
- setTitle("AES Crypt/Decrypt");
- selectBtn = (Button) findViewById(R.id.selectBtn);
- cryptBtn = (Button) findViewById(R.id.cryptBtn);
- decryptBtn = (Button) findViewById(R.id.decryptBtn);
- pathView = (TextView) findViewById(R.id.pathView);
- textView = (TextView) findViewById(R.id.textView);
- if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
- != PackageManager.PERMISSION_GRANTED){
- requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
- }
- cryptBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- newEncrypt(path, key, initVector);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- decryptBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- newDecrypt(path, key, initVector);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public void encrypt() throws Exception{
- if(path.length() !=0) {
- String out = encrypt(key, initVector, readFile(path));
- Toast.makeText(this, "зашифровано", Toast.LENGTH_SHORT).show();
- createFile(this, "encrypted.txt", out);
- }else{
- Toast.makeText(this, "файл не выбран", Toast.LENGTH_SHORT).show();
- }
- }
- public void decrypt() throws Exception{
- if(path.length() !=0) {
- String out = decrypt(key, initVector, readFile(path));
- Toast.makeText(this, out, Toast.LENGTH_SHORT).show();
- createFile(this, "decrypted.txt", out);
- }else{
- Toast.makeText(this, "файл не выбран", Toast.LENGTH_SHORT).show();
- }
- }
- public void newEncrypt(String fPath, String nKey, String niv) throws Exception{
- SecretKeySpec secretKeySpec = new SecretKeySpec(nKey.getBytes("UTF-8"), "AES");
- Encryptor encryptor = new Encryptor(secretKeySpec, "AES/CBC/PKCS5PADDING", 16);
- InputStream is = null;
- OutputStream os = null;
- try {
- is = new FileInputStream(fPath);
- os = encryptor.wrapOutputStream(new FileOutputStream("encrypted.txt"));
- byte[] buffer = new byte[4096];
- int nRead;
- while((nRead = is.read(buffer)) != -1) {
- os.write(buffer, 0, nRead);
- }
- os.flush();
- Toast.makeText(this, "зашифровано", Toast.LENGTH_SHORT).show();
- } finally {
- if(is != null) {
- is.close();
- }
- if(os != null) {
- os.close();
- }
- }
- }
- public void newDecrypt(String fPath, String nKey, String niv) throws Exception{
- SecretKeySpec secretKeySpec = new SecretKeySpec(nKey.getBytes("UTF-8"), "AES");
- Encryptor encryptor = new Encryptor(secretKeySpec, "AES/CBC/PKCS5PADDING", 16);
- InputStream is = null;
- OutputStream os = null;
- try {
- is = encryptor.wrapInputStream(new FileInputStream(fPath));
- os = new FileOutputStream("decrypted.txt");
- byte[] buffer = new byte[4096];
- int nRead;
- while((nRead = is.read(buffer)) != -1) {
- os.write(buffer, 0, nRead);
- }
- os.flush();
- Toast.makeText(this, "расшифровано", Toast.LENGTH_SHORT).show();
- } finally {
- if(is != null) {
- is.close();
- }
- if(os != null) {
- os.close();
- }
- }
- }
- public void select(View v){
- new MaterialFilePicker()
- .withActivity(cryptActivity.this)
- .withRequestCode(1000)
- .withFilter(Pattern.compile(".*\\.txt$")) // Filtering files and directories by file name using regexp
- //.withFilterDirectories(true) // Set directories filterable (false by default)
- .withHiddenFiles(true) // Show hidden files and folders
- .start();
- }
- public static String readFile (String filePath) throws Exception {
- File fl = new File(filePath);
- FileInputStream fin = new FileInputStream(fl);
- String ret = convertStreamToString(fin);
- //Make sure you close all streams.
- fin.close();
- return ret;
- }
- public static String convertStreamToString(InputStream is) throws Exception {
- BufferedReader reader = new BufferedReader(new InputStreamReader(is));
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null) {
- sb.append(line).append("\n");
- }
- reader.close();
- return sb.toString();
- }
- public static String encrypt(String key, String initVector, String value) {
- try {
- IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
- SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
- cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
- byte[] encrypted = cipher.doFinal(value.getBytes());
- String str = new String(encrypted);
- return str;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return null;
- }
- public static String decrypt(String key, String initVector, String encrypted) {
- try {
- IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
- SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
- cipher.init(Cipher.DECRYPT_MODE, skeySpec);
- byte[] decrypted = cipher.doFinal(encrypted.getBytes());
- String str = new String(decrypted);
- return str;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return null;
- }
- public void createFile(Context context, String sFileName, String sBody) {
- try {
- File root = new File(Environment.getExternalStorageDirectory(), ".aNotes");
- if (!root.exists()) {
- root.mkdirs();
- }
- File gpxfile = new File(root, sFileName);
- FileWriter writer = new FileWriter(gpxfile);
- writer.append(sBody);
- writer.flush();
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == 1000 && resultCode == RESULT_OK) {
- String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
- path = filePath;
- pathView.setText(path);
- try {
- textView.setText(readFile(filePath));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- switch (requestCode) {
- case 1001:{
- if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
- Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(this, "Permission not granted!", Toast.LENGTH_SHORT).show();
- finish();
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment