怎么对加密的JAVA class文件进行解密

怎么对加密的JAVA class文件进行解密,第1张

JAVA class文件加散野密解密

package com.copy.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=0i<ri++)

{

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 = 0i <ri++)

{

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

}

}

1、file.mkdir() 如果你想在已经存在的文件夹下建立新的文件夹,就可以用此方法。此方法不能在不存在的文件夹下建立新的文件夹。

2、file.mkdirs() 如果你想根据File里的路径名建立文件夹(当你不知道此文件夹歼肢是否存在,也不知道父文件夹存在),就可用此方法,它建立文件夹的原则是:如果父文件夹不存在并且最后一级子文件夹不存在,它就自动新建所有路经里写的文件夹;如果父文件夹存在,它就直接在已经存在的父文件夹下新建子文件夹。

3、createNewFile文件不存在则创建,存在则不创建并返回false,文件路径必须存在才可创建路径下的文件(注意它只能创建文件,即如果你给了/storage/emulated/0/hello/snow/这样一个路径,它最后也只氏谨世是在hello文件夹晌改中创建了snow的未知文件而不是文件夹,如上所述的创建成功的前提还是要/storage/emulated/0/hello/这样的文件夹路径存在,如果只有/storage/emulated/0这样的文件夹路径,它是不能够创建hello文件夹的,所以创建失败)

4、mkdirs()和mkdirs()专门用来创建文件夹的,不存在则创建返回true,存在则返回false,区别在于mkdirs可以creating missing parent directories if necessary.同样的路径为/storage/emulated/0/hello/snow.bin也只是在hello文件夹啊下创建了snow.bin文件夹而不是文件。

5、所以一般需要createNewFile()和mkdirs()结合使用,先创建文件夹再创建文件。

可以先切换到滚肆亩指定的文件夹路径下,之后直接通过mkdir方法进行文件夹创建。举例:

String path = "d:/oldfilepath"//定义指定文件路径

String newPath = path+"/newpath"//指定新路径

File file = new File(newPath )//定义一个文件流

file.mkdir()//创建文件夹

备注:如果不确定原有文件夹是否存在的情况下,可以通过”mkdirs“创建多层雹埋路径。大森


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12271153.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存