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))
本帖子中包含资源
您需要 登录 才可以下载,没有帐号?立即注册