0
package com.androidedsoft.aesencryptor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import android.util.Base64;
import org.apache.commons.codec.Decoder;
import org.apache.commons.codec.Encoder;
public class MainActivity extends ActionBarActivity {
public static SecretKey secretKey;
static Cipher cipher;
Button encryptbutton;
String plainText;
public static void main(String[] args) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128); //key is 128 bit
SecretKey secretKey = keyGenerator.generateKey();
cipher = Cipher.getInstance("AES"); //sets as AES encryption type
}
public void btnClick() {
encryptbutton = (Button) findViewById(R.id.encryptbutton);
encryptbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
錯誤1獲取的錯誤,我解決不了
String encryptedText = encrypt(plainText, secretKey);
續
EditText Resultbox = (EditText) findViewById(R.id.Resultbox);
Resultbox.setText(String.valueOf(encryptedText));
}
});
}
public static String encrypt(String plainText, SecretKey secretKey)
throws Exception {
byte[] plainTextByte = plainText.getBytes();
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedByte = cipher.doFinal(plainTextByte);
錯誤2
Encoder encoder = Base64.getEncoder();
String encryptedText = encoder.encodeToString(encryptedByte);
續
return encryptedText;
} //defines the encryption function
public static String decrypt(String encryptedText, SecretKey secretKey)
throws Exception {
錯誤3
Decoder decoder = Base64.getDecoder();
續
byte[] encryptedTextByte = (byte[]) decoder.decode(encryptedText);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
String decryptedText = new String(decryptedByte);
return decryptedText;
} //defines the decryption function
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在線路51,52和58我越來越無法解析方法錯誤,不知道如何解決這些問題。在第39行,我得到一個未處理的異常錯誤。任何人有任何想法如何解決這個問題?我不知道我是不是進口東西,但是我能找到的所有東西都包含在我已有的進口產品中。
請提供的代碼在noticable方式的線條和錯誤,使我們不」我們不得不自己來計算線.. –
你有沒有確保Apache Commons的導入可以工作,可以通過libs文件夾中的.jar文件,gradle等? – FWeigl