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

📄 encryption.java

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

import java.security.Security;

import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.SecureRandom;

/**
 * An utility to do the encryption and decryption.
 */
public abstract class Encryption {

  private static final String ALGORITHM = "DES";
  private static final int KEY_SIZE = 24;
  public static final String key ="HR_Soft@7F";

  static {
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
  }

  /**
   * Encrypts a byte array. This is a generic method encrypts bytes to bytes.
   */
  public static byte[] encrypt(byte[] message, String rawkey) throws Exception {
    byte[] key = composeKey(rawkey);
    try {
      SecretKey desKey = new SecretKeySpec(key, ALGORITHM);
      Cipher cipher = Cipher.getInstance(ALGORITHM);
      cipher.init(Cipher.ENCRYPT_MODE, desKey);
      return cipher.doFinal(message);
    }
    catch (Exception e) {
      e.printStackTrace();
      return new byte[0];
    }
  }

  /**
   * Decrypts a byte array. This is a generic method decrypts bytes to bytes.
   */
  public static byte[] decrypt(byte[] message, String rawkey) throws Exception {
    byte[] key = composeKey(rawkey);
    try {
      SecretKey desKey = new SecretKeySpec(key, ALGORITHM);
      Cipher cipher = Cipher.getInstance(ALGORITHM);
      cipher.init(Cipher.DECRYPT_MODE, desKey);
      return cipher.doFinal(message);
    }
    catch (Exception e) {

      //e.printStackTrace();
      //return new byte[0];
      throw e;
    }
  }

  /**
   * Encrypts string to bytes. The string can only contain characters with ASCII 1 to 127, inclusive.
   */
  public static byte[] encryptFromString(String message, String rawkey) throws
      Exception {
    return encrypt(message.getBytes(), rawkey);
  }

  /**
   * Decrypts bytes to string.
   */
  public static String decryptToString(byte[] message, String rawkey) throws
      Exception {
    return new String(decrypt(message, rawkey));
  }

  public static byte[] getSecretKey(String key) throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance(ALGORITHM);
    keygen.init(new SecureRandom(key.getBytes()));
    SecretKey deskey = keygen.generateKey();

    return deskey.getEncoded();
  }

  private static byte[] composeKey(String rawKey) throws Exception {

    return getSecretKey(rawKey);
  }

  /**
   * For testing.
   */
  private static void printBytes(byte[] bytes) {
    System.out.print("'");
    for (int i = 0; i < bytes.length; i++) {
      System.out.print(bytes[i]);
    }
    System.out.println("'");
  }

  /**
   * Unit testing.
   */
  public static void main(String[] args) throws Exception {
    final String TEST_KEY = "123";
    String in = "";
    for (int i = 1; i < 128; i++) {
      in += (char) i;
    }

    try {
      printBytes("A".getBytes("UTF8"));
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    String out = decryptToString(encryptFromString(in, TEST_KEY), TEST_KEY);
    System.out.println(in);
    System.out.println(out);
    System.out.println(in.equals(out));
  }
}

⌨️ 快捷键说明

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