资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

des加密代码java 加密解密 java

java des 加密 解密 密钥随机取得方法

java DES 加密 解密 生成随机密钥

成都创新互联是一家集网站建设,儋州企业网站建设,儋州品牌网站建设,网站定制,儋州网站建设报价,网络营销,网络优化,儋州网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

举例说明:

//保存生成的key

public static void saveDesKey() {

try {

SecureRandom sr = new SecureRandom();

// 选择的DES算法生成一个KeyGenerator对象

KeyGenerator kg = KeyGenerator.getInstance("DES");

kg.init(sr);

// 相对路径 需要新建 conf 文件夹

// String fileName = "conf/DesKey.xml";

// 绝对路径

String fileName = "d:/DesKey.xml";

FileOutputStream fos = new FileOutputStream(fileName);

ObjectOutputStream oos = new ObjectOutputStream(fos);

// 生成密钥

Key key = kg.generateKey();

oos.writeObject(key);

oos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

//获取生成的key

public static Key getKey() {

Key kp = null;

try {

// 相对路径 需要新建 conf 文件夹

// String fileName = "conf/DesKey.xml";

// InputStream is = Encrypt.class.getClassLoader().getResourceAsStream(fileName);

// 绝对路径

String fileName = "d:/DesKey.xml";

FileInputStream is = new FileInputStream(fileName);

ObjectInputStream oos = new ObjectInputStream(is);

kp = (Key) oos.readObject();

oos.close();

} catch (Exception e) {

e.printStackTrace();

}

return kp;

}

//加密开始

public static void encrypt(String file, String dest) throws Exception {

Cipher cipher = Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE, getKey());

InputStream is = new FileInputStream(file);

OutputStream out = new FileOutputStream(dest);

CipherInputStream cis = new CipherInputStream(is, cipher);

byte[] buffer = new byte[1024];

int r;

while ((r = cis.read(buffer)) 0) {

out.write(buffer, 0, r);

}

cis.close();

is.close();

out.close();

}

//解密开始

public static void decrypt(String file, String dest) throws Exception {

Cipher cipher = Cipher.getInstance("DES");

cipher.init(Cipher.DECRYPT_MODE, getKey());

InputStream is = new FileInputStream(file);

OutputStream out = new FileOutputStream(dest);

CipherOutputStream cos = new CipherOutputStream(out, cipher);

byte[] buffer = new byte[1024];

int r;

while ((r = is.read(buffer)) = 0) {

cos.write(buffer, 0, r);

}

cos.close();

out.close();

is.close();

}

}

//des加密主方法

public class DES {

public static void main(String[] args) throws Exception {

Encrypt.saveDesKey();

System.out.println("生成key");

Encrypt.getKey();

System.out.println("获取key");

Encrypt.encrypt("d:\\hello.txt", "d:\\encrypt.txt");

System.out.println("加密");

Encrypt.decrypt("d:\\encrypt.txt", "d:\\decrypt.txt");

System.out.println("解密");

}

以上方法亲测可用。

用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()+(byteLeft8 ? (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; i4; i++) {

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

}

outBuf.flip();

}else{

outBuf.position(0);

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

for (int i = 0; idestStr.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实现对字符串的DES加密和解密

/**

* ECB模式的des加密,以base64的编码输出

* @param message

* @param key

* @return

* @throws Exception

*/

public static String desEncrypt(String message, String key)throws Exception{

// DES/ECB CBC CFB OFB /PKCS5Padding  NoPadding  加密/模式/填充

Cipher cipher = Cipher.getInstance("DES");//默认就是 DES/ECB/PKCS5Padding

DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

cipher.init(1, secretKey);

return new BASE64Encoder().encode(cipher.doFinal(message.getBytes("UTF-8")));

}

/**

* ECB模式的des解密

* @param message

* @param key

* @return

* @throws Exception

*/

public static String desDecrypt(String message, String key)throws Exception{

Cipher cipher = Cipher.getInstance("DES");

DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

cipher.init(2, secretKey);

return new String(cipher.doFinal(Base64.decode(message)), "UTF-8");

}

你自己写main方法测试一下,应该是没问题的

求高手给出以下JAVA代码的DES加密解密方法的对应的C#的DES加密解密方法

Java密码学结构设计遵循两个原则:

1) 算法的独立性和可靠性。

2) 实现的独立性和相互作用性。

算法的独立性是通过定义密码服务类来获得。用户只需了解密码算法的概念,而不用去关心如何实现这些概念。实现的独立性和相互作用性通过密码服务提供器来实现。密码服务提供器是实现一个或多个密码服务的一个或多个程序包。软件开发商根据一定接口,将各种算法实现后,打包成一个提供器,用户可以安装不同的提供器。安装和配置提供器,可将包含提供器的ZIP和JAR文件放在CLASSPATH下,再编辑Java安全属性文件来设置定义一个提供器。

DES算法及如何利用DES算法加密和解密类文件的步骤:

DES算法简介

DES(Data Encryption Standard)是发明最早的最广泛使用的分组对称加密算法。DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。


分享文章:des加密代码java 加密解密 java
文章出自:http://cdkjz.cn/article/dopoepi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220