用java实现des算法

用java实现des算法,第1张

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

各位好,请求各位java学习者帮助钉解决这稿蔽局个问题。

我想用des算法对我的名键让字进行加密

我也在网上下载了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)

}

.security.*

import javax.crypto.*

import javax.crypto.spec.SecretKeySpec

public class ThreeDes {

private static final String Algorithm = "DESede"//定义 加密算法,可用 DES,DESede,Blowfish

//keybyte为盯腊加密密钥,长度为24字节凯闭滑

//src为被加密的数据缓冲区(源)

public static byte[] encryptMode(byte[] keybyte, byte[] src) {

try {

//生成密钥

SecretKey deskey = new SecretKeySpec(keybyte, Algorithm)

//态让加密

Cipher c1 = Cipher.getInstance(Algorithm)

c1.init(Cipher.ENCRYPT_MODE, deskey)

return c1.doFinal(src)

} catch (java.security.NoSuchAlgorithmException e1) {

e1.printStackTrace()

} catch (javax.crypto.NoSuchPaddingException e2) {

e2.printStackTrace()

} catch (java.lang.Exception e3) {

e3.printStackTrace()

}

return null

}

//keybyte为加密密钥,长度为24字节

//src为加密后的缓冲区

public static byte[] decryptMode(byte[] keybyte, byte[] src) {

try {

//生成密钥

SecretKey deskey = new SecretKeySpec(keybyte, Algorithm)

//解密

Cipher c1 = Cipher.getInstance(Algorithm)

c1.init(Cipher.DECRYPT_MODE, deskey)

return c1.doFinal(src)

} catch (java.security.NoSuchAlgorithmException e1) {

e1.printStackTrace()

} catch (javax.crypto.NoSuchPaddingException e2) {

e2.printStackTrace()

} catch (java.lang.Exception e3) {

e3.printStackTrace()

}

return null

}

//转换成十六进制字符串

public static String byte2hex(byte[] b) {

String hs=""

String stmp=""

for (int n=0n<b.lengthn++) {

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()

}

public static void main(String[] args)

{

//添加新安全算法,如果用JCE就要把它添加进去

Security.addProvider(new com.sun.crypto.provider.SunJCE())

final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte)0x88, 0x10, 0x40, 0x38

, 0x28, 0x25, 0x79, 0x51, (byte)0xCB, (byte)0xDD, 0x55, 0x66

, 0x77, 0x29, 0x74, (byte)0x98, 0x30, 0x40, 0x36, (byte)0xE2}//24字节的密钥

String szSrc = "This is a 3DES test. 测试"

System.out.println("加密前的字符串:" + szSrc)

byte[] encoded = encryptMode(keyBytes, szSrc.getBytes())

System.out.println("加密后的字符串:" + new String(encoded))

byte[] srcBytes = decryptMode(keyBytes, encoded)

System.out.println("解密后的字符串:" + (new String(srcBytes)))

}

}


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

原文地址: http://outofmemory.cn/yw/12560914.html

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

发表评论

登录后才能评论

评论列表(0条)

保存