當前位置:首頁 » 密碼管理 » java的des加密演算法

java的des加密演算法

發布時間: 2023-05-12 08:03:04

『壹』 java 中使用DES加密演算法 生產的密鑰為啥是8個位元組

使用DES加密演算法生產密鑰,java6對DES演算法僅支持56位密鑰長度,但生成的密鑰是64位的.在這64位中,實際的密鑰只有56位,另有8位是奇偶校驗位,分布灶森悄於64位密鑰中,每隱渣8位中春碼有1 位奇偶檢驗位.

『貳』 用java實現DES加密演算法,細致點,要直接粘貼進平台能運行的!!

/*des密鑰生成代碼*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import com.huateng.util.common.Log;

public class GenKey {

private static final String DES = "DES";
public static final String SKEY_NAME = "key.des";

public static void genKey1(String path) {

// 密鑰
SecretKey skey = null;
// 密鑰隨機數生成
SecureRandom sr = new SecureRandom();
//生成密鑰文件
File file = genFile(path);

try {
// 獲取密鑰生成實例
KeyGenerator gen = KeyGenerator.getInstance(DES);
// 初始化密鑰生成器
gen.init(sr);
// 生成密鑰
skey = gen.generateKey();
// System.out.println(skey);

ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeObject(skey);
oos.close();
Log.sKeyPath(path);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* @param file : 生成密鑰的路徑
* SecretKeyFactory 方式生成des密鑰
* */
public static void genKey2(String path) {
// 密鑰隨機數生成
SecureRandom sr = new SecureRandom();
// byte[] bytes = {11,12,44,99,76,45,1,8};
byte[] bytes = sr.generateSeed(20);
// 密鑰
SecretKey skey = null;
//生成密鑰文件路徑
File file = genFile(path);

try {
//創建deskeyspec對象
DESKeySpec desKeySpec = new DESKeySpec(bytes,9);
//實例化des密鑰工廠
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//生成密鑰對象
skey = keyFactory.generateSecret(desKeySpec);
//寫出密鑰對象
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeObject(skey);
oos.close();
Log.sKeyPath(path);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

private static File genFile(String path) {
String temp = null;
File newFile = null;
if (path.endsWith("/") || path.endsWith("\\")) {
temp = path;
} else {
temp = path + "/";
}

File pathFile = new File(temp);
if (!pathFile.exists())
pathFile.mkdirs();

newFile = new File(temp+SKEY_NAME);

return newFile;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
genKey2("E:/a/aa/");
}

}
/*加解密*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;
public class SecUtil {
public static void decrypt(String keyPath, String source, String dest) {
SecretKey key = null;
try

{
ObjectInputStream keyFile = new ObjectInputStream(
//讀取加密密鑰
new FileInputStream(keyPath));
key = (SecretKey) keyFile.readObject();
keyFile.close();
}
catch (FileNotFoundException ey1) {

throw new RuntimeException(ey1);
}
catch (Exception ey2) {
throw new RuntimeException(ey2);
}
//用key產生Cipher
Cipher cipher = null;
try {
//設置演算法,應該與加密時的設置一樣
cipher = Cipher.getInstance("DES");
//設置解密模式
cipher.init(Cipher.DECRYPT_MODE, key);
}
catch (Exception ey3) {
throw new RuntimeException(ey3);
}
//取得要解密的文件並解密
File file = new File(source);
String filename = file.getName();
try {
//輸出流,請注意文件名稱的獲取
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
//輸入流
CipherInputStream in = new CipherInputStream(new BufferedInputStream(
new FileInputStream(file)), cipher);
int thebyte = 0;
while ( (thebyte = in.read()) != -1) {
out.write(thebyte);
}
in.close();
out.close();
}
catch (Exception ey5) {
throw new RuntimeException(ey5);
}
}

public static void encrypt(String keyPath, String source, String dest) {
SecretKey key = null;
try

{
ObjectInputStream keyFile = new ObjectInputStream(
//讀取加密密鑰
new FileInputStream(keyPath));
key = (SecretKey) keyFile.readObject();
keyFile.close();
}
catch (FileNotFoundException ey1) {
throw new RuntimeException(ey1);
}
catch (Exception ey2) {
throw new RuntimeException(ey2);
}
//用key產生Cipher
Cipher cipher = null;
try {
//設置演算法,應該與加密時的設置一樣
cipher = Cipher.getInstance("DES");
//設置解密模式
cipher.init(Cipher.ENCRYPT_MODE, key);
}
catch (Exception ey3) {
throw new RuntimeException(ey3);
}
//取得要解密的文件並解密
File file = new File(source);
String filename = file.getName();
try {
//輸出流,請注意文件名稱的獲取
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
//輸入流
CipherInputStream in = new CipherInputStream(new BufferedInputStream(
new FileInputStream(file)), cipher);
int thebyte = 0;
while ( (thebyte = in.read()) != -1) {
out.write(thebyte);
}
in.close();
out.close();
}
catch (Exception ey5) {
throw new RuntimeException(ey5);
}
}
}

『叄』 java的 DES 加密解密方法 求對應C#的加密解密方法,急切

/*
* @param arrB 需要轉換的byte數組
* @return 轉換後的字元串
* @throws Exception 本方法不處理任何異常,所有異常全部拋出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每個byte用兩個字元才能表示,所以字元串的長度是數組長度的兩倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把負數轉換為正數
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小於0F的數需要在前面補0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}

/*
* @param strIn 需要轉換的字元串
* @return 轉換後的byte數組
* @throws Exception 本方法不處理任何異常,所有異常全部拋出
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// 兩個字元表示一個位元組,所以位元組數組長度是字元串長度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
/**
* 加密位元組數組
*
* @param arrB
* 需加密的位元組數組
* @return 加密後的位元組數組
* @throws Exception
*/
@SuppressWarnings("restriction")
private static byte[] encrypt(byte[] arrB,String keyParameter) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(keyParameter.getBytes());
Cipher encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
return encryptCipher.doFinal(arrB);
}

/**
* 加密字元串
*
* @param strIn
* 需加密的字元串
* @return 加密後的字元串
* @throws Exception
*/
public static String encrypt(String strIn,String keyParameter) throws Exception {
return HexStrByteArrUtils.byteArr2HexStr(encrypt(strIn.getBytes(PiccConfig.PICC_INPUT_CHARSET),keyParameter));
}

/**
* 解密位元組數組
*
* @param arrB
* 需解密的位元組數組
* @return 解密後的位元組數組
* @throws Exception
*/
@SuppressWarnings("restriction")
private static byte[] decrypt(byte[] arrB,String keyParameter) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(keyParameter.getBytes());
Cipher decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
return decryptCipher.doFinal(arrB);
}

/**
* 解密字元串
*
* @param strIn
* 需解密的字元串
* @return 解密後的字元串
* @throws Exception
*/
public static String decrypt(String strIn,String keyParameter) throws Exception {
return new String(decrypt(HexStrByteArrUtils.hexStr2ByteArr(strIn),keyParameter),PiccConfig.PICC_INPUT_CHARSET);
}

『肆』 JAVA和.NET使用DES對稱加密的區別

JAVA和.NET的系統類庫里都有封裝DES對稱加密的實現方式,但是對外暴露的介面卻各不相同,甚至有時會讓自己難以解決其中的問題,比如JAVA加密後的結果在.NET中解密不出來等,由於最近項目有跨JAVA和.NET的加解密,經過我的分析調試,終於讓它們可以互相加密解密了。

DES加密
DES是一種對稱加密(Data Encryption Standard)演算法,以前我寫過一篇文章:.NET中加密解密相關知識,有過簡單描述。
DES演算法一般有兩個關鍵點,第一個是加密演算法,第二個是數據補位。

加密演算法常見的有ECB模式和CBC模式:
ECB模式:電子密本方式,這是JAVA封裝的DES演算法的默認模式,就是將數據按照8個位元組一段進行DES加密或解密得到一段8個位元組的密文或者明文,最後一段不足8個位元組,則補足8個位元組(注意:這里就涉及到數據補位了)進行計算,之後按照順序將計算所得的數據連在一起即可,各段數據之間互不影響。
CBC模式:密文分組鏈接方式,這是.NET封裝的DES演算法的默認模式,它比較麻煩,加密步驟如下:
1、首先將數據按照8個位元組一組進行分組得到D1D2......Dn(若數據不是8的整數倍,就涉及到數據補位了)
2、第一組數據D1與向量I異或後的結果進行DES加密得到第一組密文C1(注意:這里有向量I的說法,ECB模式下沒有使用向量I)
3、第二組數據D2與第一組的加密結果C1異或以後的結果進行DES加密,得到第二組密文C2
4、之後的數據以此類推,得到Cn
5、按順序連為C1C2C3......Cn即為加密結果。

數據補位一般有NoPadding和PKCS7Padding(JAVA中是PKCS5Padding)填充方式,PKCS7Padding和PKCS5Padding實際只是協議不一樣,根據相關資料說明:PKCS5Padding明確定義了加密塊是8位元組,PKCS7Padding加密快可以是1-255之間。但是封裝的DES演算法默認都是8位元組,所以可以認為他們一樣。數據補位實際是在數據不滿8位元組的倍數,才補充到8位元組的倍數的填充過程。
NoPadding填充方式:演算法本身不填充,比如.NET的padding提供了有None,Zeros方式,分別為不填充和填充0的方式。
PKCS7Padding(PKCS5Padding)填充方式:為.NET和JAVA的默認填充方式,對加密數據位元組長度對8取余為r,如r大於0,則補8-r個位元組,位元組為8-r的值;如果r等於0,則補8個位元組8。比如:
加密字元串為為AAA,則補位為AAA55555;加密字元串為BBBBBB,則補位為BBBBBB22;加密字元串為CCCCCCCC,則補位為CCCCCCCC88888888。

『伍』 DES加密演算法 java實現

c語言的源代碼,供參考:

http://hi..com/gaojinshan/blog/item/8b2710c4ece4b3ce39db49e9.html

『陸』 用java實現des加密和解密

一個用DES來加密、解密的類
http://www.javanb.com/java/1/17816.html

import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
* 字元串工具集合
* @author Liudong
*/
public class StringUtils {

private static final String PASSWORD_CRYPT_KEY = "__jDlog_";
private final static String DES = "DES";

/**
* 加密
* @param src 數據源
* @param key 密鑰,長度必須是8的倍數
* @return 返回加密後的數據
* @throws Exception
*/
public static byte[] encrypt(byte[] src, byte[] key)throws Exception {
//DES演算法要求有一個可信任的隨機數源
SecureRandom sr = new SecureRandom();
// 從原始密匙數據創建DESKeySpec對象
DESKeySpec dks = new DESKeySpec(key);
// 創建一個密匙工廠,然後用它把DESKeySpec轉換成
// 一個SecretKey對象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher對象實際完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密匙初始化Cipher對象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
// 現在,獲取數據並加密
// 正式執行加密操作
return cipher.doFinal(src);
}

/**
* 解密
* @param src 數據源
* @param key 密鑰,長度必須是8的倍數
* @return 返回解密後的原始數據
* @throws Exception
*/
public static byte[] decrypt(byte[] src, byte[] key)throws Exception {
// DES演算法要求有一個可信任的隨機數源
SecureRandom sr = new SecureRandom();
// 從原始密匙數據創建一個DESKeySpec對象
DESKeySpec dks = new DESKeySpec(key);
// 創建一個密匙工廠,然後用它把DESKeySpec對象轉換成
// 一個SecretKey對象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher對象實際完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密匙初始化Cipher對象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
// 現在,獲取數據並解密
// 正式執行解密操作
return cipher.doFinal(src);
}
/**
* 密碼解密
* @param data
* @return
* @throws Exception
*/
public final static String decrypt(String data){
try {
return new String(decrypt(hex2byte(data.getBytes()),
PASSWORD_CRYPT_KEY.getBytes()));
}catch(Exception e) {
}
return null;
}
/**
* 密碼加密
* @param password
* @return
* @throws Exception
*/
public final static String encrypt(String password){
try {
return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes())); }catch(Exception e) {
}
return null;
}

比較長, 轉了一部分.

『柒』 JAVA和.NET使用DES對稱加密的區別

DES加密演算法原理:DES 使用一個 56 位的密鑰以及附加的 8 位奇偶校驗位(每組的第8位作為奇偶校驗位),產生最大 64 位的分組大小。這是一個迭代的分組密碼,使用稱為 Feistel 的技術,其中將加密的文本塊分成兩半。使用子密鑰對其中一半應用循環功能,然後將輸出與另一半進行「異或」運算;接著交換這兩半,這一過程會繼續下去,但最後一個循環不交換。DES 使用 16 輪循環,使用異或,置換,代換,移位操作四種基本運算。
.NET中的DES加密:
對於.NET,框架在System.Security.Cryptography命名空間下提供了DESCryptoServiceProvider作為System.Security.Cryptography.DES加密解密的包裝介面,它提供了如下的4個方法:

public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)

public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)

public override void GenerateIV()

public override void GenerateKey()
從.NET類庫封裝情況,加解密需要傳入一個Key和IV向量。而且Key必須為8位元組的數據,否則會直接拋異常出來,當使用ECB模式下,不管傳入什麼IV向量,加密結果都一樣
JAVA中的DES加密:
JAVA的javax.crypto.Cipher包下,提供了加密解密的功能,它的靜態getInstance方法,可以返回一個Cipher對象,一般有public static final Cipher getInstance(String transformation)方法,transformation為:algorithm/mode/padding,分別表示演算法名稱,比如DES,也可以在後麵包含演算法模式和填充方式,但也可以只是演算法名稱,如為:"DES/CBC/PKCS5Padding","DES"等。JAVA中默認的演算法為ECB,默認填充方式為PKCS5Padding。Cipher的Init方法用來初始化加密對象,常見的有:

public final void init(int opmode, Key key, AlgorithmParameterSpec params) ,
public final void init(int opmode,Key key, SecureRandom random),用SecureRandom時,一般用於不需要IV的演算法模式
總結

對於.NET和JAVA在使用DES對稱加密時,需要大家指定一樣的演算法和填充模式,並且JAVA在寫DES加解密演算法時,還需要根據創建Cipher對象的不同,正確使用IV向量。在不同系統需要互相數據時,必須要明確的是加密演算法,Key和演算法模式,再根據不同模式是否需要IV向量,最後是填充模式。

『捌』 java des加密,密鑰的長度是多少

3des演算法是指使用雙長度(16位元組)密鑰k=(kl||kr)將8位元組明文數據塊進行3次des加密/解密。如下所示:
y
=
des(kl)[des-1(kr)[des(kl[x])]]
解密方式為:
x
=
des-1
(kl)[des
(kr)[
des-1
(kl[y])]]
其中,des(kl[x])表示用密鑰k對數據x進行des加密,des-1
(kl[y])表示用密鑰k對數據y進行解密。
sessionkey的計算採用3des演算法,計算出單倍長度的密鑰。表示法為:sk
=
session(dk,data)
3des加密演算法為:
void
3des(byte
doublekeystr[16],
byte
data[8],
byte
out[8])
{
byte
buf1[8],
buf2[8];
des
(&doublekeystr[0],
data,
buf1);
udes(&doublekeystr[8],
buf1,
buf2);
des
(&doublekeystr[0],
buf2,
out);
}

『玖』 求用JAVA實現的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 analyzePath(){
String dirName;
int pos=srcFileName.lastIndexOf("/");
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=destFileName.lastIndexOf("/");
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 = 0; i<4; i++) {
outBuf.putChar(destStr.charAt(i));
}
outBuf.flip();
}else{
outBuf.position(0);
outBuf.limit(2*destStr.length());
for (int i = 0; i<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;
analyzePath();
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實現des演算法

分類: 電腦/網路 >> 程序設計 >> 其他編程語言
問題描述:

各位好,請求各位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 = 0; i<4; i++) {

outBuf.putChar(destStr.charAt(i));

}

outBuf.flip();

}else{

outBuf.position(0);

outBuf.limit(2*destStr.length());

for (int i = 0; i<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);

}

熱點內容
美國雲伺服器快還是香港快 發布:2025-02-09 09:34:33 瀏覽:988
怎麼解壓qq文件 發布:2025-02-09 09:18:14 瀏覽:581
安卓最新怎麼調靈敏度更穩 發布:2025-02-09 09:12:44 瀏覽:400
豌豆莢如何用安卓手機下載 發布:2025-02-09 09:11:57 瀏覽:213
吃雞腳本輔助 發布:2025-02-09 09:09:29 瀏覽:6
sessionidpython 發布:2025-02-09 09:08:53 瀏覽:276
華為手機驗證碼和密碼忘了是多少 發布:2025-02-09 08:53:53 瀏覽:799
逆戰筆記哪個配置好玩 發布:2025-02-09 08:53:04 瀏覽:600
怎麼打開電腦雲伺服器 發布:2025-02-09 08:36:01 瀏覽:220
日元對人民幣演算法 發布:2025-02-09 08:35:52 瀏覽:40