怎么用C#解密 Java写的3des加密 ~~~~密钥是48位的。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么用C#解密 Java写的3des加密 ~~~~密钥是48位的。相关的知识,希望对你有一定的参考价值。
参考技术A 3DES的密钥不可能48bit吧,那安全性比普通DES更差。
密钥高于56bit
用相同的块操作模式CBC、ECB等
用相同的补齐方式,PKCS5/7
密钥用相同的
散列函数
或扩张函数。比如
md5
,
sha1
各方面一致,c#,java#可以互转无误的。
java des 加密 解密 密钥随机取得方法
请大侠们详细说明,希望带上详细代码,包括先从密钥随机开始
lzyaisr00163 能否讲讲DesKey.xml怎么搞
举例说明:
//保存生成的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("解密");
以上方法亲测可用。 参考技术A 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("解密");
以上方法亲测可用。 参考技术B package com.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
class Encrypt
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();
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();
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("解密");
以上是关于怎么用C#解密 Java写的3des加密 ~~~~密钥是48位的。的主要内容,如果未能解决你的问题,请参考以下文章