
正文
java数据库加密代码 java数据库加密代码有哪些
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
javajdbc连接mysql数据库如何实现用户名密码以及传输数据的加密?
如果jdbc和mysql都支持SSL那通过配置就可以了\x0d\x0a如果不支持,那也可以自己来实现。\x0d\x0a实现思路:\x0d\x0a1、在数据库的主机上运行一个java服务,用来转发数据这个服务我们成为A服务\x0d\x0a2、客户端并不直接访问数据库,而访问A服务,客户端和A服务之间的传输代码由用户自己完成,当然可以加密。走套接字,走http,或者其他什么都是可以的。
相关问答
Q1: java加密解密代码
package com.cube.limail.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;/**
* 加密解密类
*/
public class Eryptogram
{
private static String Algorithm ="DES";
private String key="CB7A92E3D3491964";
//定义 加密算法,可用 DES,DESede,Blowfish
static boolean debug = false ;
/**
* 构造子注解.
*/
public Eryptogram ()
{
} /**
* 生成密钥
* @return byte[] 返回生成java数据库加密代码的密钥
* @throws exception 扔出异常.
*/
public static byte [] getSecretKey () throws Exception
{
KeyGenerator keygen = KeyGenerator.getInstance (Algorithm );
SecretKey deskey = keygen.generateKey ();
System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
if (debug ) System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
return deskey.getEncoded ();
} /**
* 将指定的数据根据提供的密钥进行加密
* @param input 需要加密的数据
* @param key 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
public static byte [] encryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug )
{
System.out.println ("加密前的二进串:"+byte2hex (input ));
System.out.println ("加密前的字符串:"+new String (input ));
} Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.ENCRYPT_MODE ,deskey );
byte [] cipherByte =c1.doFinal (input );
if (debug ) System.out.println ("加密后的二进串:"+byte2hex (cipherByte ));
return cipherByte ;
} /**
* 将给定的已加密的数据通过指定的密钥进行解密
* @param input 待解密的数据
* @param key 密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
public static byte [] decryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));
Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.DECRYPT_MODE ,deskey );
byte [] clearByte =c1.doFinal (input );
if (debug )
{
System.out.println ("解密后的二进串:"+byte2hex (clearByte ));
System.out.println ("解密后的字符串:"+(new String (clearByte )));
} return clearByte ;
} /**
* 字节码转换成16进制字符串
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的16进制字符串
*/
public static String byte2hex (byte [] b )
{
String hs ="";
String stmp ="";
for (int n =0 ;n b.length ;n ++)
{
stmp =(java.lang.Integer.toHexString (b [n ] 0XFF ));
if (stmp.length ()==1 ) hs =hs +"0"+stmp ;
else hs =hs +stmp ;
if (n b.length -1 ) hs =hs +":";
} return hs.toUpperCase ();
}
/**
* 字符串转成字节数组.
* @param hex 要转化的字符串.
* @return byte[] 返回转化后的字符串.
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
/**
* 字节数组转成字符串.
* @param String 要转化的字符串.
* @return 返回转化后的字节数组.
*/
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i bArray.length; i++) {
sTemp = Integer.toHexString(0xFF bArray[i]);
if (sTemp.length() 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/**
* 从数据库中获取密钥.
* @param deptid 企业id.
* @return 要返回的字节数组.
* @throws Exception 可能抛出的异常.
*/
public static byte[] getSecretKey(long deptid) throws Exception {
byte[] key=null;
String value=null;
//CommDao dao=new CommDao();
// List list=dao.getRecordList("from Key k where k.deptid="+deptid);
//if(list.size()0){
//value=((com.csc.sale.bean.Key)list.get(0)).getKey();
value = "CB7A92E3D3491964";
key=hexStringToByte(value);
//}
if (debug)
System.out.println("密钥:" + value);
return key;
}
public String encryptData2(String data) {
String en = null;
try {
byte[] key=hexStringToByte(this.key);
en = bytesToHexString(encryptData(data.getBytes(),key));
} catch (Exception e) {
e.printStackTrace();
}
return en;
}
public String decryptData2(String data) {
String de = null;
try {
byte[] key=hexStringToByte(this.key);
de = new String(decryptData(hexStringToByte(data),key));
} catch (Exception e) {
e.printStackTrace();
}
return de;
}
} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //获得钥匙(字节数组)
byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //传入密码和钥匙java数据库加密代码,获得加密后的字节数组的密码
password=Eryptogram.bytesToHexString(tmp); //将字节数组转化为字符串,获得加密后的字符串密码解密与之差不多
Q2: 怎么用java代码把imei和uin取得微信数据库加密方法密码
这还真是第一次写java程序。其实,一直是对java有偏见的。无奈,从BlackFeather那里知道了微信数据库加密方法,以及密码的获取方式。
发现是基于hashmap的,本来吧,还是想用C或者其他语言来实现对hashmap的读取,可后来都失败了。要么就是太复杂了。于是,还是硬着头皮去把jdk装好,然后~~~开始各种百度。
先用反编译出来的代码搭了个大体的轮廓。然后,自己各种加代码(考虑到一些问题,还是只贴出一些关键代码吧):
加密方式是:hash(imeiuin).substring(0,7)
public static void main(String[] args)
{
try
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[0]));
Object DL = in.readObject();
HashMap hashWithOutFormat = (HashMap)DL;
ObjectInputStream in1 = new ObjectInputStream(new FileInputStream(args[1]));
Object DJ = in1.readObject();
HashMap hashWithOutFormat1 = (HashMap)DJ;
String s = String.valueOf(hashWithOutFormat1.get(Integer.valueOf(258))); //取IMEI
s=s+hashWithOutFormat.get(Integer.valueOf(1));//
s=encode(s);//hash
System.out.println("The Key is : "+s.substring(0,7));
in.close();
in1.close();
}
}
Q3: java 加密的示例
import java.security.*;
import javax.crypto.*;
import java.io.*;
public class tCipher
{
public static void main(String[] args)
{
test_Cipher();
}
public static void test_Cipher()
{
try
{
//待加密的数据
String strToEnc = "Hello Java!";
byte[] plainText = strToEnc.getBytes();
System.out.println( "\n开始生成DES密钥" );
KeyGenerator keyGen = KeyGenerator.getInstance("DES"); //初始化为DES算法
keyGen.init(56); //设置其密钥长度,56bits
Key key = keyGen.generateKey(); //生成密钥
System.out.println( "生成DES密钥成功。" );
//打印出DES密钥
byte[] keyencode=key.getEncoded();
PrintHex(keyencode,keyencode.length);
//生成Cipher对象,设置算法为ECB模式的DES算法,补位填充模式为PKCS5
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
//打印Cipher对象密码服务提供者信息
System.out.println( "\n" + cipher.getProvider().getInfo() );
// 加密
System.out.println( "\n开始加密" );
cipher.init(Cipher.ENCRYPT_MODE, key);//cipher对象初始化,设置为加密
byte[] cipherText = cipher.doFinal(plainText);//结束数据加密,输出密文
System.out.println( "加密完成,密文为: " );
PrintHex(cipherText,cipherText.length);//打印密文
// 使用相同的key解密数据
System.out.println( "\n开始解密" );
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] newPlainText = cipher.doFinal(cipherText);
System.out.println( "解密完成 ,明文为:" );
//输出原文
System.out.println( new String(newPlainText, "UTF8") );
}
catch (Exception e)
{
System.out.println("加解密出错。");
}
}
public static void PrintHex(byte data[],int len)
{
int i;
int tmp;
String Tmp="";
for(i=0; ilen; i++)
{
if(i%16 == 0)
{
System.out.println("");
//0x0000
if(i0x10)
Tmp = "0x000";
if((i0x100) (i=0x10))
Tmp = "0x00";
if((i=0x100)(i0x1000))
Tmp = "0x0";
if(i=0x1000)
Tmp = "0x";
System.out.print(Tmp+Integer.toHexString(i)+"h: ");
}
tmp = data[i];
if(tmp 0)
tmp = 256 + tmp;
if(tmp 0x10)
System.out.print("0"+Integer.toHexString(tmp) +" ");
else
System.out.print(Integer.toHexString(tmp) +" ");
}
System.out.println("");
}
}
Q4: 写一个java加密程序
/**
* SHA-1加密函数
*
*
*/
public class SsytemSha1 {
private final int[] abcde = {
0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
};
// 摘要数据存储数组
private int[] digestInt = new int[5];
// 计算过程中的临时数据存储数组
private int[] tmpData = new int[80];
// 计算sha-1摘要
private int process_input_bytes(byte[] bytedata) {
// 初试化常量
System.arraycopy(abcde, 0, digestInt, 0, abcde.length);
// 格式化输入字节数组,补10及长度数据
byte[] newbyte = byteArrayFormatData(bytedata);
// 获取数据摘要计算的数据单元个数
int MCount = newbyte.length / 64;
// 循环对每个数据单元进行摘要计算
for (int pos = 0; pos MCount; pos++) {
// 将每个单元的数据转换成16个整型数据,并保存到tmpData的前16个数组元素中
for (int j = 0; j 16; j++) {
tmpData[j] = byteArrayToInt(newbyte, (pos * 64) + (j * 4));
}
// 摘要计算函数
encrypt();
}
return 20;
}
// 格式化输入字节数组格式
private byte[] byteArrayFormatData(byte[] bytedata) {
// 补0数量
int zeros = 0;
// 补位后总位数
int size = 0;
// 原始数据长度
int n = bytedata.length;
// 模64后的剩余位数
int m = n % 64;
// 计算添加0的个数以及添加10后的总长度
if (m 56) {
zeros = 55 - m;
size = n - m + 64;
} else if (m == 56) {
zeros = 63;
size = n + 8 + 64;
} else {
zeros = 63 - m + 56;
size = (n + 64) - m + 64;
}
// 补位后生成的新数组内容
byte[] newbyte = new byte[size];
// 复制数组的前面部分
System.arraycopy(bytedata, 0, newbyte, 0, n);
// 获得数组Append数据元素的位置
int l = n;
// 补1操作
newbyte[l++] = (byte) 0x80;
// 补0操作
for (int i = 0; i zeros; i++) {
newbyte[l++] = (byte) 0x00;
}
// 计算数据长度,补数据长度位共8字节,长整型
long N = (long) n * 8;
byte h8 = (byte) (N 0xFF);
byte h7 = (byte) ((N 8) 0xFF);
byte h6 = (byte) ((N 16) 0xFF);
byte h5 = (byte) ((N 24) 0xFF);
byte h4 = (byte) ((N 32) 0xFF);
byte h3 = (byte) ((N 40) 0xFF);
byte h2 = (byte) ((N 48) 0xFF);
byte h1 = (byte) (N 56);
newbyte[l++] = h1;
newbyte[l++] = h2;
newbyte[l++] = h3;
newbyte[l++] = h4;
newbyte[l++] = h5;
newbyte[l++] = h6;
newbyte[l++] = h7;
newbyte[l++] = h8;
return newbyte;
}
private int f1(int x, int y, int z) {
return (x y) | (~x z);
}
private int f2(int x, int y, int z) {
return x ^ y ^ z;
}
private int f3(int x, int y, int z) {
return (x y) | (x z) | (y z);
}
private int f4(int x, int y) {
return (x y) | x (32 - y);
}
// 单元摘要计算函数
private void encrypt() {
for (int i = 16; i = 79; i++) {
tmpData[i] = f4(tmpData[i - 3] ^ tmpData[i - 8] ^ tmpData[i - 14] ^
tmpData[i - 16], 1);
}
int[] tmpabcde = new int[5];
for (int i1 = 0; i1 tmpabcde.length; i1++) {
tmpabcde[i1] = digestInt[i1];
}
for (int j = 0; j = 19; j++) {
int tmp = f4(tmpabcde[0], 5) +
f1(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +
tmpData[j] + 0x5a827999;
tmpabcde[4] = tmpabcde[3];
tmpabcde[3] = tmpabcde[2];
tmpabcde[2] = f4(tmpabcde[1], 30);
tmpabcde[1] = tmpabcde[0];
tmpabcde[0] = tmp;
}
for (int k = 20; k = 39; k++) {
int tmp = f4(tmpabcde[0], 5) +
f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +
tmpData[k] + 0x6ed9eba1;
tmpabcde[4] = tmpabcde[3];
tmpabcde[3] = tmpabcde[2];
tmpabcde[2] = f4(tmpabcde[1], 30);
tmpabcde[1] = tmpabcde[0];
tmpabcde[0] = tmp;
}
for (int l = 40; l = 59; l++) {
int tmp = f4(tmpabcde[0], 5) +
f3(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +
tmpData[l] + 0x8f1bbcdc;
tmpabcde[4] = tmpabcde[3];
tmpabcde[3] = tmpabcde[2];
tmpabcde[2] = f4(tmpabcde[1], 30);
tmpabcde[1] = tmpabcde[0];
tmpabcde[0] = tmp;
}
for (int m = 60; m = 79; m++) {
int tmp = f4(tmpabcde[0], 5) +
f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] +
tmpData[m] + 0xca62c1d6;
tmpabcde[4] = tmpabcde[3];
tmpabcde[3] = tmpabcde[2];
tmpabcde[2] = f4(tmpabcde[1], 30);
tmpabcde[1] = tmpabcde[0];
tmpabcde[0] = tmp;
}
for (int i2 = 0; i2 tmpabcde.length; i2++) {
digestInt[i2] = digestInt[i2] + tmpabcde[i2];
}
for (int n = 0; n tmpData.length; n++) {
tmpData[n] = 0;
}
}
// 4字节数组转换为整数
private int byteArrayToInt(byte[] bytedata, int i) {
return ((bytedata[i] 0xff) 24) | ((bytedata[i + 1] 0xff) 16) |
((bytedata[i + 2] 0xff) 8) | (bytedata[i + 3] 0xff);
}
// 整数转换为4字节数组
private void intToByteArray(int intValue, byte[] byteData, int i) {
byteData[i] = (byte) (intValue 24);
byteData[i + 1] = (byte) (intValue 16);
byteData[i + 2] = (byte) (intValue 8);
byteData[i + 3] = (byte) intValue;
}
// 将字节转换为十六进制字符串
private static String byteToHexString(byte ib) {
char[] Digit = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
'D', 'E', 'F'
};
char[] ob = new char[2];
ob[0] = Digit[(ib 4) 0X0F];
ob[1] = Digit[ib 0X0F];
String s = new String(ob);
return s;
}
// 将字节数组转换为十六进制字符串
private static String byteArrayToHexString(byte[] bytearray) {
String strDigest = "";
for (int i = 0; i bytearray.length; i++) {
strDigest += byteToHexString(bytearray[i]);
}
return strDigest;
}
// 计算sha-1摘要,返回相应的字节数组
public byte[] getDigestOfBytes(byte[] byteData) {
process_input_bytes(byteData);
byte[] digest = new byte[20];
for (int i = 0; i digestInt.length; i++) {
intToByteArray(digestInt[i], digest, i * 4);
}
return digest;
}
// 计算sha-1摘要,返回相应的十六进制字符串
public String getDigestOfString(byte[] byteData) {
return byteArrayToHexString(getDigestOfBytes(byteData));
}
public static void main(String[] args) { //测试通过
String data = "123";
String digest = new SsytemSha1().getDigestOfString(data.getBytes());
}
}
java数据库加密代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java数据库加密代码有哪些、java数据库加密代码的信息别忘了在本站进行查找喔。







