des加密算法的那些矩阵是怎么出来的?是怎么规定的每次迭代用的矩阵好像都不一样,有什么规律吗?
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/09 02:43:23
![des加密算法的那些矩阵是怎么出来的?是怎么规定的每次迭代用的矩阵好像都不一样,有什么规律吗?](/uploads/image/z/13997367-63-7.jpg?t=des%E5%8A%A0%E5%AF%86%E7%AE%97%E6%B3%95%E7%9A%84%E9%82%A3%E4%BA%9B%E7%9F%A9%E9%98%B5%E6%98%AF%E6%80%8E%E4%B9%88%E5%87%BA%E6%9D%A5%E7%9A%84%3F%E6%98%AF%E6%80%8E%E4%B9%88%E8%A7%84%E5%AE%9A%E7%9A%84%E6%AF%8F%E6%AC%A1%E8%BF%AD%E4%BB%A3%E7%94%A8%E7%9A%84%E7%9F%A9%E9%98%B5%E5%A5%BD%E5%83%8F%E9%83%BD%E4%B8%8D%E4%B8%80%E6%A0%B7%2C%E6%9C%89%E4%BB%80%E4%B9%88%E8%A7%84%E5%BE%8B%E5%90%97%3F)
des加密算法的那些矩阵是怎么出来的?是怎么规定的每次迭代用的矩阵好像都不一样,有什么规律吗?
des加密算法的那些矩阵是怎么出来的?是怎么规定的
每次迭代用的矩阵好像都不一样,有什么规律吗?
des加密算法的那些矩阵是怎么出来的?是怎么规定的每次迭代用的矩阵好像都不一样,有什么规律吗?
// 密钥随机数生成
//SecureRandom sr = new SecureRandom();
// byte[] bytes = {11,12,44,99,76,45,1,8};
//byte[] bytes = sr.generateSeed(20);
/*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/");
}
}