开发者

What's wrong with this encryption code in Android?

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();

From the 开发者_开发问答code line that I have bolded (cipher.init(Cipher.ENCRYPT_MODE, key);) the programme is not working- I am getting an exception. What is wrong with this line? I am trying to basically encrypt a string and return it with this function.


Your key must be 16, 24, or 32 bytes long. No other sizes are legal for AES.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜