问题描述:
各位好,请求各位java学习者帮助钉解决这个问题。
我也在网上下载了des算法,包括FileDES,SubKey,Des各程序,
可能没真正理解这些程序,所以我想调用都不知道将这些东西
组合起来,有知道的请帮帮忙啊!
解析:
package des
import java.io.*
import java.nio.*
import java.nio.channels.FileChannel
public class FileDES{
private static final boolean enc=true加密
private static final boolean dec=false解密
private String srcFileName
private String destFileName
private String inKey
private boolean actionType
private File srcFile
private File destFile
private Des des
private void *** yzePath(){
String dirName
int pos=srcFileNamestIndexOf("/")
dirName=srcFileName.substring(0,pos)
File dir=new File(dirName)
if (!dir.exists()){
System.err.println(dirName+" is not exist")
System.exit(1)
}else if(!dir.isDirectory()){
System.err.println(dirName+" is not a directory")
System.exit(1)
}
pos=destFileNamestIndexOf("/")
dirName=destFileName.substring(0,pos)
dir=new File(dirName)
if (!dir.exists()){
if(!dir.mkdirs()){
System.out.println ("can not creat directory:"+dirName)
System.exit(1)
}
}else if(!dir.isDirectory()){
System.err.println(dirName+" is not a directory")
System.exit(1)
}
}
private static int replenish(FileChannel channel,ByteBuffer buf) throws IOException{
long byteLeft=channel.size()-channel.position()
if(byteLeft==0L)
return -1
buf.position(0)
buf.limit(buf.position()+(byteLeft<8 ? (int)byteLeft :8))
return channel.read(buf)
}
private void file_operate(boolean flag){
des=new Des(inKey)
FileOutputStream outputFile=null
try {
outputFile=new FileOutputStream(srcFile,true)
}catch (java.io.FileNotFoundException e) {
e.printStackTrace(System.err)
}
FileChannel outChannel=outputFile.getChannel()
try{
if(outChannel.size()%2!=0){
ByteBuffer bufTemp=ByteBuffer.allocate(1)
bufTemp.put((byte)32)
bufTemp.flip()
outChannel.position(outChannel.size())
outChannel.write(bufTemp)
bufTemp.clear()
}
}catch(Exception ex){
ex.printStackTrace(System.err)
System.exit(1)
}
FileInputStream inFile=null
try{
inFile=new FileInputStream(srcFile)
}catch(java.io.FileNotFoundException e){
e.printStackTrace(System.err)
System.exit(1)
}
outputFile=null
try {
outputFile=new FileOutputStream(destFile,true)
}catch (java.io.FileNotFoundException e) {
e.printStackTrace(System.err)
}
FileChannel inChannel=inFile.getChannel()
outChannel=outputFile.getChannel()
ByteBuffer inBuf=ByteBuffer.allocate(8)
ByteBuffer outBuf=ByteBuffer.allocate(8)
try{
String srcStr
String destStr
while(true){
if (replenish(inChannel,inBuf)==-1) break
srcStr=((ByteBuffer)(inBuf.flip())).asCharBuffer().toString()
inBuf.clear()
if (flag)
destStr=des.enc(srcStr,srcStr.length())
else
destStr=des.dec(srcStr,srcStr.length())
outBuf.clear()
if (destStr.length()==4){
for (int i = 0i<4i++) {
outBuf.putChar(destStr.charAt(i))
}
outBuf.flip()
}else{
outBuf.position(0)
outBuf.limit(2*destStr.length())
for (int i = 0i<destStr.length()i++) {
outBuf.putChar(destStr.charAt(i))
}
outBuf.flip()
}
try {
outChannel.write(outBuf)
outBuf.clear()
}catch (java.io.IOException ex) {
ex.printStackTrace(System.err)
}
}
System.out.println (inChannel.size())
System.out.println (outChannel.size())
System.out.println ("EoF reached.")
inFile.close()
outputFile.close()
}catch(java.io.IOException e){
e.printStackTrace(System.err)
System.exit(1)
}
}
public FileDES(String srcFileName,String destFileName,String inKey,boolean actionType){
this.srcFileName=srcFileName
this.destFileName=destFileName
this.actionType=actionType
*** yzePath()
srcFile=new File(srcFileName)
destFile=new File(destFileName)
this.inKey=inKey
if (actionType==enc)
file_operate(enc)
else
file_operate(dec)
}
public static void main(String[] args){
String file1=System.getProperty("user.dir")+"/111.doc"
String file2=System.getProperty("user.dir")+"/222.doc"
String file3=System.getProperty("user.dir")+"/333.doc"
String passWord="1234ABCD"
FileDES fileDes=new FileDES(file1,file2,passWord,true)
FileDES fileDes1=new FileDES(file2,file3,passWord,false)
}
java.security.MessageDigest
/** MD5 算法
*/
public class MD5 {
// 全局数组
private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }
public MD5() {
}
// 返回形式为数字跟字符串
private static String byteToArrayString(byte bByte) {
int iRet = bByte
// System.out.println("iRet="+iRet)
if (iRet < 0) {
iRet += 256
}
int iD1 = iRet / 16
int iD2 = iRet % 16
return strDigits[iD1] + strDigits[iD2]
}
// 返回形式只为数字
private static String byteToNum(byte bByte) {
int iRet = bByte
System.out.println("iRet1=" + iRet)
if (iRet < 0) {
iRet += 256
}
return String.valueOf(iRet)
}
// 转换字节数组为16进制字串
private static String byteToString(byte[] bByte) {
StringBuffer sBuffer = new StringBuffer()
for (int i = 0 i < bByte.length i++) {
sBuffer.append(byteToArrayString(bByte[i]))
}
return sBuffer.toString()
}
public static String GetMD5Code(String strObj) {
String resultString = null
try {
resultString = new String(strObj)
MessageDigest md = MessageDigest.getInstance("MD5")
// md.digest() 该函数返回值为存放哈希值结果的byte数组
resultString = byteToString(md.digest(strObj.getBytes()))
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace()
}
return resultString
}
public static void main(String[] args) {
MD5 getMD5 = new MD5()
System.out.println(getMD5.GetMD5Code("000000"))
}
}
不需要第三方包,java自带就有。我给你个例子。
import java.security.InvalidKeyException
import java.security.NoSuchAlgorithmException
import java.security.Security
import javax.crypto.BadPaddingException
import javax.crypto.Cipher
import javax.crypto.IllegalBlockSizeException
import javax.crypto.KeyGenerator
import javax.crypto.NoSuchPaddingException
import javax.crypto.SecretKey
public class EncrypDES {
//KeyGenerator 提供对称密钥生成器的功能,支持各种算法
private KeyGenerator keygen
//SecretKey 负责保存对称密钥
private SecretKey deskey
//Cipher负责完成加密或解密工作
private Cipher c
//该字节数组负责保存加密的结果
private byte[] cipherByte
public EncrypDES() throws NoSuchAlgorithmException, NoSuchPaddingException{
Security.addProvider(new com.sun.crypto.provider.SunJCE())
//实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
keygen = KeyGenerator.getInstance("DES")
//生成密钥
deskey = keygen.generateKey()
//生成Cipher对象,指定其支持的DES算法
c = Cipher.getInstance("DES")
}
/**
* 对字符串加密
*
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public byte[] Encrytor(String str) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, deskey)
byte[] src = str.getBytes()
// 加密,结果保存进cipherByte
cipherByte = c.doFinal(src)
return cipherByte
}
/**
* 对字符串解密
*
* @param buff
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, deskey)
cipherByte = c.doFinal(buff)
return cipherByte
}
/**
* @param args
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws InvalidKeyException
*/
public static void main(String[] args) throws Exception {
EncrypDES de1 = new EncrypDES()
String msg ="郭XX-搞笑相声全集"
byte[] encontent = de1.Encrytor(msg)
byte[] decontent = de1.Decryptor(encontent)
System.out.println("明文是:" + msg)
System.out.println("加密后:" + new String(encontent))
System.out.println("解密后:" + new String(decontent))
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)