openssl对任何大小的键都开放

时间:2014-04-23 15:43:03

标签: cryptography openssl pyopenssl

openssl如何使用密钥,因为它采用任意大小的密钥(任意大小为1个字节)。在这里转到实际密钥的步骤是什么..

openssl enc -d -des-ecb -in cipher.txt -out text.out -K '530343412312345445123345677812345678812324' 

2 个答案:

答案 0 :(得分:2)

  

openssl如何使用密钥......程序是什么......

这取决于程序,但程序通常在整个库中是一致的。在您的示例中,您使用的是openssl dec,因此您正在使用dec子程序。源代码在<openssl dir>/apps/enc.c中提供(encdec属于enc.c)。

以下是相关部分:

unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH];
unsigned char salt[PKCS5_SALT_LEN];
...
char *hkey=NULL,*hiv=NULL,*hsalt = NULL;

-K的参数存储在hkey

else if (strcmp(*argv,"-K") == 0)
{
    if (--argc < 1) goto bad;
    hkey= *(++argv);
}

然后,在第580行附近:

if ((hkey != NULL) && !set_hex(hkey,key,sizeof key))
{
    /* Handle failure */
}

set_hex如下所示,hex解码通过-K传入的参数。它通过memset向后填充未使用的长度为0。未使用的长度为EVP_MAX_KEY_LENGTH减去长度-K参数(十六进制解码后)。

最后,围绕610行:

if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc))
{
    /* Handle failure */
}

注意-k(小k)采用不同的代码路径,并使用EVP_BytesToKey派生密钥。


int set_hex(char *in, unsigned char *out, int size)
{
    int i,n;
    unsigned char j;

    n=strlen(in);
    if (n > (size*2))
    {
        BIO_printf(bio_err,"hex string is too long\n");
        return(0);
    }
    memset(out,0,size);
    for (i=0; i<n; i++)
    {
        j=(unsigned char)*in;
        *(in++)='\0';
        if (j == 0) break;
        if ((j >= '0') && (j <= '9'))
            j-='0';
        else if ((j >= 'A') && (j <= 'F'))
            j=j-'A'+10;
        else if ((j >= 'a') && (j <= 'f'))
            j=j-'a'+10;
        else
        {
            BIO_printf(bio_err,"non-hex digit\n");
            return(0);
        }
        if (i&1)
            out[i/2]|=j;
        else
            out[i/2]=(j<<4);
    }
    return(1);
}

答案 1 :(得分:0)

我对案件的观察得出以下结论:

  1. 采用十六进制值
  2. 如果大小小于8个字节,则填充0
  3. 前8个字节作为键