Не получается скомпилировать данный код для шифратора - дешифратора
Много ошибок по коду ничего не понимаю (
public class MainActivity extends AppCompatActivity {
private static SecretKeySpec secret;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static SecretKey generateKey()
throws NoSuchAlgorithmException, InvalidKeySpecException
{
String password = null;
return secret = new SecretKeySpec(password.getBytes(), "AES");
}
public static byte[] encryptMsg(String message, SecretKey secret)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
Cipher cipher = null;
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
return cipherText;
}
public static String decryptMsg(byte[] cipherText, SecretKey secret)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException
{
Cipher cipher = null;
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret);
String decryptString = new String(cipher.doFinal(cipherText), "UTF-8");
return decryptString;
}
public void Encrypt (View view) //Зашифровать
{
EditText el1 = (EditText) findViewById(R.id.editText1);
EditText el2 = (EditText) findViewById(R.id.editText2);
SecretKey secret = generateKey();
EncUtil.encryptMsg(String toEncrypt, secret))
}
public void Decrypt (View view) //Расшифровать
{
EditText el1 = (EditText) findViewById(R.id.editText1);
EditText el2 = (EditText) findViewById(R.id.editText2);
EncUtil.decryptMsg(byte[] toDecrypt, secret))
}
}
////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:hint="Слово обычное"
android:inputType="textPersonName"
android:textColorHint="#000" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Encrypt"
android:text="Зашифровать" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#000"
android:ems="10"
android:gravity="center"
android:hint="Слово шифрованное"
android:inputType="textPersonName" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Decrypt"
android:text="Расшифровать" />
</LinearLayout>
Ответы (1 шт):
На будущее - не вставляйте код скриншотами, это чертовски неудобно. Вот я хочу процитировать ваш код, чтобы указать в нем проблему, а не могу
У вас есть метод Encrypt, который не бросает исключений. И есть метод generateKey(), вызываемый в Encrypt, который может бросит исключения. Обработки этих исключений в Encrypt у вас нет. То то есть если в generateKey что-то пойдет не так эту ошибку обработать будет некому. Либо засуньте вызов generateKey в try/catch, который ловит нужные типы исключений, либо поставьте для метода Encrypt возможность выбрасывать исключения тех типов, которые выбрасывает generateKey. Тогда try/catch нужно ставить там, где вызывается Encrypt