java代码用户密码加密 java 代码 加密

java加密解密代码package com.cube.limail.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;/**
* 加密解密类
*/
publicclassEryptogram
{
privatestaticStringAlgorithm ="DES";
private String key="CB7A92E3D3491964";
//定义 加密算法,可用 DES,DESede,Blowfish
staticbooleandebug= false ;
/**
* 构造子注解.
*/
publicEryptogram ()
{
} /**
* 生成密钥
* @return byte[] 返回生成java代码用户密码加密的密钥
* @throws exception 扔出异常.
*/
publicstaticbyte [] getSecretKey () throwsException
{
KeyGeneratorkeygen= KeyGenerator.getInstance (Algorithm );
SecretKeydeskey= keygen.generateKey ();
System.out.println ("生成密钥:" bytesToHexString (deskey.getEncoded ()));
if(debug ) System.out.println ("生成密钥:" bytesToHexString (deskey.getEncoded ()));
returndeskey.getEncoded ();
} /**
* 将指定的数据根据提供的密钥进行加密
* @param input 需要加密的数据
* @param key 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
publicstaticbyte [] encryptData (byte [] input ,byte [] key ) throwsException
{
SecretKeydeskey= newjavax.crypto.spec.SecretKeySpec (key ,Algorithm );
if(debug )
{
System.out.println ("加密前的二进串:" byte2hex (input ));
System.out.println ("加密前的字符串:" newString (input ));
} Cipherc1= Cipher.getInstance (Algorithm );
c1.init (Cipher.ENCRYPT_MODE ,deskey );
byte [] cipherByte =c1.doFinal (input );
if(debug ) System.out.println ("加密后的二进串:" byte2hex (cipherByte ));
returncipherByte ;
} /**
* 将给定的已加密的数据通过指定的密钥进行解密
* @param input 待解密的数据
* @param key 密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
publicstaticbyte [] decryptData (byte [] input ,byte [] key ) throwsException
{
SecretKeydeskey= newjavax.crypto.spec.SecretKeySpec (key ,Algorithm );
if(debug ) System.out.println ("解密前的信息:" byte2hex (input ));
Cipherc1= Cipher.getInstance (Algorithm );
c1.init (Cipher.DECRYPT_MODE ,deskey );
byte [] clearByte =c1.doFinal (input );
if(debug )
{
System.out.println ("解密后的二进串:" byte2hex (clearByte ));
System.out.println ("解密后的字符串:" (newString (clearByte )));
} returnclearByte ;
} /**
* 字节码转换成16进制字符串
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的16进制字符串
*/
publicstaticStringbyte2hex (byte [] b )
{
Stringhs ="";
Stringstmp ="";
for(intn =0 ;n b.length ;n)
{
stmp =(java.lang.Integer.toHexString (b [n ]0XFF ));
if(stmp.length ()==1 ) hs =hs"0" stmp ;
elsehs =hsstmp ;
if(n b.length -1 ) hs =hs":";
} returnhs.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; ilen; i) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos])4 | toByte(achar[pos1]));
}
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; ibArray.length; i) {
sTemp = Integer.toHexString(0xFFbArray[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=https://www.04ip.com/post/null;
//CommDao dao=new CommDao();
// List list=dao.getRecordList("from Key k where k.deptid=" deptid);
//if(list.size()0){
//value=https://www.04ip.com/post/((com.csc.sale.bean.Key)list.get(0)).getKey();
value = "https://www.04ip.com/post/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;
}
} 加密使用java代码用户密码加密:byte[] key=Eryptogram.getSecretKey(deptid);//获得钥匙(字节数组)
byte[] tmp=Eryptogram.encryptData(password.getBytes(), key);//传入密码和钥匙java代码用户密码加密,获得加密后的字节数组的密码
password=Eryptogram.bytesToHexString(tmp);//将字节数组转化为字符串java代码用户密码加密,获得加密后的字符串密码解密与之差不多
javajdbc连接mysql数据库如何实现用户名密码以及传输数据的加密?如果jdbc和mysql都支持SSL那通过配置就可以了\x0d\x0a如果不支持,那也可以自己来实现 。\x0d\x0a实现思路:\x0d\x0a1、在数据库的主机上运行一个java服务,用来转发数据这个服务我们成为A服务\x0d\x0a2、客户端并不直接访问数据库,而访问A服务,客户端和A服务之间的传输代码由用户自己完成,当然可以加密 。走套接字,走http , 或者其他什么都是可以的 。
java web开发用户注册时密码加密一般用什么技术?MD5加密,这是一种不可逆的加密算法,即一旦进行MD5加密算法,不能再得到原始的密码\x0d\x0a \x0d\x0a开发者可以将用户输入的密码进行MD5加密后,再与数据库中存储的加密后的密码比较 , 即可知道密码的准确性 。\x0d\x0a \x0d\x0a若想找回密码,一种即可以重置密码,即有一个默认的密码 。重置后 , 可以自己再修改密码;另一种即可以通过其他方面的验证后,来录入一个新密码 。现在很多都是使用邮箱验证或是手机随机验证,验证成功后 , 可以设置新密码
java密码加密与解密 以下两个类可以很方便的完成字符串的加密和解密
加密 CryptHelper encrypt(password)
解密 CrypHelper decrypt(password)
代码如下
CryptUtils java
[java]
package gdie lab crypt;
import java io IOException;
import javax crypto Cipher;
import javax crypto KeyGenerator;
import javax crypto SecretKey;
import apache xerces internal impl dv util Base ;
public class CryptUtils {
private static String Algorithm = DES ;
private static byte[] DEFAULT_KEY=new byte[] { };
private static String VALUE_ENCODING= UTF ;
/**
* 生成密钥
*
* @return byte[] 返回生成的密钥
* @throws exception
*扔出异常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator getInstance(Algorithm)
SecretKey deskey = keygen generateKey()
// if (debug ) System out println ( 生成密钥byte hex (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 ( 加密前的二进串byte hex (input ))
// System out println ( 加密前的字符串new String (input ))
//
// }
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher ENCRYPT_MODE deskey)
byte[] cipherByte = c doFinal(input)
// if (debug ) System out println ( 加密后的二进串byte hex (cipherByte ))
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input DEFAULT_KEY)
}
/**
* 将给定的已加密的数据通过指定的密钥进行解密
*
* @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 ( 解密前的信息byte hex (input ))
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher DECRYPT_MODE deskey)
byte[] clearByte = c doFinal(input)
// if (debug )
// {
// System out println ( 解密后的二进串byte hex (clearByte ))
// System out println ( 解密后的字符串(new String (clearByte )))
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input DEFAULT_KEY)
}
/**
* 字节码转换成 进制字符串
*
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的 进制字符串
*/
public static String byte hex(byte[] bytes) {
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs append(String format( % $ X b))
return hs toString()
}
public static byte[] hex byte(String content) {
int l=content length()》 ;
byte[] result=new byte[l];
for(int i= ;il;i) {
int j=i《 ;
String s=content substring(j j)
result[i]=Integer valueOf(s ) byteValue()
}
return result;
}
/**
* 将字节数组转换为base 编码字符串
* @param buffer
* @return
*/
public static String bytesToBase (byte[] buffer) {
//BASE Encoder en=new BASE Encoder()
return Base encode(buffer)
//return encoder encode(buffer)
}
/**
* 将base 编码的字符串解码为字节数组
* @param value
* @return
* @throws IOException
*/
public static byte[] base ToBytes(String value) throws IOException {
//return Base decodeToByteArray(value)
//System out println(decoder decodeBuffer(value))
//return decoder decodeBuffer(value)
return Base decode(value)
}
/**
* 加密给定的字符串
* @param value
* @return 加密后的base 字符串
*/
public static String encryptString(String value) {
return encryptString(value DEFAULT_KEY)
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 以BASE 形式存在的密钥
* @return 加密后的base 字符串
* @throws IOException
*/
public static String encryptString(String value String key) throws IOException {
return encryptString(value base ToBytes(key))
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 字节数组形式的密钥
* @return 加密后的base 字符串
*/
public static String encryptString(String value byte[] key) {
try {
byte[] data=https://www.04ip.com/post/value getBytes(VALUE_ENCODING)
data=https://www.04ip.com/post/CryptUtils encryptData(data key)
return bytesToBase (data)
} catch (Exception e) {
// TODO Auto generated catch block
e printStackTrace()
return null;
}
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value DEFAULT_KEY)
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key base 形式存在的密钥
* @return 明文
* @throws IOException
*/
public static String decryptString(String value String key) throws IOException {
String s=decryptString(value base ToBytes(key))
return s;
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key 字节数据形式存在的密钥
* @return 明文
*/
public static String decryptString(String value byte[] key) {
try {
byte[] data=https://www.04ip.com/post/base ToBytes(value)
data=https://www.04ip.com/post/CryptUtils decryptData(data key)
return new String(data VALUE_ENCODING)
}catch(Exception e) {
e printStackTrace()
return null;
}
}
}
package gdie lab crypt;
import java io IOException;
import javax crypto Cipher;
import javax crypto KeyGenerator;
import javax crypto SecretKey;
import apache xerces internal impl dv util Base ;
public class CryptUtils {
private static String Algorithm = DES ;
private static byte[] DEFAULT_KEY=new byte[] { };
private static String VALUE_ENCODING= UTF ;
/**
* 生成密钥
*
* @return byte[] 返回生成的密钥
* @throws exception
*扔出异常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator getInstance(Algorithm)
SecretKey deskey = keygen generateKey()
// if (debug ) System out println ( 生成密钥byte hex (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 ( 加密前的二进串byte hex (input ))
// System out println ( 加密前的字符串new String (input ))
//
// }
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher ENCRYPT_MODE deskey)
byte[] cipherByte = c doFinal(input)
// if (debug ) System out println ( 加密后的二进串byte hex (cipherByte ))
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input DEFAULT_KEY)
}
/**
* 将给定的已加密的数据通过指定的密钥进行解密
*
* @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 ( 解密前的信息byte hex (input ))
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher DECRYPT_MODE deskey)
byte[] clearByte = c doFinal(input)
// if (debug )
// {
// System out println ( 解密后的二进串byte hex (clearByte ))
// System out println ( 解密后的字符串(new String (clearByte )))
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input DEFAULT_KEY)
}
/**
* 字节码转换成 进制字符串
*
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的 进制字符串
*/
public static String byte hex(byte[] bytes) {
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs append(String format( % $ X b))
return hs toString()
}
public static byte[] hex byte(String content) {
int l=content length()》 ;
byte[] result=new byte[l];
for(int i= ;il;i) {
int j=i《 ;
String s=content substring(j j)
result[i]=Integer valueOf(s ) byteValue()
}
return result;
}
/**
* 将字节数组转换为base 编码字符串
* @param buffer
* @return
*/
public static String bytesToBase (byte[] buffer) {
//BASE Encoder en=new BASE Encoder()
return Base encode(buffer)
//return encoder encode(buffer)
}
/**
* 将base 编码的字符串解码为字节数组
* @param value
* @return
* @throws IOException
*/
public static byte[] base ToBytes(String value) throws IOException {
//return Base decodeToByteArray(value)
//System out println(decoder decodeBuffer(value))
//return decoder decodeBuffer(value)
return Base decode(value)
}
/**
* 加密给定的字符串
* @param value
* @return 加密后的base 字符串
*/
public static String encryptString(String value) {
return encryptString(value DEFAULT_KEY)
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 以BASE 形式存在的密钥
* @return 加密后的base 字符串
* @throws IOException
*/
public static String encryptString(String value String key) throws IOException {
return encryptString(value base ToBytes(key))
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 字节数组形式的密钥
* @return 加密后的base 字符串
*/
public static String encryptString(String value byte[] key) {
try {
byte[] data=https://www.04ip.com/post/value getBytes(VALUE_ENCODING)
data=https://www.04ip.com/post/CryptUtils encryptData(data key)
return bytesToBase (data)
} catch (Exception e) {
// TODO Auto generated catch block
e printStackTrace()
return null;
}
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value DEFAULT_KEY)
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key base 形式存在的密钥
* @return 明文
* @throws IOException
*/
public static String decryptString(String value String key) throws IOException {
String s=decryptString(value base ToBytes(key))
return s;
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key 字节数据形式存在的密钥
* @return 明文
*/
public static String decryptString(String value byte[] key) {
try {
byte[] data=https://www.04ip.com/post/base ToBytes(value)
data=https://www.04ip.com/post/CryptUtils decryptData(data key)
return new String(data VALUE_ENCODING)
}catch(Exception e) {
e printStackTrace()
return null;
}
}
}
CryptHelper java
[java]
package gdie lab crypt;
import javax crypto Cipher;
import javax crypto SecretKey;
import javax crypto SecretKeyFactory;
import javax crypto spec DESKeySpec;
import javax crypto spec IvParameterSpec;
import springframework util DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = zhongqian ;
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()
KEY = KEY substring( )
byte[] bytes = KEY getBytes()
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory getInstance( DES )
SecretKey sk = skf generateSecret(ks)
IvParameterSpec iv = new IvParameterSpec(bytes)
ecip = Cipher getInstance( DES/CBC/PKCS Padding )
ecip init(Cipher ENCRYPT_MODE sk iv )
dcip = Cipher getInstance( DES/CBC/PKCS Padding )
dcip init(Cipher DECRYPT_MODE sk iv )
}catch(Exception ex) {
ex printStackTrace()
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip doFinal(content getBytes( ascii ))
return CryptUtils byte hex(bytes)
}
public static String decrypt(String content) throws Exception {
byte[] bytes= CryptUtils hex byte(content)
bytes = dcip doFinal(bytes)
return new String(bytes ascii )
}
//test
public static void main(String[] args) throws Exception {
String password = gly ;
String en = encrypt(password)
System out println(en)
System out println(decrypt(en))
}
}
package gdie lab crypt;
import javax crypto Cipher;
import javax crypto SecretKey;
import javax crypto SecretKeyFactory;
import javax crypto spec DESKeySpec;
import javax crypto spec IvParameterSpec;
import springframework util DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = zhongqian ;
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()
KEY = KEY substring( )
byte[] bytes = KEY getBytes()
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory getInstance( DES )
SecretKey sk = skf generateSecret(ks)
IvParameterSpec iv = new IvParameterSpec(bytes)
ecip = Cipher getInstance( DES/CBC/PKCS Padding )
ecip init(Cipher ENCRYPT_MODE sk iv )
dcip = Cipher getInstance( DES/CBC/PKCS Padding )
dcip init(Cipher DECRYPT_MODE sk iv )
}catch(Exception ex) {
ex printStackTrace()
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip doFinal(content getBytes( ascii ))
return CryptUtils byte hex(bytes)
}
public static String decrypt(String content) throws Exception {
byte[] bytes= CryptUtils hex byte(content)
bytes = dcip doFinal(bytes)
return new String(bytes ascii )
}
//test
public static void main(String[] args) throws Exception {
String password = gly ;
String en = encrypt(password)
System out println(en)
System out println(decrypt(en))
}
lishixinzhi/Article/program/Java/hx/201311/26449
【java代码用户密码加密 java 代码 加密】java代码用户密码加密的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 代码 加密、java代码用户密码加密的信息别忘了在本站进行查找喔 。

    推荐阅读