创建一个从56位二进制串DES密钥 [英] Create DES key from 56 bit binary string

查看:441
本文介绍了创建一个从56位二进制串DES密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想作为DES加密密钥使用一个56位二进制字符串。

I have a 56 bit binary string that i want to use as the secret key for DES encryption.

我发现下面的code。在JCA文档网站

I found the following code at the JCA docs website

byte[] desKeyData = { (byte)0x01, (byte)0x02, (byte)0x03, 
(byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08 };
DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

然而,该使用8字节用于键(而不是7)。目前尚不清楚,如果desKeyData [0]对应于至少显著字节或最显著之一。另外,是否有可能直接使用56位串,以产生字节数组可用于这一目的

However this uses 8 bytes for the key (instead of 7). It is not clear if the desKeyData[0] corresponds to the least significant byte or the most significant one. Also, is it possible to use the 56 bit string directly to generate the byte array that can be used for this purpose ?

推荐答案

维基百科:

的关键表面上由64比特;然而,只有这些56实际使用的算法。八位仅仅是用来检查校验,并随后丢弃。因此,有效的密钥长度为56位,并且它从不引述这样。选择键的每个第8位被丢弃,即职务8,16,24,32,40,48,56,64与64位密钥徒留56位密钥删除。

The key ostensibly consists of 64 bits; however, only 56 of these are actually used by the algorithm. Eight bits are used solely for checking parity, and are thereafter discarded. Hence the effective key length is 56 bits, and it is never quoted as such. Every 8th bit of the selected key is discarded, i.e. positions 8, 16, 24, 32, 40, 48, 56, 64 are removed from the 64 bit key leaving behind only the 56 bit key.

因此​​,至少显著位(即第0位)不用于键结构,他们可以用于通过检查奇偶DESKeySpec.isParityAdjusted()

So, the least significant bits (i.e. 0th bits) are not used for key construction, they can be used for checking parity by DESKeySpec.isParityAdjusted().

编辑:简单的测试显示,最不显著位被忽略:

Simple test showing that the least significant bits are ignored:

SecretKeyFactory sf = SecretKeyFactory.getInstance("DES");
byte[] in = "test".getBytes("UTF-8");

Cipher c1 = Cipher.getInstance("DES");
c1.init(Cipher.ENCRYPT_MODE, sf.generateSecret(new DESKeySpec(
   new byte[] {0x10,0x20,0x30,0x40,0x50,0x60,0x70,(byte) 0x80})));
byte[] r1 = c1.doFinal(in);

Cipher c2 = Cipher.getInstance("DES");
c2.init(Cipher.ENCRYPT_MODE, sf.generateSecret(new DESKeySpec(
    new byte[] {0x11,0x21,0x31,0x41,0x51,0x61,0x71,(byte) 0x81})));
byte[] r2 = c2.doFinal(in);

assertArrayEquals(r1, r2);  

这篇关于创建一个从56位二进制串DES密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆