数据库加密

来源:互联网 发布:ubuntu libxml 编辑:程序博客网 时间:2024/06/07 05:33

内容转自http://tiantiankaixin.iteye.com/blog/743228

 

package com.lxitedu.tools.generate; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import com.dcivision.framework.Crypt; import com.dcivision.framework.SystemParameterConstant; import com.dcivision.framework.SystemParameterFactory; /** * suit the php * * @author Administrator * */ public class MD5 {   // 解密类: Crypt   private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",       "e", "f"                           };   public static void main(String[] args) throws Exception {     getMD5EncryptedString("");     String encrptedPwd = Crypt.encrypt("diaoer",         SystemParameterFactory.getSystemParameter(SystemParameterConstant.CRYPTO_SALT));     System.out.println(getMD5EncryptedString("diaoer"));     System.out.println(encrptedPwd);   }   public static String getMD5EncryptedString(String sourceString) throws NoSuchAlgorithmException {     // 加密后的字符串     // 创建具有指定算法名称的信息摘要     MessageDigest md = MessageDigest.getInstance("MD5");     // 使用指定的字节数组对摘要进行最后更新,然后完成摘要计算     byte[] results = md.digest(sourceString.getBytes());     return byteArrayToHexString(results);   }   private static String byteArrayToHexString(byte[] b) {     StringBuffer resultSb = new StringBuffer();     for (int i = 0; i < b.length; i++) {       resultSb.append(byteToHexString(b[i]));     }     return resultSb.toString();   }   /**    * 将一个字节转化成十六进制形式的字符串    */   private static String byteToHexString(byte b) {     int n = b;     if (n < 0)       n = 256 + n;     int d1 = n / 16;     int d2 = n % 16;     return hexDigits[d1] + hexDigits[d2];   } } 解密: /* * @(#)Crypt.java * * Copyright (c) 2003 DCIVision Ltd * All rights reserved. * * This software is the confidential and proprietary information of DCIVision * Ltd ("Confidential Information").  You shall not disclose such Confidential * Information and shall use it only in accordance with the terms of the license * agreement you entered into with DCIVision Ltd. */ package com.dcivision.framework; /** * Crypt.java * * This class is to provide encrypt and decrypt function by a password provided. * * @author Rollo Chan * @company DCIVision Limited * @creation date 25/06/2003 * @version $Revision: 1.6 $ */ public class Crypt {   public static final String REVISION = "$Revision: 1.6 $";   private Crypt() {   }   /**    * encrypt    *    * Encrypt a string with a password    *    * @param buffer    *          The buffer to be encrypted    * @param password    *          The password    */   public static String encrypt(String buffer, String password) {     StringBuffer sbSpace = new StringBuffer("     ");     buffer = sbSpace.toString() + buffer + sbSpace.toString();     char[] temp = new char[buffer.length()];     int a = 0;     for (int i = 0; i < buffer.length(); i++) {       int b = password.charAt(a);       a++;       if (a >= password.length()) {         a = 0;       }       int c = (int) buffer.charAt(i);       temp[i] = (char) (c ^ b);     }     return toHex(temp);   }   /**    * decrypt    *    * Decrypt a string with a password    *    * @param buffer    *          The buffer to be decrypted    * @param password    *          The password    */   public static String decrypt(String buffer, String password) {     char[] temp = toChar(buffer);     String result = "";     int a = 0;     for (int i = 0; i < temp.length; i++) {       int b = password.charAt(a);       a++;       if (a >= password.length()) {         a = 0;       }       int c = temp[i];       result += (char) (c ^ b);// ^ 按位异或XOR 就是二个操作数只有一个是1的话结果就是 1 否则就是 0 。     }     return result.trim();   }   private static int hexToInt(char c) {     if (c >= '0' && c <= '9') {       return c - '0';     }     return (c - 'a') + 10;   }   private static char hexToChar(String buffer) {     int l = hexToInt(buffer.charAt(1));     int h = hexToInt(buffer.charAt(0));     return (char) (h * 16 + l);   }   private static String toHex(char[] buffer) {     String temp = "";     String result = "";     for (int i = 0; i < buffer.length; i++) {       int c = buffer[i];       temp = Integer.toHexString((int) c);       if (temp.length() == 1) {         temp = "0" + temp;       }       result += temp;     }     return result;   }   private static char[] toChar(String buffer) {     char[] result = new char[buffer.length() / 2];     for (int i = 0; i < buffer.length(); i += 2) {       if (i + 2 <= buffer.length()) {         String temp = buffer.substring(i, i + 2);         result[i / 2] = (char) hexToChar(temp);       }     }     return result;   }   /**    * main    *    * @param arg    */   public static void main(String arg[]) {     // String buffer = arg[0];     // String password = arg[1];     String buffer = "diaoer";     String password = "test";     String temp = Crypt.encrypt(buffer, password);     System.out.println("buffer    = \"" + buffer + "\"");     System.out.println("password  = \"" + password + "\"");     System.out.println("encrypted = \"" + temp + "\"");     System.out.println("decrypted = \"" + Crypt.decrypt(temp, password) + "\"");   } } 

 

原创粉丝点击