什么';Android中的加密代码有什么问题?

什么';Android中的加密代码有什么问题?,android,encryption,Android,Encryption,从我用粗体显示的代码行(cipher.init(cipher.ENCRYPT_MODE,key);)程序无法运行-我收到一个异常。这条线怎么了?我正在尝试基本上加密字符串并使用此函数返回它。您的密钥必须是16、24或32字节长。AES没有其他合法尺寸。尝试重新格式化,并包括异常内容。否则,很难回答你的问题。 public String Encryption(String toEncrypt) throws Exception { Security.addProvider(new org.

从我用粗体显示的代码行(
cipher.init(cipher.ENCRYPT_MODE,key);
)程序无法运行-我收到一个异常。这条线怎么了?我正在尝试基本上加密字符串并使用此函数返回它。

您的密钥必须是16、24或32字节长。AES没有其他合法尺寸。

尝试重新格式化,并包括异常内容。否则,很难回答你的问题。
public String Encryption(String toEncrypt) throws Exception
{
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    EditText et = (EditText) findViewById(R.id.entry);
    byte[] input = toEncrypt.getBytes();
    byte[] keyBytes = "hello".getBytes();
    // et.setText("in encryption");
    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
    // et.setText("in encryption");

    cipher.init(Cipher.ENCRYPT_MODE, key);

    et.setText("in encryption");
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);
    // et.setText("in encryption");
    // return "abc";
    return cipherText.toString();