常用的加密算法--对称加密

时间:2021-10-13 07:43:01

常用的加密算法--对称加密

对称加密是最快速、最简单的一种加密方式,加密与解密用的是相同的密钥。对称加密有很多种算法,由于它效率很高,所以被广泛使用在很多加密协议的核心当中。

对称加密通常使用的是相对较小的密钥,一般小于256 bit。因为密钥越大,加密越强,但加密与解密的过程越慢。

常见的对称加密算法:DES算法、3DES算法 、AES算法

特点:算法公开,计算量小,加密速度快,加密效率高。其安全性主要依赖于秘钥的安全性。加密的时候使用的密钥只有一个。


DES算法

对称加密算法,明文按照64位进行分组,密钥长64位,但是实际上只用56位参与DES运算,第8、16、24、32、40、48、56、64位是校验位,使得每个密钥都有一个奇数位。分组后的明文和56位的密钥按位替代或交换的方法形成密文。

   /**     * DES 生成密钥
     * @return
     * @throws Exception
     */
    public static String genKeyDES() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance("DES");
        keyGen.init(56); //产生秘钥为56位
        SecretKey key = keyGen.generateKey();
        String base64Str = byte2base64(key.getEncoded());
        return base64Str;
    }
    /**
     *  DES 将字符串秘钥转换成SecretKey对象
     * @param base64Key
     * @return
     * @throws Exception
     */
    public static SecretKey loadKeyDES(String base64Key) throws Exception {
        byte[] bytes = base642byte(base64Key);
        SecretKey key = new SecretKeySpec(bytes, "DES");
        return key;
    }
    /**
     *DES 使用生成秘钥对其进行对称加密
     * @param source 加密的字节数组
     * @param key  秘钥
     * @return
     * @throws Exception
     */
    public static byte[] encryptDES(byte[] source,SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] bytes = cipher.doFinal(source);
        return bytes;
    }
    /**
     * DES使用生成秘钥对其进行解密
     * @param source
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptDES(byte[] source,SecretKey key) throws Exception{
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] bytes = cipher.doFinal(source);
        return bytes;
    }
    /**
     * base64 ��a
     * @param base64
     * @return
     * @throws IOException
     */
    private static byte[] base642byte(String base64) throws IOException {
         BASE64Decoder bs = new BASE64Decoder();
         return bs.decodeBuffer(base64); 
    }

    /**
     * base64 解码
     * @param bytes
     * @return
     */
    private static String byte2base64(byte[] bytes) {
        BASE64Encoder bse = new BASE64Encoder();
        return bse.encode(bytes);
    }

AES算法

全世界所使用,对称加密算法中最流行的算法之一。设计有三个密钥长度(128,192,256位),比DES加密算法更加的安全

/**     * AES 获取公钥     * @return     * @throws Exception     */    public static String genKeyAES() throws Exception {        KeyGenerator keyGenerator= KeyGenerator.getInstance("AES");        keyGenerator.init(128);        SecretKey key = keyGenerator.generateKey();        String base64Str = byte2base64(key.getEncoded());        return base64Str;    }    /**     * AES 生成SecretKey对象的秘钥     * @param base64Key     * @return     * @throws Exception     */    public static SecretKey loadKeyAES(String base64Key) throws Exception {        byte[] bytes = base642byte(base64Key);        SecretKey key = new SecretKeySpec(bytes, "AES");        return key;    }    /**     * AES  数据加密     * @param source     * @param key     * @return     * @throws Exception     */    public static byte[] encryptAES(byte[] source,SecretKey key) throws Exception{        Cipher cipher = Cipher.getInstance("AES");        cipher.init(Cipher.ENCRYPT_MODE, key);        byte[] bytes = cipher.doFinal(source);        return bytes;    }    /**     * AES 数据解密     * @param source     * @param key     * @return     * @throws Exception     */    public static byte[] decryptAES(byte[] source,SecretKey key) throws Exception{        Cipher cipher = Cipher.getInstance("AES");        cipher.init(Cipher.DECRYPT_MODE, key);        byte[] bytes = cipher.doFinal(source);        return bytes;    }


本文出自 “In the eyes of the sun” 博客,请务必保留此出处http://wang963825.blog.51cto.com/8695943/1862645