导航:首页 > 文档加密 > deskey加密

deskey加密

发布时间:2022-01-15 20:24:17

java 给定十六位密钥 如何进行des加密

packagecom.palic.pss.afcs.worldthrough.common.util;

importjavax.crypto.Cipher;
importjavax.crypto.spec.SecretKeySpec;

importrepack.com.thoughtworks.xstream.core.util.Base64Encoder;
/**
*AES加密解密
*@authorEX-CHENQI004
*
*/
publicclassAesUtils{
publicstaticfinalStringcKey="assistant7654321";
/**
*加密--把加密后的byte数组先进行二进制转16进制在进行base64编码
*@paramsSrc
*@paramsKey
*@return
*@throwsException
*/
publicstaticStringencrypt(StringsSrc,StringsKey)throwsException{
if(sKey==null){
("ArgumentsKeyisnull.");
}
if(sKey.length()!=16){
(
"ArgumentsKey'lengthisnot16.");
}
byte[]raw=sKey.getBytes("ASCII");
SecretKeySpecskeySpec=newSecretKeySpec(raw,"AES");

Ciphercipher=Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,skeySpec);

byte[]encrypted=cipher.doFinal(sSrc.getBytes("UTF-8"));
StringtempStr=parseByte2HexStr(encrypted);

Base64Encoderencoder=newBase64Encoder();
returnencoder.encode(tempStr.getBytes("UTF-8"));
}

/**
*解密--先进行base64解码,在进行16进制转为2进制然后再解码
*@paramsSrc
*@paramsKey
*@return
*@throwsException
*/
publicstaticStringdecrypt(StringsSrc,StringsKey)throwsException{

if(sKey==null){
("499");
}
if(sKey.length()!=16){
("498");
}

byte[]raw=sKey.getBytes("ASCII");
SecretKeySpecskeySpec=newSecretKeySpec(raw,"AES");

Ciphercipher=Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE,skeySpec);

Base64Encoderencoder=newBase64Encoder();
byte[]encrypted1=encoder.decode(sSrc);

StringtempStr=newString(encrypted1,"utf-8");
encrypted1=parseHexStr2Byte(tempStr);
byte[]original=cipher.doFinal(encrypted1);
StringoriginalString=newString(original,"utf-8");
returnoriginalString;
}

/**
*将二进制转换成16进制
*
*@parambuf
*@return
*/
(bytebuf[]){
StringBuffersb=newStringBuffer();
for(inti=0;i<buf.length;i++){
Stringhex=Integer.toHexString(buf[i]&0xFF);
if(hex.length()==1){
hex='0'+hex;
}
sb.append(hex.toUpperCase());
}
returnsb.toString();
}

/**
*将16进制转换为二进制
*
*@paramhexStr
*@return
*/
publicstaticbyte[]parseHexStr2Byte(StringhexStr){
if(hexStr.length()<1)
returnnull;
byte[]result=newbyte[hexStr.length()/2];
for(inti=0;i<hexStr.length()/2;i++){
inthigh=Integer.parseInt(hexStr.substring(i*2,i*2+1),16);
intlow=Integer.parseInt(hexStr.substring(i*2+1,i*2+2),
16);
result[i]=(byte)(high*16+low);
}
returnresult;
}
publicstaticvoidmain(String[]args)throwsException{
/*
*加密用的Key可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定
*/
StringcKey="assistant7654321";
//需要加密的字串
StringcSrc="123456";
//加密
longlStart=System.currentTimeMillis();
StringenString=encrypt(cSrc,cKey);
System.out.println("加密后的字串是:"+enString);
longlUseTime=System.currentTimeMillis()-lStart;
System.out.println("加密耗时:"+lUseTime+"毫秒");
//解密
lStart=System.currentTimeMillis();
StringDeString=decrypt(enString,cKey);
System.out.println("解密后的字串是:"+DeString);
lUseTime=System.currentTimeMillis()-lStart;
System.out.println("解密耗时:"+lUseTime+"毫秒");
}
}

㈡ DES 加密后的密钥,如何保存或发送

import java.security.Key;
import java.security.KeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class Test {

////这块不熟还请自己测试
public static void main(String [] args) {
String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish
KeyGenerator keygen;
try {
keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
Cipher ci = Cipher.getInstance("DES");
//将其包装为byte[]发送
byte[] b = ci.wrap(deskey);
//发送
//...
//接收到后
//解封装
Key key = ci.unwrap(b, Algorithm, Cipher.PRIVATE_KEY);
//...

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}

}
}

㈢ des加密中的key是多少进制

二进制。
DES算法为密码体制中的对称密码体制,又被成为美国数据加密标准,是1972年美国IBM公司研制的对称密码体制加密算法。
其密钥长度为56位,明文按64位进行分组,将分组后的明文组和56位的密钥按位替代或交换的方法形成密文组的加密方法。DES加密算法特点:分组比较短、密钥太短、密码生命周期短、运算速度较慢。

㈣ java des加密,密钥的长度是多少

3des算法是指使用双长度(16字节)密钥k=(kl||kr)将8字节明文数据块进行3次des加密/解密。如下所示:
y
=
des(kl)[des-1(kr)[des(kl[x])]]
解密方式为:
x
=
des-1
(kl)[des
(kr)[
des-1
(kl[y])]]
其中,des(kl[x])表示用密钥k对数据x进行des加密,des-1
(kl[y])表示用密钥k对数据y进行解密。
sessionkey的计算采用3des算法,计算出单倍长度的密钥。表示法为:sk
=
session(dk,data)
3des加密算法为:
void
3des(byte
doublekeystr[16],
byte
data[8],
byte
out[8])
{
byte
buf1[8],
buf2[8];
des
(&doublekeystr[0],
data,
buf1);
udes(&doublekeystr[8],
buf1,
buf2);
des
(&doublekeystr[0],
buf2,
out);
}

㈤ 本人需要设计一套加密算法,密文 = DES(明文, key = 时间戳 + 密钥 )。

des的算法我这里有,你改改就能符合你的要求

㈥ 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("解密");
}
以上方法亲测可用。

㈦ 请问DES加密算法中的这3个函数(获取密钥函数,加密函数与解密函数)的含义是什么(附函数算法)

/// <summary>
/// DES密钥
/// </summary>
private const string KEY_64 = "11111111";//注意了,是8个字符,64位
/// <summary>
/// DES向量
/// </summary>
private const string IV_64 = "11111111";
/// <summary>
/// 加密DES
/// </summary>
/// <param name="data">需要加密的字符串</param>
/// <returns>加密后的字符串</returns>
public static string EncodeDES(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

}
/// <summary>
/// 解密DES
/// </summary>
/// <param name="data">需要解密的字符串</param>
/// <returns>解密后的字符串</returns>
public static string DecodeDES(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}

㈧ 请教各位朋友,DES加密key值安全问题

给他个固定的种子

String s = "xxxxx ";
String Algorithm = "DES "; // 定义 加密算法,可用 DES,DESede,BlowfishKeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
keygen.init(new SecureRandom(s.getBytes()));
SecretKey deskey = keygen.generateKey();

㈨ 如何解决DES加密算法中KEY和IV的8字节限制

DES是70年代的东西,本身就只能支持64位密钥,实际只是56位,最后8位是奇偶校验

并且非常容易破,就算是brute force方式,也可以在1天之内破解

所以换Rijndael吧,有n个好处:
1) Rijndael的现成实现RijndaelManaged是托管类,不需要用Windows的CSP
2) 支持更长的密钥
3) 是国际新标准,叫做AES

㈩ C# des加密,密钥可以不是8位

标准的DES密钥长度为64bit,密钥每个字符占7bit,外加1bit的奇偶校验,64/(7+1)=8。

所以必须是8个字符也只能是8个字符。

但 .NET 里 DESCryptoServiceProvider 这个类是微软已经封装好的了,如果密钥长度不足,会以 PKCS7Padding 方式补足位。

补足位原理参考:http://www.cnblogs.com/Lawson/archive/2012/05/20/2510781.html

阅读全文

与deskey加密相关的资料

热点内容
phpsql单引号 浏览:83
英雄联盟压缩壁纸 浏览:450
办公app需要什么服务器 浏览:626
安卓服务器怎么获得 浏览:806
空调压缩机冷媒的作用 浏览:779
淘宝app是以什么为利的 浏览:655
java提取图片文字 浏览:922
我的世界手机版指令复制命令 浏览:33
java判断字符串为数字 浏览:924
androidrpc框架 浏览:488
云服务器essd和ssd 浏览:522
家用网关的加密方式 浏览:1
怎么从ppt导出pdf文件 浏览:971
换汽车空调压缩机轴承 浏览:845
平板怎么登录安卓端 浏览:195
图像拼接计算法 浏览:255
怎么打开饥荒服务器的本地文件夹 浏览:291
usb扫描枪编程 浏览:673
博易大师手机app叫什么 浏览:663
刮眼影盘解压方法 浏览:966