java实现Des对TXT文件内容的加解密

合集下载

如何在Java中进行数据的加密和解密

如何在Java中进行数据的加密和解密

如何在Java中进行数据的加密和解密在Java中,有多种加密和解密的算法可供选择,如对称加密算法、非对称加密算法和哈希算法等。

下面将介绍几种常见的加密和解密方法。

1.对称加密算法对称加密算法指的是使用相同密钥进行加密和解密的算法,常见的对称加密算法有DES、3DES、AES等。

DES加密算法是一种对称密钥算法,加密解密使用相同的密钥,密钥长度为56位。

以下是使用DES加密和解密的示例代码:```javaimport javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;public class DESExample {public static void main(String[] args) throws Exception { String plainText = "Hello World";String key = "abcdefgh"; //密钥必须是8位长度byte[] encryptedBytes = desEncrypt(plainText.getBytes(), key.getBytes());byte[] decryptedBytes = desDecrypt(encryptedBytes,key.getBytes());String decryptedText = new String(decryptedBytes);System.out.println("解密后的文本:" + decryptedText);}public static byte[] desEncrypt(byte[] plainText, byte[] keyBytes) throws Exception {SecretKey key = new SecretKeySpec(keyBytes, "DES");Cipher cipher = Cipher.getInstance("DES");cipher.init(Cipher.ENCRYPT_MODE, key);return cipher.doFinal(plainText);}public static byte[] desDecrypt(byte[] cipherText, byte[] keyBytes) throws Exception {SecretKey key = new SecretKeySpec(keyBytes, "DES");Cipher cipher = Cipher.getInstance("DES");cipher.init(Cipher.DECRYPT_MODE, key);return cipher.doFinal(cipherText);}}```3DES加密算法是DES的一种改进版,使用3个不同的密钥对数据进行3次加密。

实验二 DES加解密算法的实现

实验二 DES加解密算法的实现

实验二 DES加解密算法的实现实验二:DES的编程实现一、实验目的在本实验中,用VC/ C/C++/Java实现DES的加解密算法。

完成实验后将能够深入理解DES加解密算法及其在VC/ C/C++/Java中的实现过程。

二、实验条件熟悉VC/ C/C++/Java开发环境和有关DES算法的有关知识;一台安装有VC/ C/C++/Java的计算机。

三、实验要求本实验要求:输入十六位十六进制明文(占64Bit)和十六位十六进制密钥(占64Bit),输出十六位十六进制密文和六十四位二进制密文。

四、实验步骤对数据进行加密传输能有效地保证数据的机密性,DES是一个保证数据机密性的经典算法,本实验拟在VC/ C/C++/Java环境中实现DES算法。

以下以VC为例:(3)“Win32 Console Application-步骤1共1步”对话框中,选择“一个空工程”单选按钮,单击“完成”按钮,再单击“确定”按钮。

图2 “Win32 Console Application-步骤1共1步”对话框(4)在VC环境中,在其左侧的工程工作区窗口下端中选择标签“FileView”,单击“DES files”前面的“+”号展开。

图3 标签“FileView”(5)鼠标右击“Source Files”,选择“添加文件到目录”,将testMain.cpp和zlDES.cpp添加进“Source Files”。

同样,鼠标右击“Header Files”,选择“添加文件到目录”,将zlDES.h 添加进“Header Files”。

结果,如图4。

图4 标签“FileView”(6)选择“组建|编译”,分别编译testMain.cpp和zlDES.cpp。

然后再选择“组建|执行”,即可看到结果。

五、编写源代码(1)DES算法详述(主要描述算法流程)DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位,整个算法的主流程图如下:其中每一轮轮结构为:(2)可以参照如下VC++代码。

Java中的数据加密与解密

Java中的数据加密与解密

Java中的数据加密与解密数据加密与解密在现代信息安全中起着至关重要的作用。

Java作为一种广泛应用的编程语言,提供了丰富的工具和库来实现数据的加密和解密。

本文将介绍Java中常用的数据加密与解密的方法,包括对称加密和非对称加密。

一、对称加密对称加密是指使用相同的密钥进行数据的加密和解密。

Java中常用的对称加密算法有DES、AES等。

下面分别介绍这两种算法的使用方法。

1. DES加密与解密DES(Data Encryption Standard)是一种对称加密算法,使用相同的密钥进行加密和解密。

在Java中,可以使用javax.crypto包下的Cipher 类来实现DES加密和解密的功能。

具体代码如下:```javaimport javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;public class DESUtils {private static final String ALGORITHM = "DES";public static String encrypt(String data, String key) throws Exception {SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), ALGORITHM);Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, keySpec);byte[] encryptedData = cipher.doFinal(data.getBytes());return new String(encryptedData);}public static String decrypt(String encryptedData, String key) throws Exception {SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), ALGORITHM);Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, keySpec);byte[] decryptedData = cipher.doFinal(encryptedData.getBytes());return new String(decryptedData);}}```2. AES加密与解密AES(Advanced Encryption Standard)是一种高级的对称加密算法,相较于DES更为安全、高效。

DES加密和解密java代码

DES加密和解密java代码

DES加密和解密java代码在说DES加密算法之前,我们⾸先了解⼏个基本概念:1. 明⽂:明⽂是指没有经过加密的数据。

⼀般⽽⾔,明⽂都是等待传输的数据。

由于没有经过加密,明⽂很容易被识别与破解,因此在传输明⽂之前必须进⾏加密处理。

2. 密⽂:密⽂只是明⽂经过某种加密算法⽽得到的数据,通常密⽂的形式复杂难以识别及理解。

3. 密钥:密钥是⼀种参数,它是在明⽂转换为密⽂或将密⽂转换为明⽂的算法中输⼊的参数。

4. 对称加密:通信双⽅同时掌握⼀个密钥,加密解密都是由⼀个密钥完成的(即加密密钥等于解密密钥,加解密密钥可以相互推倒出来)。

双⽅通信前共同拟定⼀个密钥,不对第三⽅公开。

5. 分组加密:分组密码是将明⽂分成固定长度的组,每⼀组都采⽤同⼀密钥和算法进⾏加密,输出也是固定长度的密⽂。

好了了解这些以后,我们再来说DES加密算法。

DES 算法是⼀种常见的分组加密算法,由IBM公司在1971年提出。

DES 算法是分组加密算法的典型代表,同时也是应⽤最为⼴泛的对称加密算法。

下⾯我们详细介绍下:1.分组长度:DES 加密算法中,明⽂和密⽂为 64 位分组。

密钥的长度为 64 位,但是密钥的每个第⼋位设置为奇偶校验位,因此密钥的实际长度为56位。

2.加密流程:DES 加密算法⼤致分为 4 步:初始置换、⽣成⼦密钥、迭代过程、逆置换。

代码如下:需特别注意:不要使⽤JDK中⾃带的sun.misc.BASE64Decoder类去做BASE64,该类会在最后⾯多加换⾏。

⽽应使⽤apache中的mons.codec.binary.Base64这个类来做base64加密。

(待验证)import javax.crypto.Cipher;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.IvParameterSpec;import java.security.Key;import java.security.SecureRandom;import mons.codec.binary.Base64;public class DESUtil {// 偏移变量,固定占8位字节private final static String IV_PARAMETER = "12345678";// 字符编码public static final String CHARSET_UTF8 = "UTF-8";// 加密算法DESpublic static final String DES = "DES";// 电话本模式public static final String DES_ECB = "DES/ECB/PKCS5Padding";// 加密块链模式--推荐public static final String DES_CBC = "DES/CBC/PKCS5Padding";// 测试public static void main(String args[]) {// 待加密内容String str = "false";// 密码,长度要是8的倍数String password = "87654321";String result = DESUtil.encryptCBC(password, str);System.out.println("加密后:" + new String(result));// 直接将如上内容解密try {String decryResult = DESUtil.decryptCBC(password, result);System.out.println("解密后:" + new String(decryResult));} catch (Exception e1) {e1.printStackTrace();}}/*** ⽣成key** @param password* @return* @throws Exception*/private static Key generateKey(String password) throws Exception {DESKeySpec dks = new DESKeySpec(password.getBytes(CHARSET_UTF8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);return keyFactory.generateSecret(dks);}/*** DES加密字符串--ECB格式** @param password* 加密密码,长度不能够⼩于8位* @param data* 待加密字符串* @return加密后内容*/public static String encryptECB(String password, String data) {if (password == null || password.length() < 8) {throw new RuntimeException("加密失败,key不能⼩于8位");}if (data == null) {return null;}try {Key secretKey = generateKey(password);Cipher cipher = Cipher.getInstance(DES_ECB);cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());byte[] bytes = cipher.doFinal(data.getBytes(CHARSET_UTF8));// JDK1.8及以上可直接使⽤Base64,JDK1.7及以下可以使⽤BASE64Encoder return new String(Base64.encodeBase64(bytes));} catch (Exception e) {e.printStackTrace();return data;}}/*** DES解密字符串--ECB格式** @param password* 解密密码,长度不能够⼩于8位* @param data* 待解密字符串* @return解密后内容*/public static String decryptECB(String password, String data)throws Exception {Key secretKey = generateKey(password);Cipher cipher = Cipher.getInstance(DES_ECB);cipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom());return new String(cipher.doFinal(Base64.decodeBase64(data.getBytes(CHARSET_UTF8))), CHARSET_UTF8);}/*** DES加密字符串-CBC加密格式** @param password* 加密密码,长度不能够⼩于8位* @param data* 待加密字符串* @return加密后内容*/public static String encryptCBC(String password, String data) {if (password == null || password.length() < 8) {throw new RuntimeException("加密失败,key不能⼩于8位");}if (data == null) {return null;}try {Key secretKey = generateKey(password);Cipher cipher = Cipher.getInstance(DES_CBC);IvParameterSpec spec = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET_UTF8));cipher.init(Cipher.ENCRYPT_MODE, secretKey, spec);byte[] bytes = cipher.doFinal(data.getBytes(CHARSET_UTF8));// JDK1.8及以上可直接使⽤Base64,JDK1.7及以下可以使⽤BASE64Encoder return new String(Base64.encodeBase64(bytes));} catch (Exception e) {e.printStackTrace();return data;}}/*** DES解密字符串--CBC格式** @param password* 解密密码,长度不能够⼩于8位* @param data* 待解密字符串* @return解密后内容*/public static String decryptCBC(String password, String data) throws Exception {Key secretKey = generateKey(password);Cipher cipher = Cipher.getInstance(DES_CBC);IvParameterSpec spec = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET_UTF8));cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);return new String(cipher.doFinal(Base64.decodeBase64(data.getBytes(CHARSET_UTF8))), CHARSET_UTF8); }}。

java实现文件内容的加密和解密

java实现文件内容的加密和解密

java实现⽂件内容的加密和解密package com.umapp.test;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;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;public class TestDES {Key key;public TestDES(String str) {getKey(str);//⽣成密匙}/*** 根据参数⽣成KEY*/public void getKey(String strKey) {try {KeyGenerator _generator = KeyGenerator.getInstance("DES");_generator.init(new SecureRandom(strKey.getBytes()));this.key = _generator.generateKey();_generator = null;} catch (Exception e) {throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);}}/*** ⽂件file进⾏加密并保存⽬标⽂件destFile中** @param file 要加密的⽂件如c:/test/srcFile.txt* @param destFile 加密后存放的⽂件名如c:/加密后⽂件.txt*/public void encrypt(String file, String destFile) throws Exception {Cipher cipher = Cipher.getInstance("DES");// cipher.init(Cipher.ENCRYPT_MODE, getKey());cipher.init(Cipher.ENCRYPT_MODE, this.key);InputStream is = new FileInputStream(file);OutputStream out = new FileOutputStream(destFile);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();}/*** ⽂件采⽤DES算法解密⽂件** @param file 已加密的⽂件如c:/加密后⽂件.txt* * @param destFile* 解密后存放的⽂件名如c:/ test/解密后⽂件.txt*/public void decrypt(String file, String dest) throws Exception {Cipher cipher = Cipher.getInstance("DES");cipher.init(Cipher.DECRYPT_MODE, this.key);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) {System.out.println();cos.write(buffer, 0, r);}cos.close();out.close();is.close();}public static void main(String[] args) throws Exception {TestDES td = new TestDES("aaa");td.encrypt("e:/r.txt", "e:/r解密.txt"); //加密 td.decrypt("e:/r解密.txt", "e:/r1.txt"); //解密 }}。

DES加密解密Java代码

DES加密解密Java代码
mainMenu.add(generateItem);
mainMenu.addSeparator();
mainMenu.add(encryptItem);
mainMenu.add(decryptItem);
menuBar.add(mainMenu);
setJMenuBar(menuBar);
//添加文本区
if (fileName == null)
return;
//读取密文
FileInputStream fis = new FileInputStream(fileName);
byte[] cipherText = new byte[fis.available()];
fis.read(cipherText);
jfc.setDialogTitle("打开加密文本内容");
if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
fileName = jfc.getSelectedFile().getPath();
}
//如果没有选择文件,就退出
e1.printStackTrace();
}
} else if (e.getSource() == decryptItem) {
try {
//读取密钥文件的完整路径
String fileName = null;
//设置文件选择对话框
JFileChooser jfc = new JFileChooser();
add(jsp);
//添加事件监听
generateItem.addActionListener(this);

DES 加解密算法(java和c#版)

DES 加解密算法(java和c#版)

/** java 版的* <p>Title: DES 加解密算法</p>* <p>Description: DES 加解密算法</p>* <p>Copyright: Copyright (c) 2004</p>* <p>Company: Aspire Corp</p>* @author zhangji* @version 1.0*/import java.security.*;import javax.crypto.*;public class DES {private static String strDefaultKey = "hnzt";private Cipher encryptCipher = null;private Cipher decryptCipher = null;/*** 将byte数组转换为表示16进制值的字符串,* 如:byte[]{8,18}转换为:0813,* 和public static byte[] hexStr2ByteArr(String strIn)* 互为可逆的转换过程* @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的数需要在前面补0if (intTmp < 16){sb.append("0");}sb.append(Integer.toString(intTmp, 16));}return sb.toString();}/*** 将表示16进制值的字符串转换为byte数组,* 和public static String byteArr2HexStr(byte[] arrB)* 互为可逆的转换过程* @param strIn 需要转换的字符串* @return 转换后的byte数组* @throws Exception 本方法不处理任何异常,所有异常全部抛出* @author <a href="mailto:zhangji@">ZhangJi</a> */public static byte[] hexStr2ByteArr(String strIn)throws Exception{byte[] arrB = strIn.getBytes();int iLen = arrB.length;//两个字符表示一个字节,所以字节数组长度是字符串长度除以2byte[] 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;}/*** 默认构造方法,使用默认密钥* @throws Exception*/public DES()throws Exception{this(strDefaultKey);}/*** 指定密钥构造方法* @param strKey 指定的密钥* @throws Exception*/public DES(String strKey)throws Exception{Security.addProvider(new com.sun.crypto.provider.SunJCE()); Key key = getKey(strKey.getBytes());encryptCipher = Cipher.getInstance("DES");encryptCipher.init(Cipher.ENCRYPT_MODE, key);decryptCipher = Cipher.getInstance("DES");decryptCipher.init(Cipher.DECRYPT_MODE, key);}/*** 加密字节数组* @param arrB 需加密的字节数组* @return 加密后的字节数组* @throws Exception*/public byte[] encrypt(byte[] arrB)throws Exception{return encryptCipher.doFinal(arrB);}/*** 加密字符串* @param strIn 需加密的字符串* @return 加密后的字符串* @throws Exception*/public String encrypt(String strIn)throws Exception{return byteArr2HexStr(encrypt(strIn.getBytes())); }/*** 解密字节数组* @param arrB 需解密的字节数组* @return 解密后的字节数组* @throws Exception*/public byte[] decrypt(byte[] arrB)throws Exception{return decryptCipher.doFinal(arrB);}/*** 解密字符串* @param strIn 需解密的字符串* @return 解密后的字符串* @throws Exception*/public String decrypt(String strIn)throws Exception{return new String(decrypt(hexStr2ByteArr(strIn)));}/*** 从指定字符串生成密钥,密钥所需的字节数组长度为8位* 不足8位时后面补0,超出8位只取前8位* @param arrBTmp 构成该字符串的字节数组* @return 生成的密钥* @throws ng.Exception*/private Key getKey(byte[] arrBTmp)throws Exception{//创建一个空的8位字节数组(默认值为0)byte[] arrB = new byte[8];//将原始字节数组转换为8位for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {arrB[i] = arrBTmp[i];}//生成密钥Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); return key;}/*** 单元测试方法* @param args*/public static void main(String[] args){String strOriginal = "1111";String strOp = "-de";// 检查入参个数if (args.length == 2 ){strOp = args[0] ;strOriginal = args[1];}else{System.out.println("Wrong Parameter count , try use \"java DES -de|-en 'the string you want to be Encrypted'\"");System.out.println("Now do Encrypt with \"1111\"");try{DES des = new DES();// 加密测试System.out.println("***** 加密测试 *****") ;des.enTest("1111");// 解密测试System.out.println("***** 解密测试 *****") ;des.deTest("0fc7648b53e54cfb");}catch (Exception ex){ex.printStackTrace();}return ;}try{if ( strOp.equals("-de")) {DES des = new DES();des.deTest(strOriginal);}else if ( strOp.equals("-en")) {DES des = new DES();des.enTest(strOriginal);}else{System.out.println("Wrong operater , try use \"java DES -de|-en 'the string you want to be Encrypted'\"");System.out.println("Now do Encrypt with \"1111\""); }}catch (Exception ex){ex.printStackTrace();}}/*** 单元测试方法,打印对指定字符串加密后的字符串*/private void enTest(String strOriginal){try{System.out.println("Plain String: " + strOriginal);String strEncrypt= encrypt(strOriginal);System.out.println("Encrypted String: " + strEncrypt);}catch (Exception ex){ex.printStackTrace();}}/*** 单元测试方法,打印对指定字符串解密后的字符串*/private void deTest(String strOriginal){try{System.out.println("Encrypted String: " + strOriginal); System.out.println("Encrypted String length = " + strOriginal.length());String strPlain = decrypt(strOriginal);System.out.println("Plain String: " + strPlain);}catch (Exception ex){ex.printStackTrace();}}}===============c#版的================using System;using System.Text;using System.IO;using System.Security.Cryptography;class Class1{static void Main(){Console.WriteLine("Encrypt String...");txtKey = "tkGGRmBErvc=";btnKeyGen();Console.WriteLine("Encrypt Key :{0}",txtKey);txtIV = "Kl7ZgtM1dvQ=";btnIVGen();Console.WriteLine("Encrypt IV :{0}",txtIV);Console.WriteLine();string txtEncrypted = EncryptString("1111");Console.WriteLine("Encrypt String : {0}",txtEncrypted);string txtOriginal = DecryptString(txtEncrypted);Console.WriteLine("Decrypt String : {0}",txtOriginal);}private static SymmetricAlgorithm mCSP;private static string txtKey;private static string txtIV;private static void btnKeyGen(){mCSP = SetEnc();byte[] byt2 = Convert.FromBase64String(txtKey);mCSP.Key = byt2;}private static void btnIVGen(){byte[] byt2 = Convert.FromBase64String(txtIV);mCSP.IV = byt2;}private static string EncryptString(string Value){ICryptoTransform ct;MemoryStream ms;CryptoStream cs;byte[] byt;ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);byt = Encoding.UTF8.GetBytes(Value);ms = new MemoryStream();cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length);cs.FlushFinalBlock();cs.Close();return Convert.ToBase64String(ms.ToArray());}private static string DecryptString(string Value){ICryptoTransform ct;MemoryStream ms;CryptoStream cs;byte[] byt;ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);byt = Convert.FromBase64String(Value);ms = new MemoryStream();cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length);cs.FlushFinalBlock();cs.Close();return Encoding.UTF8.GetString(ms.ToArray());}private static SymmetricAlgorithm SetEnc() {return new DESCryptoServiceProvider(); }}。

JAVA加密解密DES对称加密算法

JAVA加密解密DES对称加密算法

JAVA加密解密DES对称加密算法1下⾯⽤DES对称加密算法(设定⼀个密钥,然后对所有的数据进⾏加密)来简单举个例⼦。

23⾸先,⽣成⼀个密钥KEY。

4我把它保存到key.txt中。

这个⽂件就象是⼀把钥匙。

谁拥有它,谁就能解开我们的类⽂件。

代码参考如下:5package com.neusoft.jiami;6import Java.io.File;7import java.io.FileOutputStream;8import java.security.SecureRandom;9import javax.crypto.KeyGenerator;10import javax.crypto.SecretKey;11class Key {12private String keyName;13public Key(String keyName) {14this.keyName = keyName;15 }16public void createKey(String keyName) throws Exception {17// 创建⼀个可信任的随机数源,DES算法需要18 SecureRandom sr = new SecureRandom();19// ⽤DES算法创建⼀个KeyGenerator对象20 KeyGenerator kg = KeyGenerator.getInstance("DES");21// 初始化此密钥⽣成器,使其具有确定的密钥长度22 kg.init(sr);23// ⽣成密匙24 SecretKey key = kg.generateKey();25// 获取密钥数据26byte rawKeyData[] = key.getEncoded();27// 将获取到密钥数据保存到⽂件中,待解密时使⽤28 FileOutputStream fo = new FileOutputStream(new File(keyName));29 fo.write(rawKeyData);30 }31public static void main(String args[]) {32try {33new Key("key.txt");34 } catch (Exception e) {35 e.printStackTrace();36 }37 }38 }39第⼆步,对我们所要进⾏加密的类⽂件进⾏加密。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 }; // 48 private static final int[] P = { 7, 16, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 9, 3, 19, 13, 30, 6, 22, 11, 4, 25 }; // 32 private static final int[][][] S_Box = { //S-盒 {// S_Box[1] { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 }, { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 }, { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 }, { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } }, { // S_Box[2] { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 }, { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 }, { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 }, { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } }, { // S_Box[3] { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 }, { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 }, { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 }, { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } }, { // S_Box[4] { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 }, { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 }, { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 }, { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } }, { // S_Box[5] { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 }, { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 }, { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 }, { 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } }, { // S_Box[6] { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 }, { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 }, { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 }, { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } }, { // S_Box[7]
// 初试化密钥数组 private void KeyInitialize(int[] key, int[][] keyarray) { int i; int j; int[] K0 = new int[56];// 特别注意:xxx[IP[i]-1]等类似变换 for (i = 0; i < 56; i++) { K0[i] = key[PC_1[i] - 1];// 密钥进行PC-1变换 } for (i = 0; i < 16; i++) { LeftBitMove(K0, LeftMove[i]); // 特别注意:xxx[IP[i]-1]等类似 变换 for (j = 0; j < 48; j++) { keyarray[i][j] = K0[PC_2[j] - 1]; // 生成子密钥keyarray[i][j] } } } // 执行加密解密操作,flag,为1加密,为0解密 private byte[] Encrypt(int[] timeData, int flag, int[][] keyarray) { int i; byte[] encrypt = new byte[8]; int flags = flag; int[] M = new int[64]; int[] MIP_1 = new int[64]; // 特别注意:xxx[IP[i]-1]等类似变换 for (i = 0; i < 64; i++) { M[i] = timeData[IP[i] - 1]; // 明文IP变换 } if (flags == 1) { // 加密 for (i = 0; i < 16; i++) { LoopF(M, i, flags, keyarray); } } else if (flags == 0) { // 解密 for (i = 15; i > -1; i--) { LoopF(M, i, flags, keyarray); } }
{ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 }, { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 }, { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 }, { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } }, { // S_Box[8] { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 }, { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 }, { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8Байду номын сангаас}, { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } } }; // 左移位置列表 private static final int[] LeftMove = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; byte[] bytekey; //密码的二进制数组 public SY_1_DesUtil(String strKey) { this.bytekey = strKey.getBytes(); } private byte[] UnitDes(byte[] des_key, byte[] des_data, int flag) { // 检测输入参数格式是否正确,错误直接返回空值(null) if ((des_key.length != 8) || (des_data.length != 8)|| ((flag != 1) && (flag != 0))) { throw new RuntimeException("Data Format Error !"); } int flags = flag;// 二进制加密密钥 int[] keydata = new int[64];// 二进制加密数据 int[] encryptdata = new int[64]; // 加密操作完成后的字节数组 byte[] EncryptCode = new byte[8];// 密钥初试化成二维数组 int[][] KeyArray = new int[16][48];// 将密钥字节数组转换成二进制 字节数组 keydata = ReadDataToBirnaryIntArray(des_key);// 将加密数据字节 数组转换成二进制字节数组 encryptdata = ReadDataToBirnaryIntArray(des_data);// 初试化密钥 为二维密钥数组 KeyInitialize(keydata, KeyArray); // 执行加密解密操作 EncryptCode = Encrypt(encryptdata, flags, KeyArray); return EncryptCode; }
public class SY_1_DesUtil { // 声明常量字节数组 private static final int[] IP = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 }; // 64 private static final int[] IP_1 = { 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 }; // 64 private static final int[] PC_1 = { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 }; // 56 private static final int[] PC_2 = { 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 }; // 48 private static final int[] E = { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9,
相关文档
最新文档