java学习记录(4) -加密算法AES, MD5编码, 图片转Base64

来源:互联网 发布:mac os x系统更新失败 编辑:程序博客网 时间:2024/06/07 19:28

一些基础的编码

  1. 工作中常用的一些编码例如md5加密,对称加密,Base64装换等

    1 Md5编码

public static String md5Encode(String s) throws NoSuchAlgorithmException {        String encodeStr = "";        MessageDigest m;        m = MessageDigest.getInstance("MD5");        m.update(s.getBytes());        BigInteger bigInt = new BigInteger(1, m.digest());        encodeStr = bigInt.toString(16);        return encodeStr;    }

2 使用对称加密算法进行加解密,通常采用Base64进行加密的编码。当然将字节流转成16进制字符串也是可行的。
这里采用AES进行加解密,需要双发约定好一个公钥,通过公钥进行加解密。

//加密部分public static String symmetryEncode(String string) {        try {            byte[] pkey = getAESkey(Encrypt.corsfacePubKey);            SecretKey secretKey = new SecretKeySpec(pkey, "AES");            Cipher cipher = Cipher.getInstance("AES");            cipher.init(Cipher.ENCRYPT_MODE, secretKey);            byte[] cipherByte = cipher.doFinal(string.getBytes());//加密data            //16进制字符串转成字符            return Base64.getEncoder().encodeToString(cipherByte);        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {            e.printStackTrace();        }        return null;    }//解密部分public static String symmetryDecode(String string) {        byte[] byt = null;        try {            byt = Base64.getDecoder().decode(string);        } catch (Exception e) {            e.printStackTrace();        }        try {            byte[] pkey = getAESkey(Encrypt.corsfacePubKey);            SecretKey secretKey = new SecretKeySpec(pkey, "AES");//恢复密钥            Cipher cipher = Cipher.getInstance("AES");//Cipher完成加密或解密工作类            cipher.init(Cipher.DECRYPT_MODE, secretKey);//对Cipher初始化,解密模式            return new String(cipher.doFinal(byt));        } catch (InvalidKeyException e) {            e.printStackTrace();        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        } catch (NoSuchPaddingException e) {            e.printStackTrace();        } catch (IllegalBlockSizeException e) {            e.printStackTrace();        } catch (BadPaddingException e) {            e.printStackTrace();        }        return null;    }//生成公钥算子private static byte[] getAESkey(String pkey) {        try {            KeyGenerator keyGen = KeyGenerator.getInstance("AES");            keyGen.init(128, new SecureRandom(pkey.getBytes()));             SecretKey secretKey = keyGen.generateKey();            byte[] key = secretKey.getEncoded();             return key;        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        }        return null;    }

3 图片转base64

public static String imgToBase64(String imgPath) {        String lastStr = "";        File imgFile = new File(imgPath);        try {            FileInputStream fileInputStreamReader = new FileInputStream(imgFile);            byte[] bytes = new byte[(int)imgFile.length()];            fileInputStreamReader.read(bytes);            String encodedfile = Base64.getEncoder().encodeToString(bytes);            return encodedfile;        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return lastStr;    }

最后,通过这些编码可以明确编码和解码的过程主要用的数据结构是byte[] 数组,也就是字节流。字节流可以是计算机上的各个字节。可以将字节数组转成16进制字符串, byte[] <—> 16进制字符串, String <–> byte[], String <–> 16进制字符串, Base64 <–> String, Base64 <–> byte[]。
这些转化是很有意思的。

阅读全文
0 0
原创粉丝点击