关于des加密的示例

c中常用的是opensslcrypto库中的方法:

openssl中的测试代码:Link

static int test_des_cbc(void)
{
    unsigned char cbc_in[40];
    unsigned char cbc_out[40];
    DES_cblock iv3;
    DES_key_schedule ks;
    const size_t cbc_data_len = strlen((char *)cbc_data);

    if (!TEST_int_eq(DES_set_key_checked(&cbc_key, &ks), 0))
        return 0;
    memset(cbc_out, 0, sizeof(cbc_out));
    memset(cbc_in, 0, sizeof(cbc_in));
    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_ncbc_encrypt(cbc_data, cbc_out, cbc_data_len + 1, &ks,
                     &iv3, DES_ENCRYPT);
    if (!TEST_mem_eq(cbc_out, 32, cbc_ok, 32))
        return 0;

    memcpy(iv3, cbc_iv, sizeof(cbc_iv));
    DES_ncbc_encrypt(cbc_out, cbc_in, cbc_data_len + 1, &ks,
                     &iv3, DES_DECRYPT);
    return TEST_mem_eq(cbc_in, cbc_data_len, cbc_data, cbc_data_len);
}

常用函数说明:LINK

nt DES_set_key_checked (DES_cblock *key, DES_key_schedule *ks)
Just like DES_set_key_unchecked() except checking that the key is not weak for or have correct parity.
Parameters:

key a key to initialize the key schedule with. 
ks a key schedule to initialize.
Returns:

0 on success, -1 on invalid parity, -2 on weak key.
 

int DES_set_key_unchecked (DES_cblock *key, DES_key_schedule *ks)
Setup a des key schedule from a key. The key is no longer needed after this transaction and can cleared.
Does NOT check that the key is weak for or have wrong parity.

Parameters:

key a key to initialize the key schedule with. 
ks a key schedule to initialize.
Returns:

0 on success
 

void DES_set_odd_parity (DES_cblock *key)
Set the parity of the key block, used to generate a des key from a random key. See DES key generation.
Parameters:

key key to fixup the parity for.

更详细一点的解释:LINK

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值