网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java代码永久解封 java封包解包

java加密解密代码

package com.cube.limail.util;

10年的梅里斯网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整梅里斯建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“梅里斯网站设计”,“梅里斯网站推广”以来,每个客户项目都认真落实执行。

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[] 返回生成的密钥

* @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); //传入密码和钥匙,获得加密后的字节数组的密码

password=Eryptogram.bytesToHexString(tmp); //将字节数组转化为字符串,获得加密后的字符串密码解密与之差不多

程序中,用户账号被禁用,其解封的实现思路是什么?(java)

数据库用中一个字段表示封号时间 存入Date转换的Long值

用户登录时,通过系统当前时间与数据库读到的时间比对就知道当前是不是封号状态了。。

这段JAVA代码什么意思

javax.crypto.Cipher类提供加密和解密功能,该类是JCE框架的核心。

一,与所有的引擎类一样,可以通过调用Cipher类中的getInstance静态工厂方法得到Cipher对象。

public static Cipher getInstance(String transformation);

public static Cipher getInstance(String transformation,String provider);

参数transformation是一个字符串,它描述了由指定输入产生输出所进行的操作或操作集合。

参数transformation总是包含密码学算法名称,比如DES,也可以在后面包含模式和填充方式。

参数transformation可以是下列两种形式之一:

“algorithm/mode/padding”

“algorithm”

例如下面的例子就是有效的transformation形式:

"DES/CBC/PKCS5Padding"

"DES"

如 果没有指定模式或填充方式,就使用特定提供者指定的默认模式或默认填充方式。例如,SunJCE提供者使用ECB作为DES、DES-EDE和 Blowfish等Cipher的默认模式,并使用PKCS5Padding作为它们默认的填充方案。这意味着在SunJCE提供者中,下列形式的声明是 等价的:Cipher c1=Cipher.getInstance("DES/ECB/PKCS5Padding");

Cipher c1=Cipher.getInstance("DES");

当 以流加密方式请求以块划分的cipher时,可以在模式名后面跟上一次运算需要操作的bit数目,例如采用"DES/CFB8/NoPadding"和 "DES/OFB32/PKCS5Padding"形式的transformation参数。如果没有指定数目,则使用提供者指定的默认值(例如 SunJCE提供者使用的默认值是64bit)。

getInstance工厂方法返回的对象没有进行初始化,因此在使用前必须进行初始化。

通过getInstance得到的Cipher对象必须使用下列四个模式之一进行初始化,这四个模式在Cipher类中被定义为final integer常数,我们可以使用符号名来引用这些模式:

ENCRYPT_MODE,加密数据

DECRYPT_MODE,解密数据

WRAP_MODE,将一个Key封装成字节,可以用来进行安全传输

UNWRAP_MODE,将前述已封装的密钥解开成java.security.Key对象

每个Cipher初始化方法使用一个模式参数opmod,并用此模式初始化Cipher对象。此外还有其他参数,包括密钥key、包含密钥的证书certificate、算法参数params和随机源random。

我们可以调用以下的init方法之一来初始化Cipher对象:

public void init(int opmod,Key key);

public void init(int opmod,Certificate certificate);

public void init(int opmod,Key key,SecureRandom random);

public void init(int opmod,Certificate certificate,SecureRandom random);

public void init(int opmod,Key key,AlgorithmParameterSpec params);

public void init(int opmod,Key key,AlgorithmParameterSpec params,SecureRandom random);

public void init(int opmod,Key key,AlgorithmParameters params);

public void init(int opmod,Key key,AlgorithmParameters params,SecureRandom random);

必须指出的是,加密和解密必须使用相同的参数。当Cipher对象被初始化时,它将失去以前得到的所有状态。即,初始化Cipher对象与新建一个Cipher实例然后将它初始化是等价的。

二,可以调用以下的doFinal()方法之一完成单步的加密或解密数据:

public byte[] doFinal(byte[] input);

public byte[] doFinal(byte[] input,int inputOffset,int inputLen);

public int doFinal(byte[] input,int inputOffset,int inputLen,byte[] output);

public int doFinal(byte[] input,int inputOffset,int inputLen,byte[] output,int outputOffset);

在多步加密或解密数据时,首先需要一次或多次调用update方法,用以提供加密或解密的所有数据:

public byte[] update(byte[] input);

public byte[] update(byte[] input,int inputOffset,int inputLen);

public int update(byte[] input,int inputOffset,int inputLen,byte[] output);

public int update(byte[] input,int inputOffset,int inputLen,byte[] output,int outputOffset);

如果还有输入数据,多步操作可以使用前面提到的doFinal方法之一结束。如果没有数据,多步操作可以使用下面的doFinal方法之一结束:

public byte[] doFinal();

public int doFinal(byte[] output,int outputOffset);

如果在transformation参数部分指定了padding或unpadding方式,则所有的doFinal方法都要注意所用的padding或unpadding方式。

调用doFinal方法将会重置Cipher对象到使用init进行初始化时的状态,就是说,Cipher对象被重置,使得可以进行更多数据的加密或解密,至于这两种模式,可以在调用init时进行指定。

三,包裹wrap密钥必须先使用WRAP_MODE初始化Cipher对象,然后调用以下方法:

public final byte[] wrap(Key key);

如果将调用wrap方法的结果(wrap后的密钥字节)提供给解包裹unwrap的人使用,必须给接收者发送以下额外信息:

(1)密钥算法名称:

密钥算法名称可以调用Key接口提供的getAlgorithm方法得到:

public String getAlgorithm();

(2)被包裹密钥的类型(Cipher.SECRET_KEY,Cipher.PRIVATE_KEY,Cipher.PUBLIC_KEY)

sourcelink: ;nid=41716order=tid=

为了对调用wrap方法返回的字节进行解包,必须先使用UNWRAP_MODE模式初始化Cipher对象,然后调用以下方法 :

public final Key unwrap(byte[] wrappedKey,String wrappedKeyAlgorithm,int wrappedKeyType));

其 中,参数wrappedKey是调用wrap方法返回的字节,参数wrappedKeyAlgorithm是用来包裹密钥的算法,参数 wrappedKeyType是被包裹密钥的类型,该类型必须是Cipher.SECRET_KEY,Cipher.PRIVATE_KEY, Cipher.PUBLIC_KEY三者之一。

四,SunJCE提供者实现的cipher算法使用如下参数:

(1)采用CBC、CFB、OFB、PCBC模式的DES、DES-EDE和Blowfish算法。,它们使用初始化向量IV作为参数。可以使用javax.crypto.spec.IvParameterSpec类并使用给定的IV参数来初始化Cipher对象。

(2)PBEWithMD5AndDES使用的参数是一个由盐值和迭代次数组成的参数集合。可以使用javax.crypto.spec.PBEParameterSpec类并利用给定盐值和迭代次数来初始化Cipher对象。

注意:如果使用SealedObject类,就不必为解密运算参数的传递和保存担心。这个类在加密对象内容中附带了密封和加密的参数,可以使用相同的参数对其进行解封和解密。

Cipher 中的某些update和doFinal方法允许调用者指定加密或解密数据的输出缓存。此时,保证指定的缓存足够大以容纳加密或解密运算的结果是非常重要 的


分享文章:java代码永久解封 java封包解包
网页网址:http://bjjierui.cn/article/doohdho.html

其他资讯