⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testsecurity.java

📁 基于Java的地图数据管理软件。使用MySQL数据库管理系统。
💻 JAVA
字号:
package net.aetherial.gis.test;

import java.security.*;
import java.io.*;
import javax.crypto.*;
import net.aetherial.gis.util.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class TestSecurity {
  public TestSecurity() {
  }

  public static String toHexString(byte[] b) {
    StringBuffer hexString = new StringBuffer();
    String plainText;
    for (int i = 0; i < b.length; i++) {
      plainText = Integer.toHexString(0xFF & b[i]);
      if (plainText.length() < 2) {
        plainText = "0" + plainText;
      }
      hexString.append(plainText);
    }
    return hexString.toString();
  }

  public void sha(String text) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(text.getBytes());
    byte[] digest = md.digest();
    System.out.println(this.toHexString(digest));
  }

  public void cipher(boolean ENCRYPT, String key, FileInputStream in,
                     FileOutputStream out) {
    KeyGenerator generator = null;
    Key myKey;
    try {
      generator = KeyGenerator.getInstance("DES");
    }
    catch (NoSuchAlgorithmException ex) {
      System.out.println(ex);
      return;
    }
    generator.init(new SecureRandom(key.getBytes()));
    myKey = generator.generateKey();
    //得到一个Cipher对象
    Cipher cipher = null;

    try {
      cipher = Cipher.getInstance("DES");
    }
    catch (NoSuchPaddingException ex1) {
      System.out.println(ex1);
      return;
    }
    catch (NoSuchAlgorithmException ex1) {
      System.out.println(ex1 + ",in line 61");
      return;
    }
    ////////////加密或者解密
    try {
      if (ENCRYPT) {
        cipher.init(Cipher.ENCRYPT_MODE, myKey);
      }
      else {
        cipher.init(Cipher.DECRYPT_MODE, myKey);
      }
    }
    catch (InvalidKeyException ex2) {
      System.out.println(ex2 + ",in line 74");
    }
    ////////////////////////
    CipherOutputStream cout = new CipherOutputStream(out, cipher);
    byte[] buffer = new byte[8192];
    int length;
    try {
      while ( (length = in.read(buffer)) != -1) {
        cout.write(buffer, 0, length);
      }
      in.close();
      cout.close();
    }
    catch (IOException ex3) {
      System.out.println(ex3);
    }
  }

  public void cipher(boolean ENCRYPT, String key, String in, String out) {
    this.cipher(ENCRYPT, key, (FileInputStream) getInputStream(in),
                (FileOutputStream) getOutputStream(out));
  }

  public String cipher(boolean ENCRYPT, String key, String string) {
    File fin = new File("inTemp");
    File fout = new File("outTemp");
    return "";
  }

  public void test(String in, String key, String out) {
    ByteArrayInputStream bin = null;
    byte[] buf = null;
    buf = in.getBytes();
    bin = new ByteArrayInputStream(buf);

  }

  private InputStream getInputStream(String in) {
    ByteArrayInputStream bin = null;
    byte[] buf = null;
    buf = in.getBytes();
    bin = new ByteArrayInputStream(buf);
    return bin;
  }

  private OutputStream getOutputStream(String out) {
    PipedInputStream pin = (PipedInputStream)this.getInputStream(out);
    PipedOutputStream pout = null;
    try {
      pout = new PipedOutputStream(pin);
    }
    catch (IOException ex) {
    }
    return pout;
  }

  public static void main(String[] args) throws Exception {
    //File f1 = new File("C:\\Documents and Settings\\Administrator\\桌面\\17_1_1.wpt");
    //File f2 = new File("C:\\Documents and Settings\\Administrator\\桌面\\17_1_2.wpt");
    //f1.createNewFile();
    //f2.createNewFile();
    //TestSecurity ts = new TestSecurity();
    //FileInputStream in = new FileInputStream(new File("C:\\Documents and Settings\\Administrator\\桌面\\17.wpt"));
    //FileOutputStream out = new FileOutputStream(f1);
    //FileInputStream in = new FileInputStream(f1);
    //FileOutputStream out = new FileOutputStream(f2);
    //ts.cipher(false,"tt",in,out);

    //ts.cipher(false,"test",in,out);
    String src = "testtttt汉字测试";
    String key = "test";
    byte[] result = Encryption.encryptFromString(src, key);
    //String s = new String(result);
    String out = Encryption.decryptToString(result, key);
    System.out.println("out: " + out);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -