有什么不对的加密code在Android的? [英] what's wrong with this encryption code in android?

查看:135
本文介绍了有什么不对的加密code在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();

这是我已经加粗了code线( cipher.init(Cipher.ENCRYPT_MODE,关键); )的程序没有working-我正在一个例外。什么是错的这条线?我想基本上都是加密的字符串,并用此功能返回。

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.

推荐答案

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

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

这篇关于有什么不对的加密code在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆