PoisonGive 2016-10-01 09:05 采纳率: 0%
浏览 2522
已结题

Android AES加密后无法解密 求大神指导

    package com.example.aes;
    import java.io.UnsupportedEncodingException;
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    /**
    * AES加密解密算法
    */
    public class AESHelper {
    /** 算法/模式/填充 **/
    private static final String CipherMode = "AES/CBC/PKCS5Padding";
    /** 创建密钥 **/
    private static SecretKeySpec createKey(String key) {
    byte[] data = null;
    if (key == null) {
    key = "";
    }
    StringBuffer sb = new StringBuffer(16);
    sb.append(key);
    while (sb.length() < 16) {
    sb.append("0");
    }
    if (sb.length() > 16) {
    sb.setLength(16);
    }
    try {
    data = sb.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    return new SecretKeySpec(data, "AES");
    }
    private static IvParameterSpec createIV(String password) {
    byte[] data = null;
    if (password == null) {
    password = "";
    }
    StringBuffer sb = new StringBuffer(16);
    sb.append(password);
    while (sb.length() < 16) {
    sb.append("0");
    }
    if (sb.length() > 16) {
    sb.setLength(16);
    }
    try {
    data = sb.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    return new IvParameterSpec(data);
    }
    /** 加密字节数据 **/
    public static byte[] encrypt(byte[] content, String password, String iv) {
    try {
    SecretKeySpec key = createKey(password);
    Cipher cipher = Cipher.getInstance(CipherMode);
    cipher.init(Cipher.ENCRYPT_MODE, key, createIV(iv));
    byte[] result = cipher.doFinal(content);
    return result;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    /** 加密(结果为16进制字符串) **/
    public static String encrypt(String content, String password, String iv) {
    byte[] data = null;
    try {
    data = content.getBytes("UTF-8");
    } catch (Exception e) {
    e.printStackTrace();
    }
    data = encrypt(data, password, iv);
    String result = byte2hex(data);
    return result;
    }
    /** 解密字节数组 **/
    public static byte[] decrypt(byte[] content, String password, String iv) {
    try {
    SecretKeySpec key = createKey(password);
    Cipher cipher = Cipher.getInstance(CipherMode);
    cipher.init(Cipher.DECRYPT_MODE, key, createIV(iv));
    byte[] result = cipher.doFinal(content);
    return result;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    /** 解密(输出结果为字符串) **/
    public static String decrypt(String content, String password, String iv) {
    byte[] data = null;
    try {
    data = hex2byte(content);
    } catch (Exception e) {
    e.printStackTrace();
    }
    data = decrypt(data, password, iv);
    if (data == null)
    return null;
    String result = null;
    try {
    result = new String(data, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    return result;
    }
    /** 字节数组转成16进制字符串 **/
    public static String byte2hex(byte[] b) { // 一个字节的数,
    StringBuffer sb = new StringBuffer(b.length * 2);
    String tmp = "";
    for (int n = 0; n < b.length; n++) {
    // 整数转成十六进制表示
    tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
    if (tmp.length() == 1) {
    sb.append("0");
    }
    sb.append(tmp);
    }
    return sb.toString().toUpperCase(); // 转成大写
    }
    /** 将hex字符串转换成字节数组 **/
    private static byte[] hex2byte(String inputString) {
    if (inputString == null || inputString.length() < 2) {
    return new byte[0];
    }
    inputString = inputString.toLowerCase();
    int l = inputString.length() / 2;
    byte[] result = new byte[l];
    for (int i = 0; i < l; ++i) {
    String tmp = inputString.substring(2 * i, 2 * i + 2);
    result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
    }
    return result;
    }
    }



            ------------------------------------------------------------
    public class MainActivity extends Activity implements OnClickListener {
    private Button mJiami, mJiemi;
    private EditText mName;
    private String KEY="fe8f95cb0f968676572ab9f46d8a261b";
    private String IV ="0F968676572AB9F4";
    private String name;
    String aes;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    initView();
    }
    private void initView() {
    mName = (EditText) findViewById(R.id.editText1);
    name = mName.getText().toString().trim();
    mJiami = (Button) findViewById(R.id.button1);
    mJiemi = (Button) findViewById(R.id.button2);
    mJiami.setOnClickListener(this);
    mJiemi.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
    String help = null;
    switch (v.getId()) {

    case R.id.button1:
    try {
    aes = AESHelper.encrypt(name, KEY, IV);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Toast.makeText(this, "加密后值为:"+aes, 0).show();

    break;
    case R.id.button2:
    try {
    help = AESHelper.decrypt(aes, KEY, IV);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    if(help.length()==0){
    Toast.makeText(this, "解密失败!", 0).show();
    }else{
    Toast.makeText(this, help, 0).show();
    }
    break;
    default:
    break;
    }
    }
    }
  • 写回答

1条回答 默认 最新

  • devmiao 2016-10-01 16:05
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!