python 拖拉桌面小程序. :)
PHP: Soap 通过 soap header 认证(non-wsdl 模式)

triple_des(des3) 算法 - php,python 实现

TLHL28 posted @ Mon, 23 May 2011 11:20:37 +0800 in 随笔 with tags DES php python , 9634 readers

调用电信那边的接口. 要用到 triple_des 加密算法. 整了python 和 php 两个版本的实现.

iv, key 变量指向的都是 hex 字符串.




function PaddingPKCS7($data) {
	$block_size = mcrypt_get_block_size("tripledes", "cbc");
	$padding_char = $block_size - (strlen($data) % $block_size);
	$data .= str_repeat(chr($padding_char),$padding_char);
	return $data;

function fmt3DESEx($data, $key, $iv) {
	$td = mcrypt_module_open(MCRYPT_3DES,"", MCRYPT_MODE_CBC, "");
	$key = pack("H48",$key);
	$iv = pack("H16",$iv);
	mcrypt_generic_init($td, $key, $iv);
	$data = PaddingPKCS7($data);
	$desResult = mcrypt_generic($td, $data);
	return base64_encode($desResult);     

$data = "asdf";
$key = "313233343536373839303132333435363738393031323334";
$iv = "3132333435363738";
$code = fmt3DESEx(mhash(MHASH_SHA1,$data), $key, $iv);
echo $code;



#!/usr/bin/env python
# -*- coding:utf-8 -*-

import binascii
import hashlib
import base64
import pyDes

iv = '3132333435363738'
key = '313233343536373839303132333435363738393031323334'
data = "asdf"

iv = binascii.unhexlify(iv)
key = binascii.unhexlify(key)
data = hashlib.sha1(data)

k = pyDes.triple_des(key, pyDes.CBC, iv, pad=None, padmode=pyDes.PAD_PKCS5)
d = k.encrypt(data.digest())
print base64.encodestring(d)


SalvadorPatrick said:
Sat, 15 Dec 2018 16:21:10 +0800

All these posts are great only because there is a thing about the way that it goes on easy research topics for college students. If they can know more about it, then I will be happy about it too.

Rylan Landon said:
Fri, 28 Dec 2018 19:25:40 +0800

This is exactly the text I'm sure uncovering everywhere you look. Thanks a lot for a blog page, I just join up your website. That is the decent blog page. inches. Accountants Crawley

charlos said:
Tue, 22 Jan 2019 18:05:03 +0800

Nice blog, I will keep visiting this blog very often. videoder

voyance amour said:
Tue, 22 Jan 2019 22:29:51 +0800

I would like to say that this blog really convinced me to do it! Thanks, very good post.

Login *

loading captcha image...
(type the code from the image)
or Ctrl+Enter