星空网 > 软件开发 > Java

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

 




原标题:java 实现加密

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Prime Day易踩的坑 ,稍不注意,亏损百万!:https://www.kjdsnews.com/a/1516300.html
盘点企业知识库那些你不知道的作用:https://www.kjdsnews.com/a/1516301.html
如何玩TikTok?全世界最顶级网红MrBeast的做法你学会了吗:https://www.kjdsnews.com/a/1516302.html
100%7天达,超时1秒即赔,递四方联邮通晚必赔全新上线:https://www.kjdsnews.com/a/1516303.html
多家物流企业上榜中国市值500强;广东邮政第二架大型宽体货机即将首航:https://www.kjdsnews.com/a/1516304.html
中国游戏公司出海攻略美国:https://www.kjdsnews.com/a/1516305.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流