`
shuai1234
  • 浏览: 963431 次
  • 性别: Icon_minigender_1
  • 来自: 山西
社区版块
存档分类
最新评论

DESede加密解密程序(java)

    博客分类:
  • java
 
阅读更多

import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;

public class desede {
     public static void main(String args[]) throws Exception {
       if (args[0].compareTo("+") == 0)// 控制是加密还是减密
       {
         KeyGenerator kg;// 生产密锁的工厂
         FileInputStream fin;// 明文所在的文件
         FileOutputStream fout;// 密文所在的文件
         kg = KeyGenerator.getInstance("DESede");// 产生DESede算的密锁
         fin = new FileInputStream(args[1]);// 打开文件
         fout = new FileOutputStream(args[1] + ".DESede");// 写密文文件
         kg.init(168);// 初始化密锁
         SecretKey k = kg.generateKey();// 得到密锁
         Cipher cp = Cipher.getInstance("DESede");// 生成加密工厂对象,DESede为加密算法
         cp.init(Cipher.ENCRYPT_MODE, k);// 初始化加密器,Cipher.ENCRYPT_MODE指定加密,k为密锁
         File f = new File(args[1]);// 得到明文大小
         int num = (int) f.length();
         byte[] rb = new byte[num];
         fin.read(rb);// 读取明文
         byte[] cb = cp.doFinal(rb);// 加密,将加密结果放在cb字节数组中
         fout.write(cb);// 写入文件
         fin.close();
         fout.close();
         fout = new FileOutputStream("key.DESede");// 保存密锁
         fout.write(k.getEncoded());
         fout.close();
         System.out.print("\r\n\r\n");
         System.out.print("加密文件" + args[1] + "完成\r\n");
         System.out.print("密锁文件key.DESede\r\n");
         return;
       }
       if (args[0].compareTo("-") == 0)// 减密时
       {
         FileInputStream fin;// 用于打开密锁文件和密明文件
         FileOutputStream fout;// 写明文文件
         fin = new FileInputStream("key.DESede");
         File f = new File("key.DESede");
         int num = (int) f.length();
         byte[] kb = new byte[num];
         fin.read(kb);// 读到密锁
         fin.close();
         SecretKeySpec k = new SecretKeySpec(kb, "DESede");// 将密锁存在SecretKey对象中
         Cipher cp = Cipher.getInstance("DESede");// 生成解密工厂,DESede为算法
         cp.init(Cipher.DECRYPT_MODE, k);// 初始化解密,Cipher.DECRYPT_MODE为解密方式,k为密锁
         fout = new FileOutputStream(args[1]);
         fin = new FileInputStream(args[1] + ".DESede");
         f = new File(args[1] + ".DESede");
         num = (int) f.length();
         kb = new byte[num];
         fin.read(kb);// 读取密文
         byte[] m = cp.doFinal(kb);// 解密,将结果存在m字节数组中
         fout.write(m);// 写入文件
         fin.close();
         fout.close();
         return;
       } else// 如果不是有效参数,则出现帮助信息
       {
         System.out.print("\r\n\r\n");
         System.out.print("DESede算法加密解密:\r\n");
         System.out.print(" '-' :减号解密;\r\n");
         System.out.print(" filename:解密文件名;\r\n");
         System.out.print(" '+' :加号加密;\r\n");
         System.out.print(" filename:加密文件名;\r\n");
       }
     }//main
}//class
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import net.peoplesolution.wss.conf.SystemConfProp;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DESede
{

public DESede()
{
}

public static byte[] getKey(String keystr)
{
int l = keystr.length() / 2;
byte w[] = new byte[l];
for(int i = 0; i < l; i++)
w[i] = (byte)(Integer.parseInt(keystr.substring(i * 2, i * 2 + 2), 16) & 0xff);

return w;
}

private static byte[] EncryptionByteData(byte SourceData[], String sKey, String sIV)
throws Exception
{
byte retByte[] = (byte[])null;
byte EncryptionByte[] = getKey(sKey);
javax.crypto.SecretKey securekey = new SecretKeySpec(EncryptionByte, "DESede");
IvParameterSpec spec = new IvParameterSpec(getKey(sIV));
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(1, securekey, spec);
retByte = cipher.doFinal(SourceData);
return retByte;
}

private static byte[] DecryptionByteData(byte SourceData[], String sKey, String sIV)
throws Exception
{
byte retByte[] = (byte[])null;
byte EncryptionByte[] = getKey(sKey);
javax.crypto.SecretKey securekey = new SecretKeySpec(EncryptionByte, "DESede");
IvParameterSpec spec = new IvParameterSpec(getKey(sIV));
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(2, securekey, spec);
retByte = cipher.doFinal(SourceData);
return retByte;
}

public static String EncryptCode(String strTobeEnCrypted, String sKey, String sIV)
throws Exception
{
String retStr = null;
byte retByte[] = (byte[])null;
byte sorData[] = strTobeEnCrypted.getBytes("UTF-8");
retByte = EncryptionByteData(sorData, sKey, sIV);
BASE64Encoder be = new BASE64Encoder();
retStr = be.encode(retByte);
return retStr;
}

public static String DecryptCode(String strTobeDeCrypted, String sKey, String sIV)
throws Exception
{
if(strTobeDeCrypted.lastIndexOf(" ") > 0)
strTobeDeCrypted = strTobeDeCrypted.replaceAll(" ", "+");
String retStr = null;
byte retByte[] = (byte[])null;
BASE64Decoder bd = new BASE64Decoder();
byte sorData[] = bd.decodeBuffer(strTobeDeCrypted);
retByte = DecryptionByteData(sorData, sKey, sIV);
retStr = new String(retByte, "UTF-8");
return retStr;
}

public static String DecryptCode(String strTobeDeCrypted)
throws Exception
{
return DecryptCode(strTobeDeCrypted, getKey(), getIV());
}

public static String EncryptCode(String strTobeEnCrypted)
throws Exception
{
return EncryptCode(strTobeEnCrypted, getKey(), getIV());
}

private static String getKey()
{
if(Key == null || Key.equals(""))
Key = SystemConfProp.getInstance().getProperty("key");
return Key;
}

private static String getIV()
{
if(IV == null || IV.equals(""))
IV = SystemConfProp.getInstance().getProperty("iv");
return IV;
}

private static final String DESede = "DESede/CBC/PKCS5Padding";
private static String Key;
private static String IV;
}

 

分享到:
评论

相关推荐

    很强的Java加密解密算法源码.zip

    Java加密解密技术在软件开发中扮演着至关重要的角色,特别是在数据安全领域。3DES(Triple Data Encryption Standard)是一种常见的加密算法,它基于DES(Data Encryption Standard)并对其进行了加强,提高了安全性...

    认证码Token加密解密代码

    该代码使用Java语言编写,主要涉及到DESede加密算法、SHA-1哈希算法和Base64编码等技术。 一、DESede加密算法 DESede加密算法是一种三重DES加密算法,使用三个不同的密钥对数据进行加密和解密操作。该算法的key ...

    MD5算法,URLEncoding,Base64编码,AES,DES,DESede,RSA加密解密工具类和使用实例

    6. **DESede加密**:DESede,也称为3DES,是DES的一个加强版本,通过使用三个不同的56位密钥进行三次加密,提高安全性。这使得破解难度大大增加,但相对于AES,速度较慢。 7. **RSA加密**:RSA是非对称加密算法,它...

    3DES加密解密java版+js版

    在提供的文件`加密web.zip`和`java.zip`中,很可能包含了Java实现3DES加密解密的代码示例,以及可能用于Web端的JavaScript实现。这些代码可以帮助开发者理解如何在实际项目中跨平台地应用3DES加密技术。 3DES虽然...

    java 对称加解密 加密 解密

    Java作为一种广泛使用的编程语言,提供了丰富的库和工具来处理数据加密和解密。本篇文章将详细探讨Java中的对称加解密技术,特别是DESEDE(也称为3DES)算法。 对称加密是一种常见的加密方式,它的特点是加密和解密...

    实现使用3des在页面js加密,后台java解密

    本篇将详细介绍如何在网页前端使用JavaScript进行3DES加密,并在后端Java环境中进行解密。 一、3DES加密原理 3DES是DES的加强版,它使用了3个不同的56位密钥,通过3次独立的DES加密过程来提高安全性。具体流程如下...

    Java中对字符串进行加密和解密

    Java提供了强大的API支持,可以帮助开发者轻松实现加密解密功能。下面是一个使用DES算法实现的加密解密工具类的例子: ```java import java.io.*; import javax.crypto.*; import javax.crypto.spec.*; import java...

    Java使用Hutool实现AES、DES加密解密的方法

    Java 使用 Hutool 实现 AES、DES 加密解密的方法 Java 中使用 Hutool 实现 AES、DES 加密解密的方法主要介绍了如何使用 Hutool 库来实现 AES 和 DES 加密解密。Hutool 库对 Java 中的 Cipher 对象进行了封装,简化...

    3DES加密解密算法

    首先,为了使用3DES,Java程序需要引入特定的JAR包,这些包通常位于`JAVA_HOME/jre/lib/`目录下,包括`jce.jar`、`US_export_policy.jar`、`local_policy.jar`以及`ext/sunjce_provider.jar`。这些JAR文件包含了加密...

    JAVA实现3DES加密解密

    总的来说,通过Java实现3DES加密解密,我们需要理解加密算法的基本原理,熟悉Java的相关加密库,并掌握如何生成和使用密钥。在实际开发中,确保数据的安全性至关重要,因此正确地使用加密技术是每个IT专业人员必备的...

    JAVA 加密 解密 算法

    Java中的加密解密基础 在Java中,进行数据加密与解密主要依赖于Java Cryptography Extension (JCE)。JCE提供了强大的加密支持,包括对称加密、非对称加密以及消息摘要等功能。在实际应用中,我们通常会结合多种...

    C#加密用JAVA解密

    在IT领域,跨平台的数据安全通信是至...这些示例可以作为学习和参考的起点,帮助开发者理解跨平台加密解密的关键步骤和注意事项。在实际项目中,应根据具体需求和安全策略来调整这些示例,以确保数据的安全性和可靠性。

    FileEncrypter.rar_triple DES in java_triple des java_加密 解密

    本项目"FileEncrypter.rar"提供了Java实现的三重DES加密解密功能,允许用户对各种文件进行加解密操作,并且包含界面源码,使得用户交互更为直观易用。 首先,了解三重DES算法的基本原理。DES是一种使用56位密钥的...

    JAVA-加密解密(详细讲解-有案例-有理论)

    在Java中,可以使用`javax.crypto.Cipher`类来实现DESede加密和解密: ```java Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); SecretKey key = ...; // 创建密钥 cipher.init(Cipher.ENCRYPT_...

    3des加密解密

    在Java中实现3DES加密解密是一个常见的需求,尤其是在需要保护敏感数据的场景下。 本文将详细介绍如何在Java中实现3DES加密解密算法,并提供示例代码来帮助理解整个过程。 #### 二、基础知识 1. **DES**:DES是一...

    Java中的DES加密和解密

    ### Java中的DES加密和解密 #### 一、概述 数据加密标准(Data Encryption Standard,简称DES)是一种广泛使用的对称加密算法。它最初由IBM公司开发,并在1970年代中期被美国国家标准局(现为国家标准与技术研究院...

    PHP实现的DES加密解密封装类完整实例

    本文实例讲述了PHP实现的DES加密解密封装类。分享给大家供大家参考,具体如下: &lt;?php /** * PHP版DES加解密类 * 可与java的DES(DESede/CBC/PKCS5Padding)加密方式兼容 * */ class CryptDes { var $key; var $...

Global site tag (gtag.js) - Google Analytics