国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java 實現加密

2019-11-15 01:13:51
字體:
來源:轉載
供稿:網友
java 實現加密

加密有單向加密和雙向加密之分,也叫對稱加密和非對稱加密,雙向加密就是把數據加密后,還可以解密成原來的數據,但是需要使用到密鑰,而且必須是同一個密鑰.才可以解密成原來的數據.一開始我也不知道,密鑰是使用系統生成的密鑰,加密和解密分開操作的時候,加密過后,數據就解密不出來了.java實現加密的方法有DES,AES,等這些對稱加密.單向加密從嚴格的意義來講不算加密.只是實現了一定的算法,但是不可以逆轉.md5加密就是一個單向非對稱加密的技術.直接上代碼

package com.chen;import java.security.InvalidKeyException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;public class Chen {    // 密鑰生成器    PRivate KeyGenerator kg = null;    // 密鑰    private SecretKey key = null;    // 進行加密和解密    private Cipher cip = null;    public Chen() throws Exception{        kg = KeyGenerator.getInstance("DES");        key = kg.generateKey();        cip = Cipher.getInstance("DES");    }    public byte[] encrypt(byte[] s) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException{        // 初始化        cip.init(Cipher.ENCRYPT_MODE, key);        // 加密        return cip.doFinal(s);    }    public byte[] decrypt(byte[] b) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException{        // 初始化        cip.init(Cipher.DECRYPT_MODE, key);        // 解密        return cip.doFinal(b);    }    public static void main(String[] args) {        try {            Chen chen = new Chen();            String str = "aba薩芬斯蒂芬";            // 加密            byte[] temp = chen.encrypt(str.getBytes());            // 解密            byte[] flag = chen.decrypt(temp);            System.out.println("明文 : " + str);            System.out.println("加密后的數據 : " + new String(temp));            System.out.println("解密后的數據 : " + new String(flag));        } catch (Exception e) {            e.printStackTrace();        }    }}

AES加密和解密的代碼

package crypt;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;public class AEScrypt {    //  key生成器    private KeyGenerator kg;    // 密鑰    private SecretKey key;    // 加密解密類    private Cipher cipher;    public AEScrypt() throws NoSuchAlgorithmException, NoSuchPaddingException{        kg = KeyGenerator.getInstance("AES");        System.out.println(kg.getProvider().getName());        key = kg.generateKey();        cipher = Cipher.getInstance("AES");        System.out.println(cipher.getProvider());    }    public byte[] encrypt(byte[] b) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException{        // 初始化        cipher.init(Cipher.ENCRYPT_MODE, key);        return cipher.doFinal(b);    }    public byte[] decrypt(byte[] b) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException{        // 初始化        cipher.init(Cipher.DECRYPT_MODE, key);        // 解密        return cipher.doFinal(b);            }    public static void main(String[] args) {        try {            AEScrypt aes = new AEScrypt();            String s = "safasf的送飯飯2342(&((*";            byte[] temp = aes.encrypt(s.getBytes());            byte[] flag = aes.decrypt(temp);            System.out.println("明文 : " + s);            System.out.println("加密 : " + new String(temp));            System.out.println("解密 : " + new String(flag));        } catch (Exception e) {                        e.printStackTrace();        }            }}

非對稱RSA,有連個密鑰,一個公開密鑰,一個私有密鑰,通過其中一個密鑰加密,在通過其他一個密鑰解密

package crypt;import java.security.InvalidKeyException;  import java.security.KeyPair;  import java.security.KeyPairGenerator;  import java.security.NoSuchAlgorithmException;  import java.security.interfaces.RSAPrivateKey;  import java.security.interfaces.RSAPublicKey;    import javax.crypto.BadPaddingException;  import javax.crypto.Cipher;  import javax.crypto.IllegalBlockSizeException;  import javax.crypto.NoSuchPaddingException;    public class RSAcrypt {            /**      * 加密      * @param publicKey      * @param srcBytes      * @return      * @throws NoSuchAlgorithmException      * @throws NoSuchPaddingException      * @throws InvalidKeyException      * @throws IllegalBlockSizeException      * @throws BadPaddingException      */      protected byte[] encrypt(RSAPublicKey publicKey,byte[] srcBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{          if(publicKey!=null){              //Cipher負責完成加密或解密工作,基于RSA              Cipher cipher = Cipher.getInstance("RSA");              //根據公鑰,對Cipher對象進行初始化              cipher.init(Cipher.ENCRYPT_MODE, publicKey);              byte[] resultBytes = cipher.doFinal(srcBytes);              return resultBytes;          }          return null;      }            /**      * 解密       * @param privateKey      * @param srcBytes      * @return      * @throws NoSuchAlgorithmException      * @throws NoSuchPaddingException      * @throws InvalidKeyException      * @throws IllegalBlockSizeException      * @throws BadPaddingException      */      protected byte[] decrypt(RSAPrivateKey privateKey,byte[] srcBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{          if(privateKey!=null){              //Cipher負責完成加密或解密工作,基于RSA              Cipher cipher = Cipher.getInstance("RSA");              //根據公鑰,對Cipher對象進行初始化              cipher.init(Cipher.DECRYPT_MODE, privateKey);              byte[] resultBytes = cipher.doFinal(srcBytes);              return resultBytes;          }          return null;      }        /**      * @param args      * @throws NoSuchAlgorithmException       * @throws BadPaddingException       * @throws IllegalBlockSizeException       * @throws NoSuchPaddingException       * @throws InvalidKeyException       */      public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {          RSAcrypt rsa = new RSAcrypt();          String msg = "郭XX-精品相聲";          //KeyPairGenerator類用于生成公鑰和私鑰對,基于RSA算法生成對象          KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");          //初始化密鑰對生成器,密鑰大小為1024位          keyPairGen.initialize(1024);          //生成一個密鑰對,保存在keyPair中          KeyPair keyPair = keyPairGen.generateKeyPair();          //得到私鑰          RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate();                       //得到公鑰          RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic();                    //用公鑰加密          byte[] srcBytes = msg.getBytes();          byte[] resultBytes = rsa.encrypt(publicKey, srcBytes);                    //用私鑰解密          byte[] decBytes = rsa.decrypt(privateKey, resultBytes);                    System.out.println("明文是:" + msg);          System.out.println("加密后是:" + new String(resultBytes));          System.out.println("解密后是:" + new String(decBytes));      }    }  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 营山县| 贺兰县| 嘉定区| 额济纳旗| 渭源县| 台州市| 张掖市| 明光市| 乐平市| 济宁市| 盐边县| 临夏市| 夏邑县| 天气| 开阳县| 桦南县| 商南县| 鸡泽县| 墨竹工卡县| 黑河市| 奉化市| 宾川县| 永登县| 云阳县| 永和县| 汝城县| 巍山| 文成县| 崇阳县| 宣武区| 永寿县| 平山县| 宁远县| 叙永县| 图木舒克市| 宁蒗| 黄山市| 商河县| 临泽县| 宁陵县| 长顺县|