導航:首頁 > 文檔加密 > 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加密相關的資料

熱點內容
神獸領域安卓怎麼下載 瀏覽:250
單片機交通燈ad原理圖 瀏覽:413
多功能解壓磁鐵筆 瀏覽:80
少兒編程火箭升空 瀏覽:401
蘭斯10游戲解壓碼 瀏覽:42
手機proxy伺服器地址 瀏覽:449
吉他清音壓縮 瀏覽:301
簡歷模板程序員 瀏覽:882
螺桿壓縮機虛標型號 瀏覽:953
idea開發項目伺服器ip地址 瀏覽:125
串口伺服器出現亂碼怎麼解決 瀏覽:950
命令按鈕的default 瀏覽:161
戰網如何登錄其他伺服器 瀏覽:990
中國銀行app如何關閉簡訊 瀏覽:493
nx120編程技巧 瀏覽:722
手機也能使用源碼公式 瀏覽:918
怎樣把壓縮的文件下載 瀏覽:335
pdf是哪的 瀏覽:27
群暉伺服器如何建立自己資料庫 瀏覽:868
win10怎麼查找伺服器地址 瀏覽:506