· 5 years ago · Dec 09, 2019, 11:46 PM
1package com.klyovan.textencryptioncs;
2
3import android.app.Fragment;
4import android.os.Bundle;
5import android.support.v7.app.AppCompatActivity;
6import android.util.Base64;
7import android.view.View;
8import android.widget.Button;
9import android.widget.EditText;
10import android.widget.TextView;
11import android.widget.Toast;
12
13import java.io.UnsupportedEncodingException;
14import java.security.InvalidKeyException;
15import java.security.NoSuchAlgorithmException;
16import java.security.spec.InvalidKeySpecException;
17import java.security.spec.KeySpec;
18
19import javax.crypto.Cipher;
20import javax.crypto.NoSuchPaddingException;
21import javax.crypto.SecretKey;
22import javax.crypto.SecretKeyFactory;
23import javax.crypto.spec.DESedeKeySpec;
24
25import butterknife.BindView;
26import butterknife.ButterKnife;
27
28/**
29 * A simple {@link Fragment} subclass.
30 * Created by Klyovan Ihor
31 */
32
33public class MainActivity extends AppCompatActivity implements View.OnClickListener {
34
35 //encryption type
36 public static final String EncryptionScheme = "DESede";
37 //text format
38 private static final String TestUniCodeFormat = "UTF8";
39 //
40 byte[] arrayBytes;
41 SecretKey key;
42 @BindView(R.id.et_encryption_key)
43 EditText etEncryptionKey;
44 @BindView(R.id.et_text_to_encrypt)
45 EditText etTextToEncrypt;
46 @BindView(R.id.btn_encrypt)
47 Button btnEncrypt;
48 @BindView(R.id.tv_encryption_out)
49 TextView tvEncryptionOutPut;
50 @BindView(R.id.et_encrypted_text)
51 EditText etEncryptedText;
52 @BindView(R.id.btn_decrypt)
53 Button btnDecrypt;
54 @BindView(R.id.tv_decrypted_output)
55 TextView tvDecryptionOut;
56 private KeySpec ks;
57 private SecretKeyFactory skf;
58 private Cipher cipher;
59 private String myEncryptionKey, myEncryptionScheme;
60 private String strEncrytion, strDecryption;
61
62 @Override
63 protected void onCreate(Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
65 setContentView(R.layout.activity_main);
66 ButterKnife.bind(this);
67
68 btnEncrypt.setOnClickListener(this);
69 btnDecrypt.setOnClickListener(this);
70
71
72 }
73
74
75 @Override
76 public void onClick(View v) {
77
78 switch (v.getId()) {
79 case R.id.btn_encrypt:
80 if (etEncryptionKey.getText().length() >= 24) {
81 if (etTextToEncrypt.getText().toString().length() > 1) {
82 initEncryption();
83 strEncrytion = encrypt(etTextToEncrypt.getText().toString());
84 tvEncryptionOutPut.setText(strEncrytion);
85 etEncryptedText.setText(strEncrytion);
86 } else {
87 etTextToEncrypt.setError("введите текст");
88 }
89 } else {
90 etEncryptionKey.setError("ключ должен быть больше 24");
91 }
92 break;
93 case R.id.btn_decrypt:
94 if (etEncryptionKey.getText().length() >= 24) {
95 initEncryption();
96 strDecryption = decrypt(etEncryptedText.getText().toString());
97 tvDecryptionOut.setText(strDecryption);
98
99 } else {
100 etEncryptionKey.setError("ключ должен быть больше 24");
101 }
102 break;
103 }
104 }
105 private void initEncryption() {
106 //getting encryption
107 myEncryptionKey = etEncryptionKey.getText().toString();
108 myEncryptionScheme = EncryptionScheme;
109 try {
110 Toast.makeText(this, "Triple DES зашифрован", Toast.LENGTH_SHORT).show();
111 arrayBytes = myEncryptionKey.getBytes(TestUniCodeFormat);
112 ks = new DESedeKeySpec(arrayBytes);
113 skf = SecretKeyFactory.getInstance(myEncryptionScheme);
114 cipher = Cipher.getInstance(myEncryptionScheme);
115 key = skf.generateSecret(ks);
116 } catch (UnsupportedEncodingException e) {
117 e.printStackTrace();
118 } catch (NoSuchPaddingException e) {
119 e.printStackTrace();
120 } catch (NoSuchAlgorithmException e) {
121 e.printStackTrace();
122 } catch (InvalidKeyException e) {
123 e.printStackTrace();
124 } catch (InvalidKeySpecException e) {
125 e.printStackTrace();
126 }
127 }
128 public String encrypt(String unencryptedString) {
129 String encryptedString = null;
130 try {
131 cipher.init(Cipher.ENCRYPT_MODE, key);
132 byte[] plainText = unencryptedString.getBytes(TestUniCodeFormat);
133 byte[] encryptedText = cipher.doFinal(plainText);
134 encryptedString = new String(Base64.encodeToString(encryptedText, Base64.DEFAULT));
135 } catch (Exception e) {
136 e.printStackTrace();
137 }
138 return encryptedString;
139 }
140
141
142 public String decrypt(String encryptedString) {
143 String decryptedText = null;
144 try {
145 cipher.init(Cipher.DECRYPT_MODE, key);
146 byte[] encryptedText = Base64.decode(encryptedString, Base64.DEFAULT);
147 byte[] plainText = cipher.doFinal(encryptedText);
148 decryptedText = new String(plainText);
149 } catch (Exception e) {
150 e.printStackTrace();
151 }
152 return decryptedText;
153 }
154
155}