【问题标题】:Getting errors in Android Studio that I can't solve在 Android Studio 中出现我无法解决的错误
【发布时间】:2016-03-29 18:26:31
【问题描述】:
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 行,我收到一个未处理的异常错误。有人知道如何解决这个问题吗?我不知道我是否只是不导入某些东西,但我能找到的所有东西都包含在我已经拥有的导入中。

【问题讨论】:

  • 请注明代码行数和错误,这样我们就不用自己数行数了..
  • 您是否以某种方式确保可以通过 libs 文件夹中的 .jar 文件、gradle 等导入 Apache Commons?

标签: android


【解决方案1】:

这是我正在使用的SimpleCrypto 类。它基本上是this thread 和我为了正常工作而进行的一些小修复的组合。

SimpleCrypto.java

public class SimpleCrypto{
public static String encrypt(String seed, String cleartext) throws Exception
{
    byte[] rawKey = getRawKey(seed.getBytes());
    byte[] result = encrypt(rawKey, cleartext.getBytes());
    return toHex(result);
}

public static String decrypt(String seed, String encrypted) throws Exception
{
    byte[] rawKey = getRawKey(seed.getBytes());
    byte[] enc = toByte(encrypted);
    byte[] result = decrypt(rawKey, enc);
    return new String(result);
}

private static byte[] getRawKey(byte[] seed) throws Exception
{
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(seed);
    kgen.init(128, sr); // 192 and 256 bits may not be available
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}


private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception
{
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception
{
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

public static String toHex(String txt)
{
    return toHex(txt.getBytes());
}

public static String fromHex(String hex)
{
    return new String(toByte(hex));
}

public static byte[] toByte(String hexString)
{
    int len = hexString.length() / 2;
    byte[] result = new byte[len];
    for (int i = 0; i < len; i++)
        result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
    return result;
}

public static String toHex(byte[] buf)
{
    if (buf == null)
        return "";
    StringBuffer result = new StringBuffer(2 * buf.length);
    for (int i = 0; i < buf.length; i++)
    {
        appendHex(result, buf[i]);
    }
    return result.toString();
}

private final static String HEX = "0123456789ABCDEF";

private static void appendHex(StringBuffer sb, byte b)
{
    sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
}

你可以这样使用它:

String cryptoString = SimpleCrypto.encrypt(masterpassword, cleartext); String clearString = SimpleCrypto.decrypt(masterpassword, crypto);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 2017-05-30
    相关资源
    最近更新 更多