Android中的各种加密和MD5摘要

简单总结了下android中用到的加密方式和MD5摘要算法。不完整,还会继续追加的。
/
**
 * Created by zhoukai on 2016/3/25.
 */
public class DigestUtils {

    //base64编码 其中两个参数第一个为要编码的内容,第二个为编码的格式
    public static String base64_Encode(String val){
        byte[] input_byte = val.getBytes();
        byte[] encode_byte = Base64.encode(input_byte, Base64.NO_WRAP);
        String result_base64Encode= new String(encode_byte);
        return  result_base64Encode;
    }
    //base64解码
    public static String base64_Decode(String val){
        byte[] decode_byte = Base64.decode(val.getBytes(), Base64.NO_WRAP);
        String result_base64Decode= new String(decode_byte);
        return result_base64Decode;
    }
    //aes加密 只需要一套密码即可 密码长度64bit也就是必须是8位
    public  byte[]  desEncode(String desEncode_data,String password){
        byte desEncode_result[] = null;
        try {
            //创建加密引擎
            Cipher cipher = Cipher.getInstance("DES");
            //根据参数生成key
            SecretKey key = new SecretKeySpec(password.getBytes(),"DES");
            //对Cipher进行初始化设置为加密模式
            cipher.init(Cipher.ENCRYPT_MODE,key);
            //加密数据
            desEncode_result = cipher.doFinal(desEncode_data.getBytes());

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return  desEncode_result;
    }
    //des解密
    public static byte[] desDecode(String desDecode_data, String password) {
        byte[] desDecode_result = null;
        try {
            Cipher cipher = Cipher.getInstance("DES");
            SecretKeySpec key = new SecretKeySpec(password.getBytes(),"DES");
            //解密时只需将引擎初始化模式设置为DECRYPT_MODE即可
            cipher.init(Cipher.DECRYPT_MODE,key);
            desDecode_result = cipher.doFinal(desDecode_data.getBytes());
        } 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  desDecode_result;
    }
    /*
    aes加密有两种方式,其中一种和des相同 区别在于初始化引擎的时候调用
     Cipher cipher = Cipher.getInstance("AES");
     aes的第二种加密方式如下
     */
    public static byte[] aes2Encode(byte[] data, byte[] password, byte[] ivParams) {
        byte[] ret = null;
        //首先对数据和密码进行判读
        try {
            //CBC指定加密的工作模式,PKCS5Padding就相当于第二个密码,要不要携带参数
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec key = new SecretKeySpec(password,"AES");
            //IvParameterSpec就相当于第二个密码
            IvParameterSpec params = new IvParameterSpec(ivParams);
            //Cipher初始化,参数中就多了一个IvParameterSpec;
            //在对Cipher进行初始化时,必须要给它在传递一个参数IvParameterSpec,就相当于第二个密码
            cipher.init(Cipher.ENCRYPT_MODE,key,params);
            //将数据返回了
            ret = cipher.doFinal(data);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }

        return  ret;
    }

    public static byte[] aes2Decode(byte[] data, byte[] password, byte[] ivParams) {
        byte[] ret = null;
        //首先对数据和密码进行判读
        try {
            //CBC指定加密的工作模式,PKCS5Padding就相当于第二个密码,要不要携带参数
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec key = new SecretKeySpec(password,"AES");
            //IvParameterSpec就相当于第二个密码
            IvParameterSpec params = new IvParameterSpec(ivParams);
            //Cipher初始化,参数中就多了一个IvParameterSpec;
            //在对Cipher进行初始化时,必须要给它在传递一个参数IvParameterSpec,就相当于第二个密码
            cipher.init(Cipher.DECRYPT_MODE,key,params);
            //将数据返回了
            ret = cipher.doFinal(data);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return  ret;
    }
    //MD5摘要
    public static String md5Digest(String val)  {
        //创建指定算法名称的MessageDigest 实例对象。
        MessageDigest md5 = null;
        String md5_result=null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            // 使用指定的字节数组对摘要进行最后更新,然后完成摘要计算。
            byte[] md5_byte= md5.digest( val.getBytes("UTF-8"));
            //将字节数组转化为字符串
            md5_result = new String(md5_byte);
            return md5_result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return md5_result;
    }
 
/*补充:android中的RSA加密,在开发过程中有些地方还是用到的,例如当我们在项目中集成支付宝的时候,支付宝要求的第一步就是商户注册,然后紧接着就是获取私钥和公钥。公钥我们要上传给支付宝,对于我们没什么用,但是私钥。我们就需要用于订单的RSA签名,因为一个完整的订单信息包括的内容为
 String payInfo = "商品信息"+ "&sign=\"" + "rsa签过名的信息" + "\"&"
      +"sign_type=\"RSA\"";   
通过我们上传的公钥支付宝对商品信息就行解密,因此rsa是一种非对称加密算法,下面附上rsa加密和解密的方法
*/
/**
 * RAS密码通过KeyPareGenerator创建出来
 * 将私钥存放到文件中,公钥存放到shareprefrence中
 */
public void generateRSA() {
    try {
        //1、获取创建私钥和公钥的生成这KeyPairGenerator
        KeyPairGenerator keyPairGenerator =KeyPairGenerator.getInstance("RSA");
        //2、初始化,1024bit=128byte,相当于128个英文字母
        keyPairGenerator.initialize(1024);
        //3、获取公钥和私钥
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        //获取公钥
        aPublic = keyPair.getPublic();
        SharedPreferences sp = getSharedPreferences("RSA", MODE_PRIVATE);
        //1、通过公钥的getEncoded()方法获取公钥的内容,内部数据
        byte[] aPublicEncoded = aPublic.getEncoded();
        //2、对获取的字节数据,进行Base64转换
        byte[] encode = Base64.encode(aPublicEncoded, Base64.DEFAULT);
        //3、转后的数据存到SharePreferrences
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("aPublic",new String(encode));
        editor.commit();
        //*************************************************************************************888
        //获取私钥
        aPrivate = keyPair.getPrivate();
        //将私钥存入文件中
        file = new File(this.getCacheDir()+File.separator+"rsa");
        if(!file.exists()){
            //创建这个file
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //1、获取数据
        byte[] encoded = aPrivate.getEncoded();
        //2、对数据进行Base64编码
        byte[] encode1 = Base64.encode(encoded, Base64.DEFAULT);
        //将数据存入到文件
        try {
            FileOutputStream fos = new FileOutputStream(file);
            //将私钥的数据写入到文件中
            fos.write(encode1);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}
//以下就是rsa的加密和解密方式,只需要将私钥和公钥传入即可
public static byte[] rsaEncode(byte[] data, PrivateKey aPrivate) {

    byte[] ret = null;
    if(data!=null&&data.length>0){
        byte[] encoded = aPrivate.getEncoded();
        if(encoded!=null&&encoded.length>0){
            try {
                //1、创建RSA的加密引擎
                Cipher cipher = Cipher.getInstance("RSA");
                //2、对加密引擎进行初始化
                cipher.init(Cipher.ENCRYPT_MODE,aPrivate);
                //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[] rsaDecode(byte[] decode, PublicKey aPublic) {

    byte[] ret = null;
    if (decode!=null&&decode.length>0){
        byte[] aPublicEncoded = aPublic.getEncoded();
        if(aPublicEncoded!=null&&aPublicEncoded.length>0){
            //进行解密
            try {
                //1
                Cipher cipher = Cipher.getInstance("RSA");

                //2
                cipher.init(Cipher.DECRYPT_MODE,aPublic);

                //3
                ret = cipher.doFinal(decode);

            } 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;
}


 
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值