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

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

怎樣用Java的加密機制來保護你的數據

2019-11-18 13:08:13
字體:
來源:轉載
供稿:網友

  java開發工具包(JDK)對加密和安全性有很好的支持。其中一個優勢就是其內置的對Socket通信的支持。因此,很輕易做到在服務器和客戶之間建立安全的數據流。
  
  Java streams 是一個強大的編程工具。java.io包提供了很多標準的流類型,并能很輕易的建立自己的流類型。流的一個有用的特點是和鏈表一樣的簡單處理過程。表A是一個用鏈表讀取文本的例子:
  
  ufferedReader br =
  new BufferedReader(
  new FileReader(“c:/foo.txt”));
  String line = null;
  while((line =
  br.readLine()) != null)
  {
  System.out.PRintln(line);
  }
  
  這段代碼將 FileReader和 BufferedReader鏈接起來。我們在用客戶機/服務器應用程序的時候也會用到類似的概念。
  
  要害字
  
  對于驗證來說,要害字很重要,表B(KeyGen.java)提供了一個稱為getSecretKey的標準方法。通過運行KeyGen來產生一個要害字。因為我們采用同步方法,所以客戶機和服務器必須用相同的要害字。
  
  isting B?KeyGen.java
  
  /*
  * Created by IntelliJ IDEA.
  * User: jbirchfield
  * Date: Mar 19, 2002
  * Time: 9:33:22 AM
  */
  
  import com.sun.crypto.provider.SunJCE;
  
  import javax.crypto.KeyGenerator;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.security.Key;
  import java.security.NoSUChAlgorithmException;
  import java.security.Security;
  
  public class KeyGen
  {
  
  public static final String
  KEY_FILE = "secret.key";
  public static final String
  ALGORITHM = "DES";
  
  public static void main(String[] args)
  {
  Security.addProvider(new SunJCE());
  new KeyGen();
  }
  
  public KeyGen()
  {
  KeyGenerator kg = null;
  try {
  kg = KeyGenerator.
  getInstance(ALGORITHM);
  Key key = kg.generateKey();
  writeKey(KEY_FILE, key);
  }
  catch (NoSuchAlgorithmException e)
  {
  e.printStackTrace();
  }
  }
  
  private void writeKey(String
  filename, Object o)
  {
  try {
  FileOutputStream fos =
  new FileOutputStream(filename);
  ObjectOutputStream oos =
  new ObjectOutputStream(fos);
  oos.writeObject(o);
  oos.flush();
  fos.close();
  }
  catch (IOException e) {
  e.printStackTrace();
  }
  }
  
  public static Key getSecretKey()
  {
  Security.addProvider(new SunJCE());
  FileInputStream fis = null;
  try
  {
  fis = new FileInputStream(KEY_FILE);
  }
  catch (FileNotFoundException e)
  {
  e.printStackTrace();
  }
  Key key = null;
  
  try {
  ObjectInputStream ois = null;
  ois = new ObjectInputStream(fis);
  key = null;
  key = (Key) ois.readObject();
  }
  catch (IOException e)
  {
  e.printStackTrace();
  }
  catch (ClassNotFoundException e)
  {
  e.printStackTrace();
  }
  System.out.println("key = " + key);
  return key;
  }
  }
  
  安全socket
  
  我們從一個簡單的類開始,它提供我們在普通socket對象之上的加密。表C(SecretSocket.java)包含了兩段代碼-Socket和Key對象。我們的構造器創建了變量并初始化了密碼:
  
  outCipher = Cipher.getInstance(algorithm);
  outCipher.init(Cipher.ENCRYPT_MODE, key);
  inCipher = Cipher.getInstance(algorithm);
  inCipher.init(Cipher.DECRYPT_MODE, key);
  
  isting C?SecretSocket.java
  
  /*
  * Created by IntelliJ IDEA.
  * User: jbirchfield
  * Date: Mar 20, 2002
  * Time: 9:07:51 AM
  */
  
  import org.bouncycastle.
  jce.provider.BouncyCastleProvider;
  
  import javax.crypto.Cipher;
  import javax.crypto.CipherInputStream;
  import javax.crypto.CipherOutputStream;
  import javax.crypto.KeyGenerator;
  import javax.crypto.NoSuchPaddingException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.security.InvalidKeyException;
  import java.security.Key;
  import java.security.NoSuchAlgorithmException;
  import java.security.NoSuchProviderException;
  import java.security.Security;
  
  public class SecretSocket
  {
  
  private Key key = null;
  private Cipher outCipher = null;
  private Cipher inCipher = null;
  private CipherInputStream cis = null;
  private CipherOutputStream cos = null;
  
  private Socket socket = null;
  
  private String algorithm = "DES";
  
  public SecretSocket
  (Socket socket, Key key)
  {
  this.socket = socket;
  this.key = key;
  algorithm = key.getAlgorithm();
  initializeCipher();
  
  }
  
  private void initializeCipher()
  {
  try
  {
  outCipher = Cipher.getInstance
  (algorithm);
  outCipher.init(Cipher.ENCRYPT_MODE, key);
  inCipher = Cipher.getInstance
  (algorithm);
  inCipher.init(Cipher.DECRYPT_MODE, key);
  }
  catch (NoSuchAlgorithmException e)
  {
  e.printStackTrace();
  }
  catch (NoSuchPaddingException e)
  {
  e.printStackTrace();
  }
  catch (InvalidKeyException e)
  {
  e.printStackTrace();
  }
  
  }
  
  public InputStream getInputStream()
  throws IOException {
  InputStream is =
  socket.getInputStream();
  cis = new CipherInputStream
  (is, inCipher);
  return cis;
  }
  
  public OutputStream getOutputStream()
  throws IOException {
  OutputStream os
  = socket.getOutputStream();
  cos = new CipherOutputStream
  (os, outCipher);
  return cos;
  }
  }
  
  因為socket是雙向的通信,所以我們采用兩個密碼。加密輸出的數據并解密輸入的數據。我們使用getInputStream()和getOutputStream(),這兩種方法來加密合解密通用的輸入和輸出的經過包裝的數據流。見表D。
  
  isting D
  
  public InputStream getInputStream()
  throws IOException
  {
  InputStream is = socket.getInputStream();
  cis = new CipherInputStream(is, inCipher);
  return cis;
  }
  public OutputStream getOutputStream()
  throws IOException {
  OutputStream os = socket.getOutputStream();
  cos = new CipherOutputStream(os, outCipher);
  return cos;
  }
  
  在JCE的javax.crypto包中包含CipherInputStream和CipherOutputStream這兩種流類型。他們接收輸入輸出的流對象和密碼對象。
  
  Socket 服務器
  
  開始寫我們的socket服務器類吧。表E(SecretSocketServer.java)是一個完整的列表。SecretSocketServer在一個端口打開ServerSocket,當接收到連接時,使用SocketHandler產生一個線程來操作連接。
  
  isting E?SecretSocketServer.java
  
  /*
  * Created by IntelliJ IDEA.
  * User: jbirchfield
  * Date: Mar 20, 2002
  * Time: 9:32:17 AM
  */
  
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.io.IOException;
  
  public class SecretSocketServer
  {
  
  public static void
  main(String[] args)
  {
  new SecretSocketServer();
  }
  
  public SecretSocketServer()
  {
  ServerSocket ss = null;
  try {
  ss = new ServerSocket(4444);
  }
  catch (IOException e)
  {
  e.printStackTrace();
  }
  while(true) {
  try {
  System.out.println
  ("Waiting...");
  Socket s = ss.accept

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 革吉县| 开阳县| 全椒县| 时尚| 南和县| 张掖市| 玛沁县| 兖州市| 包头市| 高密市| 鲜城| 潮安县| 牙克石市| 什邡市| 开鲁县| 长丰县| 古丈县| 稷山县| 靖边县| 凉山| 刚察县| 苗栗县| 长顺县| 临夏市| 华阴市| 阿瓦提县| 浦城县| 紫金县| 若羌县| 崇阳县| 榆中县| 皋兰县| 中牟县| 江孜县| 遂宁市| 阿克陶县| 娄烦县| 镇赉县| 大冶市| 长寿区| 德昌县|