java代码免费加密,java代码加密方案
java加密的几种方式
基本的单向加密算法:
企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,创新互联公司面向各种领域:成都木包装箱等成都网站设计、成都全网营销推广解决方案、网站设计等建站排名服务。
BASE64 严格地说,属于编码格式,而非加密算法
MD5(Message Digest algorithm 5,信息摘要算法)
SHA(Secure Hash Algorithm,安全散列算法)
HMAC(Hash Message Authentication Code,散列消息鉴别码)
复杂的对称加密(DES、PBE)、非对称加密算法:
DES(Data Encryption Standard,数据加密算法)
PBE(Password-based encryption,基于密码验证)
RSA(算法的名字以发明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman)
DH(Diffie-Hellman算法,密钥一致协议)
DSA(Digital Signature Algorithm,数字签名)
ECC(Elliptic Curves Cryptography,椭圆曲线密码编码学)
代码参考:
/**
* BASE64加密
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* MD5加密
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptMD5(byte[] data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
return md5.digest();
}
/**
* SHA加密
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptSHA(byte[] data) throws Exception {
MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
sha.update(data);
return sha.digest();
}
}
/**
* 初始化HMAC密钥
*
* @return
* @throws Exception
*/
public static String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
SecretKey secretKey = keyGenerator.generateKey();
return encryptBASE64(secretKey.getEncoded());
}
/**
* HMAC加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(data);
}
JAVA程序加密,怎么做才安全
程序加密?你说的是代码加密还是数据加密。我都说一下吧。
Java代码加密:
这点因为Java是开源的,想达到完全加密,基本是不可能的,因为在反编译的时候,虽然反编译回来的时候可能不是您原来的代码,但是意思是接近的,所以是不行的。
那么怎么增加反编译的难度(阅读难度),那么可以采用多层继承(实现)方式来解决,这样即使反编译出来的代码,可读性太差,复用性太差了。
Java数据加密:
我们一般用校验性加密,常用的是MD5,优点是速度快,数据占用空间小。缺点是不可逆,所以我们一般用来校验数据有没有被改动等。
需要可逆,可以选用base64,Unicode,缺点是没有密钥,安全性不高。
而我们需要可逆而且采用安全的方式是:对称加密和非堆成加密,我们常用的有AES、DES等单密钥和双密钥的方式。而且是各种语言通用的。
全部手动敲字,望采纳,下面是我用Javascript方式做的一系列在线加密/解密工具:
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[] 返回生成的密钥
* @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文件加密代码
package com.happy.security.properties;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class DESEncryptUtil {
public static Key createKey() throws NoSuchAlgorithmException {//创建密钥
Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 1);
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());
Key key = generator.generateKey();
return key;
}
public static Key getKey(InputStream is) {
try {
ObjectInputStream ois = new ObjectInputStream(is);
return (Key) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private static byte[] doEncrypt(Key key, byte[] data) {//对数据进行加密?
try {
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] raw = cipher.doFinal(data);
return raw;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static InputStream doDecrypt(Key key, InputStream in) {//对数据进行解密?
try {
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[1024];
int count = 0;
while ((count = in.read(tmpbuf)) != -1) {
bout.write(tmpbuf, 0, count);
tmpbuf = new byte[1024];
}
in.close();
byte[] orgData = bout.toByteArray();
byte[] raw = cipher.doFinal(orgData);
ByteArrayInputStream bin = new ByteArrayInputStream(raw);
return bin;
} catch (Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws Exception{
System.out.println("===================");
if (args.length == 2 args[0].equals("key")){
Key key = DESEncryptUtil.createKey();
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(args[1]));
oos.writeObject(key);
oos.close();
System.out.println("成功生成密钥文件");
} else if (args.length == 3 args[0].equals("encrypt")){
File file = new File(args[1]);
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[1024];
int count = 0;
while ((count = in.read(tmpbuf)) != -1){
bout.write(tmpbuf, 0, count);
tmpbuf = new byte[1024];
}
in.close();
byte[] orgData = bout.toByteArray();
Key key = getKey(new FileInputStream(args[2]));
byte[] raw = DESEncryptUtil.doEncrypt(key, orgData);
file = new File(file.getParent() + "\\en_" + file.getName());
FileOutputStream out = new FileOutputStream(file);
out.write(raw);
out.close();
System.out.println("成功加密,加密文件位:"+file.getAbsolutePath());
} else if (args.length == 3 args[0].equals("decrypt")){//对文件进行解密
File file = new File(args[1]);
FileInputStream fis = new FileInputStream(file);
Key key = getKey(new FileInputStream(args[2]));
InputStream raw = DESEncryptUtil.doDecrypt(key, fis);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[1024];
int count = 0;
while ((count = raw.read(tmpbuf)) != -1){
bout.write(tmpbuf, 0, count);
tmpbuf = new byte[1024];
}
raw.close();
byte[] orgData = bout.toByteArray();
file = new File(file.getParent() + "\\rs_" + file.getName());
FileOutputStream fos = new FileOutputStream(file);
fos.write(orgData);
System.out.println("成功解密,解密文件位:"+file.getAbsolutePath());
}else if(args.length==1 args[0].equals("-h")) {
System.out.println("\t文件加密解密\n");
System.out.println
分享名称:java代码免费加密,java代码加密方案
URL网址:http://pwwzsj.com/article/hodihd.html