三重DES加密的密钥为啥是24个字节
import java.security.MessageDigest;import java.security.Security;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import com.sun.crypto.provider.SunJCE;public class CipherUtil{ public enum DES{\tDESede(DESede),DES(DES),Blowfish(Blowfish);\tpublic final String ALGORITHM;\tpublic void DES(final String algorithm)\t{\t\tthis.ALGORITHM = algorithm;\t} } // 定义 加密算法,可用 DES,DESede,Blowfish // keybyte为加密密钥,长度为24字节 // src为被加密的数据缓冲区(源) public byte encrypt(final byte keybyte,final byte src,final DES Cipher) { try {\t// 生成密钥\tSecretKey deskey = new SecretKeySpec(keybyte,Cipher.ALGORITHM);\t// 加密\tCipher c1 = Cipher.getInstance(Cipher.ALGORITHM);\tc1.init(Cipher.ENCRYPT_MODE, deskey);\treturn c1.doFinal(src); } catch (java.lang.Exception ex) {\t ex.printStackTrace(); }\t\treturn null; } // keybyte为加密密钥,长度为24字节\t// src为加密后的缓冲区 public byte decrypt(byte keybyte, byte src,final DES Cipher) {\ttry {\t // 生成密钥\t SecretKey deskey = new SecretKeySpec(keybyte, Cipher.ALGORITHM);\t // 解密\t Cipher c1 = Cipher.getInstance(Cipher.ALGORITHM);\t c1.init(Cipher.DECRYPT_MODE, deskey);\t return c1.doFinal(src);\t } catch (java.lang.Exception ex) {\t\t\tex.printStackTrace();\t }\t return null;\t}}
■网友
不太明白题主的着重点在哪里?但是,单单从问题来看 我这么回答吧。单des密钥是8字节,三重des密钥就是3*8=24字节啊。原理:DES 使用一个 56 位的密钥以及附加的 8 位奇偶校验位(每组的第8位作为奇偶校验位),产生最大 64 位的分组大小。这是一个迭代的分组密码,使用称为 Feistel 的技术,其中将加密的文本块分成两半。使用子密钥对其中一半应用循环功能,然后将输出与另一半进行“异或”运算;接着交换这两半,这一过程会继续下去,但最后一个循环不交换。DES 使用 16 轮循环,使用异或,置换,代换,移位操作四种基本运算。单DES,仅仅使用64bit(8bytes)做为密钥。DES是对称加密。要破解目前就是穷举暴力破解,DES的安全性完全依靠密钥长度。随着时间推移单DES已经不太安全啦。所以必须提升密钥长度,双倍长(16bytes )和 三倍长(3DES or TDES Tripe DES)(24bytes)密钥就应用起来。
■网友
实践证明密钥长度为128位(16字节)或者192位(24字节),每个字节的最后一位为校验位不用于加密,可以随意更改校验程序,密钥长度为16字节,两组密钥只有每个字节的第8位不同,第二组密钥正常的解密由第一组密钥加密的结果byte key10 = new byte { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };byte key20 = new byte { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18 };byte key12 = new byte { 0x00, 0x03, 0x02, 0x05, 0x04, 0x07, 0x06, 0x09 };byte key22 = new byte { 0x10, 0x13, 0x12, 0x15, 0x14, 0x17, 0x16, 0x19 };byte iv = new byte { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28 };string s = "gagged";byte plain = Encoding.UTF8.GetBytes(s);using (var desEnc = new TripleDESCryptoServiceProvider())using (var desDec = new TripleDESCryptoServiceProvider()){ byte dec; byte enc; desEnc.Key = key10.Concat(key20).ToArray(); desEnc.IV = iv; desEnc.Mode = CipherMode.CBC; desEnc.Padding = PaddingMode.PKCS7; desDec.Key = key12.Concat(key22).ToArray(); desDec.IV = iv; desDec.Mode = CipherMode.CBC; desDec.Padding = PaddingMode.PKCS7; using (var encryptor = desEnc.CreateEncryptor()) using (var decryptor = desDec.CreateDecryptor()) { using (var mem = new MemoryStream()) using (var cryptoStream = new CryptoStream(mem, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plain, 0, plain.Length); cryptoStream.FlushFinalBlock(); enc = mem.ToArray(); } using (var mem = new MemoryStream(enc)) using (var cryptoStream = new CryptoStream(mem, decryptor, CryptoStreamMode.Read)) { dec = new byte; int len = cryptoStream.Read(dec, 0, dec.Length); dec = dec.Take(len).ToArray(); Console.WriteLine(Encoding.UTF8.GetString(dec)); } }}
推荐阅读
- aes对称加密,需要加签名验证防止篡改吗如果需要该咋签名才是最好的方案
- 利税|三重三破!姜堰这个镇探索提升亩均效益新路径……
- 目前在做程序化购买的运营投放工作,去trading desk还是去adnetwork对个人发展较好
- 怎样把wifi密码加密,不让别人破解
- 怎样防止网站上传木马
- 在云盘存色情文件犯法不犯
- 糖分|少让孩子吃,三重隐患可能在等着四类食物是潜在的“含糖大户”
- 怎样看待苹果公司的《Designed by Apple in California》画册
- 趣头条|日产奇骏N-Design于海外市场发布 预计最快年内开售
- Java的Process.destroy()是Unix中是怎样实现的
