Android的对称加密与Base64加密

对称加密是最快速、最简单的一种加密方式,加密(encryption)与解密(decryption)用的是同样的密钥(secret key)。常用的对称加密方式为:DES,AES。

DES的加密解密实例:

public class MainActivity extends AppCompatActivity {
    private EditText des_input;
    private EditText des_password;
    private TextView show_des_encrypt;
    private TextView show_des_decrypt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        des_input = ((EditText) findViewById(R.id.des_input));
        des_password = ((EditText) findViewById(R.id.des_password));
        show_des_encrypt = ((TextView) findViewById(R.id.show_des_encrypt));
        show_des_decrypt = ((TextView) findViewById(R.id.show_des_decrypt));
    }
    /**
     * DES 加密
     * @param view
     */
    public void btnDESEncrypt(View view) {
        //将DES加密写成工具类
//        要加密内容
        byte[] data = des_input.getText().toString().getBytes();
        byte[] password = des_password.getText().toString().getBytes();
        //工具类DESUtils中方法encrypt返回值是什么类型
        byte[] result = DESUtils.encrypt(data, password);
        //处理加密结果
        byte[] encode = Base64.encode(result, Base64.DEFAULT);
        //显示一下加密的结果
        show_des_encrypt.setText(new String(encode));
    }

    /**
     * DES 进行解密
     * @param view
     */
    public void btnDESDecrypt(View view) {
        //获取解密的数据
        byte[] data = show_des_encrypt.getText().toString().getBytes();
        byte[] decode = Base64.decode(data, Base64.DEFAULT);
        //获取密码
        byte[] password = des_password.getText().toString().getBytes();
        byte[] result = DESUtils.decrypt(decode,password);
        //对解密数据进行处理
        show_des_decrypt.setText(new String(result));

    }
}

public class DESUtils {
    public static byte[] encrypt(byte[] data, byte[] password) {
        byte[] ret = null;
        //判断参数是否符合条件
        if(data!=null&&data.length>0){
            if(password!=null&&password.length==8){
                try {
                    //1、创造加密引擎
                    Cipher cipher = Cipher.getInstance("DES");
                    //2、初始化
                    SecretKeySpec key = new SecretKeySpec(password,"DES");
                    cipher.init(Cipher.ENCRYPT_MODE,key);
                    //3、对数据进行加密
                    ret =cipher.doFinal(data);

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    public static byte[] decrypt(byte[] data, byte[] password) {
        byte[] ret = null;

        if(data!=null&&data.length>0){
            if(password!=null&&password.length==8){
                try {
                    //1、获取解密引擎
                    Cipher cipher =Cipher.getInstance("DES");

                    //2、初始化
                    SecretKeySpec key = new SecretKeySpec(password,"DES");

                    cipher.init(Cipher.DECRYPT_MODE,key);

                    //3、解密
                    ret = cipher.doFinal(data);

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }

            }

        }
        return ret;
    }
}

AES的加密解密实例:

注意:输入的password和ivParams为16位

public class AESUtils {
    public static byte[] encrypt(byte[] data, byte[] password) {
        byte[] ret = null;

        if(data!=null&&data.length>0){
            if(password!=null&&password.length==16){

                try {
                    //1、创造加密引擎
                    Cipher cipher = Cipher.getInstance("AES");

                    //2、初始化,无法使用SecretKeyFactory
                    SecretKeySpec key = new SecretKeySpec(password,"AES");

                    cipher.init(Cipher.ENCRYPT_MODE,key);

                    //3、加密
                    ret = cipher.doFinal(data);

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    public static byte[] decrypt(byte[] data, byte[] password) {
        byte[] ret = null;

        if(data!=null&&data.length>0){
            if(password!=null&&password.length==16){

                try {
                    //1、创造加密引擎
                    Cipher cipher = Cipher.getInstance("AES");

                    //2、初始化,无法使用SecretKeyFactory
                    SecretKeySpec key = new SecretKeySpec(password,"AES");

                    cipher.init(Cipher.DECRYPT_MODE,key);

                    //3、加密
                    ret = cipher.doFinal(data);

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    /**
     * AES 第二种加密算法
     * @param data
     * @param password
     * @param ivParams
     * @return
     */
    public static byte[] encrypt2(byte[] data, byte[] password, byte[] ivParams) {
        byte[] ret = null;
        if(data!=null&&data.length>0){
            if(password!=null&&password.length==16&&ivParams!=null&&ivParams.length==16){
                try {
                    //1、创造加密引擎,CBC:加密模式;PKCS5Padding:填充模式
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

                    //2、初始化,无法使用SecretKeyFactory
                    SecretKeySpec key = new SecretKeySpec(password,"AES");

                    //相当于第二个密码,向量参数,加密级别更高
                    IvParameterSpec iv_params = new IvParameterSpec(ivParams);

                    cipher.init(Cipher.ENCRYPT_MODE,key,iv_params);

                    //3、加密
                    ret = cipher.doFinal(data);

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                } catch (InvalidAlgorithmParameterException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }
}

public class MainActivity extends AppCompatActivity {
    private EditText aes_input;
    private EditText aes_password;
    private TextView show_aes_decrypt;
    private TextView show_aes_encrypt;
    private byte[] password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        aes_input = ((EditText) findViewById(R.id.aes_input));
        aes_password = ((EditText) findViewById(R.id.aes_password));
        show_aes_encrypt = ((TextView) findViewById(R.id.show_aes_encrypt));
        show_aes_decrypt = ((TextView) findViewById(R.id.show_aes_decrypt));
    }

    public void qqq(View view) {

        byte[] data = aes_input.getText().toString().getBytes();
        password = aes_password.getText().toString().getBytes();
        //采用第二套加密算法,需要一个向量参数,相当于另一套密码
        //指定向量参数(密码)
        String iv = "0123456789abcdef";
        //提供一个向量参数
        byte[] ivParams = iv.getBytes();
        byte[] result = AESUtils.encrypt2(data,password,ivParams);
        //对返回来的结果进行处理,显示
        byte[] encode = Base64.encode(result, Base64.DEFAULT);
        show_aes_encrypt.setText(new String(encode));

    }

    public void www(View view) {

        byte[] data = show_aes_encrypt.getText().toString().getBytes();

        //对data进行处理
        data = Base64.decode(data,Base64.DEFAULT);

        byte[] result = AESUtils.decrypt(data,password);

        if (result != null) {
            show_aes_decrypt.setText(new String(result));
        }else {
            show_aes_decrypt.setText("解密失败");
        }
    }
}

Base64加密实例:

public class Base64Activity extends AppCompatActivity {

    private EditText input;
    private TextView show_base64_encode;
    private TextView show_base64_decode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base64);

        initView();
//      initData();
    }

    private void initData() {
        String input = this.input.getText().toString();

        if (input != null) {
            byte[] bytes = input.getBytes();
            byte[] encode = Base64.encode(bytes, 0, bytes.length, Base64.NO_WRAP);
            //显示咱们编码的内容
            show_base64_encode.setText(new String(encode));
        }
    }
    private void initView() {
        input = ((EditText) findViewById(R.id.input));
        show_base64_encode = ((TextView) findViewById(R.id.show_base64_encode));
        show_base64_decode = ((TextView) findViewById(R.id.show_base64_decode));
    }
    /**
     * 进行Base64编码
     * @param view
     */
    public void btnBase64Encode(View view) {
        String input = this.input.getText().toString();
        if (input != null) {
            byte[] bytes = input.getBytes();
            byte[] encode = Base64.encode(bytes, 0, bytes.length, Base64.NO_WRAP);
            //Base64,在进行编码时采用什么样的格式进行编码,建议大家解码时就采用什么样的形式进行解码
            //保证数据万无一失,没有错误,肯定是这种逻辑
            String encodeToString = Base64.encodeToString(bytes, 0, bytes.length, Base64.NO_WRAP);
            //显示咱们编码的内容
//            show_base64_encode.setText(new String(encode));
            show_base64_encode.setText(encodeToString);
        }
    }

    /**
     * 进行Base64解密
     * @param view
     */
    public void btnBase64Decode(View view) {
        String str = show_base64_encode.getText().toString();

        byte[] decode = Base64.decode(str, Base64.DEFAULT);

        byte[] decode1 = Base64.decode(str.getBytes(), 0, str.getBytes().length, Base64.DEFAULT);

        show_base64_decode.setText(new String(decode1));
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值