DES ecb加解密在C語言、python下的分別實現


C語言實現:

下面僅僅是用8 bytes 16進制的data和8 bytes 16進制的key,做個簡單測試,
並順手做了個解密,並把結果打印出來。
在此種情形下,網絡上各種網頁版的DES加密、解密工具的結果和下文的代碼是不同的。

舉例:(16進制8 bytes)

key:         1234567812345678  
data:        61B6EF78C6435CCD  
ciphertext:   FEDFDAA09E53547B
/*ONLY support 8 bytes input */
int des_encrypt_decrypt(const unsigned char *data, const unsigned char *key, unsigned char *ciphertext)
{

    DES_cblock output, o_de;
    DES_key_schedule schedule;


    DES_set_key_unchecked(key, &schedule);
    printf("set key unchecked!\n");

    DES_ecb_encrypt(data, &output, &schedule, DES_ENCRYPT);
    printf("DES ecb encrypt done!\n");

    printf("ciphertext: ");
    for(int i=0; i<8; i++)
    {
        printf("%02X", output[i]);
        ciphertext[i] = output[i];
    }
    printf("\n");

    DES_ecb_encrypt(ciphertext, &o_de, &schedule, DES_DECRYPT);
    printf("DES ecb decrypt done!\n");
    printf("cleartext: ");
    for(int i=0; i<8; i++)
    {
        printf("%02X", o_de[i]);
    }
    printf("\n");

    return 0;
}


python語音實現:

在python下,用pyDes實現,要注意的是進制的轉換,str轉byte array,byte array 轉str,
還有就是padmode的影響。
github上的地址: https://github.com/twhiteman/pyDes
上面有這樣的簡介:

Class initialization
--------------------
pyDes.des(key, [mode], [IV], [pad], [padmode])
pyDes.triple_des(key, [mode], [IV], [pad], [padmode])

key     -> Bytes containing the encryption key. 8 bytes for DES, 16 or 24 bytes
	   for Triple DES
mode    -> Optional argument for encryption type, can be either
	   pyDes.ECB (Electronic Code Book) or pyDes.CBC (Cypher Block Chaining)
IV      -> Optional Initial Value bytes, must be supplied if using CBC mode.
	   Length must be 8 bytes.
pad     -> Optional argument, set the pad character (PAD_NORMAL) to use during
	   all encrypt/decrpt operations done with this instance.
padmode -> Optional argument, set the padding mode (PAD_NORMAL or PAD_PKCS5)
	   to use during all encrypt/decrypt operations done with this instance.

I recommend to use PAD_PKCS5 padding, as then you never need to worry about any
padding issues, as the padding can be removed unambiguously upon decrypting
data that was encrypted using PAD_PKCS5 padmode.

Common methods
--------------
encrypt(data, [pad], [padmode])
decrypt(data, [pad], [padmode])

data    -> Bytes to be encrypted/decrypted
pad     -> Optional argument. Only when using padmode of PAD_NORMAL. For
	   encryption, adds this characters to the end of the data block when
	   data is not a multiple of 8 bytes. For decryption, will remove the
	   trailing characters that match this pad character from the last 8
	   bytes of the unencrypted data block.
padmode -> Optional argument, set the padding mode, must be one of PAD_NORMAL
	   or PAD_PKCS5). Defaults to PAD_NORMAL.
	  

Example
-------
import pyDes

# For Python3, you'll need to use bytes, i.e.:
#   data = b"Please encrypt my data"
#   k = pyDes.des(b"DESCRYPT", pyDes.CBC, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)

data = "Please encrypt my data"
k = pyDes.des("DESCRYPT", pyDes.CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
d = k.encrypt(data)
print "Encrypted: %r" % d
print "Decrypted: %r" % k.decrypt(d)
assert k.decrypt(d) == data


See the pyDes test file (test_pydes.py) for more examples of use.

Note: This code was not written for high-end systems needing a fast
      implementation, but rather a handy portable solution with small usage.

結合上述文檔和網上的資料,以及自己爬的坑,寫了下面這個代碼,
目前在8 bytes 16進制data和8 bytes 16進制key下面工作正常。
僅供參考:

舉例:(16進制8 bytes)

key:         1234567812345678  
data:        61B6EF78C6435CCD  
ciphertext:   FEDFDAA09E53547B
from pyDes import CBC,des,PAD_PKCS5,PAD_NORMAL
import binascii
#key = "1234567812345678"
#key = bytearray.fromhex(key)
#print("key: \t", key, " len: ", len(key))
iv = b"\0\0\0\0\0\0\0\0"
deser = des(key, CBC, iv, pad=None, padmode=PAD_NORMAL)
#
#data = bytearray.fromhex(data)
#print("data: \t", data, " len: ", len(data))
cipherText = deser.encrypt(data)
print("cipherText: \t", cipherText, " len: ", len(cipherText))
#import array
print("\tb2a_hex: \t", binascii.b2a_hex(cipherText), " len: ", len(cipherText))
clearText = deser.decrypt(cipherText)
print("clearText: \t", binascii.b2a_hex(clearText))


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章