class文件加密
最简单的方法就是让用户不能够访问到Java Class程序,这种方法是最根本的方法,具体实现有多种方式。例如,开发人员可以将关键的Java Class放在服务器端,客户端通过访问服务器的相关接口来获得服务,而不是直接访问Class文件。这样黑客就没有办法反编译Class文件。
2.对Class文件进行加密
为了防止 Class 文件被直接反编译,许多开发人员将一些关键的 Class 文件进行加密,例如对注册码、序列号管理相关的类等。在使用这些被加密的类之前,程序首先需要对这些类进行解密,而后再将这些类装载到JVM当中。这些类的解密可以由硬件完成,也可以使用软件完成。
② javaclass类加密后 如何部署到tomcat
1.使用加密程序对classes下所有文件加密,加密之后所有的class文件后缀变为uikoo9,可以自己修改源代码
2.将原classes文件夹删除,将加密后的classes文件夹复制进去
3.修改context.xml
4.tomcat\lib下添加loader.class
5.启动tomcat!
③ 为什么反编译点class文件不能被编辑啊
由于Java字节码的抽象级别较高,因此它们较容易被反编译。下面介绍了几种常用的方法,用于保护Java字节码不被反编译。
1.隔离Java程序
2.对Class文件进行加密
3.转换成本地代码
4.代码混淆
④ JAVA程序加密,怎么做才安全
程序加密?你说的是代码加密还是数据加密。我都说一下吧。
Java代码加密:
这点因为Java是开源的,想达到完全加密,基本是不可能的,因为在反编译的时候,虽然反编译回来的时候可能不是您原来的代码,但是意思是接近的,所以是不行的。
那么怎么增加反编译的难度(阅读难度),那么可以采用多层继承(实现)方式来解决,这样即使反编译出来的代码,可读性太差,复用性太差了。
Java数据加密:
我们一般用校验性加密,常用的是MD5,优点是速度快,数据占用空间小。缺点是不可逆,所以我们一般用来校验数据有没有被改动等。
需要可逆,可以选用base64,Unicode,缺点是没有密钥,安全性不高。
而我们需要可逆而且采用安全的方式是:对称加密和非堆成加密,我们常用的有AES、DES等单密钥和双密钥的方式。而且是各种语言通用的。
全部手动敲字,望采纳,下面是我用Javascript方式做的一系列在线加密/解密工具:
http://www.sojson.com/encrypt.html
⑤ 怎么对加密的JAVA class文件进行解密
JAVA class文件加密解密
package com..encrypt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
public class FileEncryptAndDecrypt {
/**
* 文件file进行加密
* @param fileUrl 文件路径
* @param key 密码
* @throws Exception
*/
public static void encrypt(String fileUrl, String key) throws Exception {
File file = new File(fileUrl);
String path = file.getPath();
if(!file.exists()){
return;
}
int index = path.lastIndexOf("\\");
String destFile = path.substring(0, index)+"\\"+"abc";
File dest = new File(destFile);
InputStream in = new FileInputStream(fileUrl);
OutputStream out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int r;
byte[] buffer2=new byte[1024];
while (( r= in.read(buffer)) > 0) {
for(int i=0;i<r;i++)
{
byte b=buffer[i];
buffer2[i]=b==255?0:++b;
}
out.write(buffer2, 0, r);
out.flush();
}
in.close();
out.close();
file.delete();
dest.renameTo(new File(fileUrl));
appendMethodA(fileUrl, key);
System.out.println("加密成功");
}
/**
*
* @param fileName
* @param content 密钥
*/
public static void appendMethodA(String fileName, String content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
//将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解密
* @param fileUrl 源文件
* @param tempUrl 临时文件
* @param ketLength 密码长度
* @return
* @throws Exception
*/
public static String decrypt(String fileUrl, String tempUrl, int keyLength) throws Exception{
File file = new File(fileUrl);
if (!file.exists()) {
return null;
}
File dest = new File(tempUrl);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
InputStream is = new FileInputStream(fileUrl);
OutputStream out = new FileOutputStream(tempUrl);
byte[] buffer = new byte[1024];
byte[] buffer2=new byte[1024];
byte bMax=(byte)255;
long size = file.length() - keyLength;
int mod = (int) (size%1024);
int div = (int) (size>>10);
int count = mod==0?div:(div+1);
int k = 1, r;
while ((k <= count && ( r = is.read(buffer)) > 0)) {
if(mod != 0 && k==count) {
r = mod;
}
for(int i = 0;i < r;i++)
{
byte b=buffer[i];
buffer2[i]=b==0?bMax:--b;
}
out.write(buffer2, 0, r);
k++;
}
out.close();
is.close();
return tempUrl;
}
/**
* 判断文件是否加密
* @param fileName
* @return
*/
public static String readFileLastByte(String fileName, int keyLength) {
File file = new File(fileName);
if(!file.exists())return null;
StringBuffer str = new StringBuffer();
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
//将写文件指针移到文件尾。
for(int i = keyLength ; i>=1 ; i--){
randomFile.seek(fileLength-i);
str.append((char)randomFile.read());
}
randomFile.close();
return str.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
⑥ class文件详解
能够被JVM识别,加载并执行的文件格式
1.通过IDE自动帮我们build。
2.手动通过javac去生成class文件。
记录一个类文件的所有信息。
1.一种8位字节的二进制流文件
2.各个数据按顺序紧密地排列,无间隙 (这样做的好处可以减少class文件的体积,jvm加载我们class文件的时候更加快速)
3.每个类、接口和枚举都单独占据一个class文件(这样做的好处是每个累接口等都可以独自管理自己内部的内容而无需相互交叉)
整体文件格式:
格式详解:
1.magic
无符号4字节,用来表示class文件的开头,加密段,给虚拟机用来判断当前的 class文件是否被串改过。
2.minor_version
class文件最小可以被哪个版本的jdk所加载,也就是最小适配的jdk
3.major_version
表示我们当前class文件是由哪个版本的jdk生成的。
4.constant_pool_count
class文件中常量池的数量,通常只有一个常量池。
5.constant_pool
代表常量池,类型为cp_info(结构体类型)。
常量池中主要包含的内容:
首先列举三个比较简单的
CONSTANT_Integer_info:存储class文件中的int类型。
CONSTANT_Long_info:存储class文件中的long类型。
CONSTANT_String_info:存储class文件中的string类型。
它们分别存储字节码中的int、long、string类型,当然还有CONSTANT_Short_info、CONSTANT_Float_info等。
下面列举几个稍微复杂的
CONSTANT_Class_info:记录类中相关的信息、不仅记录了当前类的信息,还记录了引用到的一些类的信息。
CONSTANT_Fieldref_info:记录类中Field相关的信息。
CONSTANT_Methodref_info:记录类中Method相关的信息。
这三个里面存储的并不是真正的内容,都是一些索引,这些索引指向的又是CONSTANT_String_info等。
6.access_flags
表示class文件的作用域标志,比如:public 、public final
取值范围:
7.this_class
this_class是指向constant pool的索引值,该值必须是CONSTANT_Class_info类型,指定当前字节码定义的类或接口。
8.super_class
super_class是指向constant pool的索引值,该值必须是CONSTANT_Class_info类型,指定当前字节码定义的类或接口的直接父类。只有Object类才没有直接父类,此时该索引值为0。并且父类不能是final类型。接口的父类都是Object类。
9.interfaces_count
当前class文件直接实现的接口数量。
10.interfaces
当前class文件直接实现的接口,只记录直接实现的,不会记录间接实现的。
11.fields_count
class文件中成员变量的数量。
12.fields
class文件中所有的成员变量,field_info类型的结构体,该结构体中主要包含了每个成员变量的name、所属的类以及类型。
13.methods_count
记录class文件中方法的数量。
14.methods
记录class文件中所有的方法,类型为method_info结构体类型,主要包含了方法的名字、类型、access_flags等信息。
15.attribute_count
记录了class文件属性的数量。
16.attributes
记录class文件的一些属性,除去上面的一些信息剩下的都包含在attributes中,比如 说注解。
1.内存占用大,不适合移动端。
2.堆栈的加载模式,加载速度慢。
3.文件IO操作多,类查找慢。
⑦ Class文件加密后,tomcat启动错误,怎么解决
给你提供几种方式吧: 1:最快最直接的办法,购买第三方的加密软件,我以前用的就是第三方的加密软件。 2:对代码进行加密,更改自己的JDK和tomcat的在解析的时候使用解密,这种的不够级别就别改了。 这个很早就有人研究了。
⑧ 怎么能让spring框架加载加密后的.class文件
加密:使用AES加密,将文件的字节码读取,对字节码进行加密后替换源文件
Java代码
/**
*
* 字节加密
*/
public static byte[] encrypt(byte[] data, String key) throws Exception {
Key k = toKey(Base64.decode(key));
byte[] raw = k.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
/**
*
* 字节加密
*/
public static byte[] encrypt(byte[] data, String key) throws Exception {
Key k = toKey(Base64.decode(key));
byte[] raw = k.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
解密:
1、在tomcat的WebappClassLoader中修改源码(自动义类加载器);
2、修改spring源码Code包源码。
加密方法
Java代码
public static byte[] decrypt(byte[] data, String key) throws Exception {
Key k = toKey(Base64.decode(key));
byte[] raw = k.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, String key) throws Exception {
Key k = toKey(Base64.decode(key));
byte[] raw = k.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
在 WebappClassLoader中解密
Java代码
/**
* 判断如需是需要解密的类进行数据处理
* */
//--------------------------------------start----------------------------------//
byte []data=null;
try {
if(isDecode(name)){
System.out.println("2818:--&&&-"+name);
data=AESUtils.decrypt(entry.binaryContent, key);
}else{
data=entry.binaryContent;
}
} catch (Exception e) {
e.printStackTrace();
}
try {
clazz = defineClass(name, data, 0,
data.length,
new CodeSource(entry.codeBase, entry.certificates));
//--------------------------------------end----------------------------------//
/**
* 判断如需是需要解密的类进行数据处理
* */
//--------------------------------------start----------------------------------//
byte []data=null;
try {
if(isDecode(name)){
System.out.println("2818:--&&&-"+name);
data=AESUtils.decrypt(entry.binaryContent, key);
}else{
data=entry.binaryContent;
}
} catch (Exception e) {
e.printStackTrace();
}
try {
clazz = defineClass(name, data, 0,
data.length,
new CodeSource(entry.codeBase, entry.certificates));
//--------------------------------------end----------------------------------//
在spring的code包的SimpleMetadataReader修改器构造函数
Java代码
// TODO 修改源码判断是否需要解密
SimpleMetadataReader(Resource resource, ClassLoader classLoader)
throws IOException {
InputStream is = resource.getInputStream();
ClassReader classReader = null;
try {
String name = "";
if (resource.getURI().toString().indexOf("jar:file") == -1) {
name = resource.getFile().getAbsolutePath();
if (!"".equals(name) && isDecode(name, cams)) {
byte[] data = inputStreamToByte(is);
try {
is = new ByteArrayInputStream(AESUtils.decrypt(data,
key));
// is = new ByteArrayInputStream(data);
} catch (Exception e) {
e.printStackTrace();
}
}
}
classReader = new ClassReader(is);
} finally {
is.close();
}
// TODO 修改源码判断是否需要解密
SimpleMetadataReader(Resource resource, ClassLoader classLoader)
throws IOException {
InputStream is = resource.getInputStream();
ClassReader classReader = null;
try {
String name = "";
if (resource.getURI().toString().indexOf("jar:file") == -1) {
name = resource.getFile().getAbsolutePath();
if (!"".equals(name) && isDecode(name, cams)) {
byte[] data = inputStreamToByte(is);
try {
is = new ByteArrayInputStream(AESUtils.decrypt(data,
key));
// is = new ByteArrayInputStream(data);
} catch (Exception e) {
e.printStackTrace();
}
}
}
classReader = new ClassReader(is);
} finally {
is.close();
}
在同样需要进行解密。
注:(此加密有弊端)
1、加密解密算法需保持一致。
2、加密加密密钥需是同一密钥。
⑨ 防止Class类反编译的方法,对Class进行加密
这个我之前也研究过,现在与你分享一下吧。希望对你有帮助:
首先在介绍反编译器之前,要提及JDK自带的一个工具 [javap] ,它是一个Java代码反汇编器。
然后其次是有一个sourceforge中开源的一个反编译器 Jode(http://jode.sourceforge.net/download.html) 下载。
反编译对安全构成的威胁是显而易见的,因此源码保护也就必不可少的。其实,反编译和代码保护是一场无停止斗争,双方都在争斗中得以发展。
那么到目前为止,保护源码大致可以分为三类: 加密、模糊、和定制JAVA类装载器。现在依我看来,流行的工具有 PGP(Pretty Good Privacy) 和 GPG(Gnu Pirvacy Guard)等等(有些名字忘记了!呵呵!)
我先讲讲模糊技术吧,Obfuscator就是对源代码进行模糊化处理的行为。但是经过模糊处理后的代码,将失去了一些可读 性,程序员很难识别代码的用意。利用模糊处理的工具是 Smokescreen(http://www.leesw.com/smokescreen/licensedownload.html).....
至于类加载器,你只需要看看ClassLoader的原理就行了。具体代码实现的太多了,打字打的累!!!!
我先说这么多了,如果还有什么不明白的可以加我MSN与我讨论:[email protected]
即可!