随笔 - 224  文章 - 41  trackbacks - 0
<2010年5月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

享受编程

常用链接

留言簿(11)

随笔分类(159)

随笔档案(224)

文章分类(2)

文章档案(4)

经典c++博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜

在python下可以下载pydes 下载地址为 http://pydes.sourceforge.net/

在c#下实现des加密较为简单,如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace des
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string ss = EncryptDES("1123""12345678");

            
string aa = DecryptDES(ss, "12345678");
        }

        
//默认密钥向量
        private static string iv = "1234567812345678";
        
/// <summary>
        
/// DES加密字符串
        
/// </summary>
        
/// <param name="encryptString">待加密的字符串</param>
        
/// <param name="encryptKey">加密密钥,要求为8位</param>
        
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            
try
            {
                
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(08));
                
byte[] rgbIV = Encoding.UTF8.GetBytes(iv);
                
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP 
= new DESCryptoServiceProvider();
                dCSP.Mode 
= CipherMode.CBC;
                dCSP.Padding 
= PaddingMode.PKCS7;
                MemoryStream mStream 
= new MemoryStream();
                CryptoStream cStream 
= new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 
0, inputByteArray.Length);
                cStream.FlushFinalBlock();

                
return Convert.ToBase64String(mStream.ToArray());
            }
            
catch
            {
                
return encryptString;
            }
        }

        
/// <summary>
        
/// DES解密字符串
        
/// </summary>
        
/// <param name="decryptString">待解密的字符串</param>
        
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string DecryptDES(string decryptString, string decryptKey)
        {
            
try
            {
                
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(08));
                
byte[] rgbIV = Encoding.UTF8.GetBytes(iv);
                
byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider dCSP 
= new DESCryptoServiceProvider();
                dCSP.Mode 
= CipherMode.CBC;
                dCSP.Padding 
= PaddingMode.PKCS7;
                MemoryStream mStream 
= new MemoryStream();
                CryptoStream cStream 
= new CryptoStream(mStream, dCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 
0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                
return Encoding.UTF8.GetString(mStream.ToArray());
            }
            
catch
            {
                
return decryptString;
            }
        }
    }
}

python的实现如下:
#coding:utf-8
import binascii
import base64
import pyDes

class DES:
    
#IV必须是 8 字节长度的十六进制数
    iv = '1234567812345678'
    
#key加密密钥长度,24字节
    key = '12345678'
    
def __init__(self, iv, key):
        self.iv 
= iv
        self.key 
= key
    
def encrypt(self, data):
        k 
= pyDes.triple_des(self.key, pyDes.CBC, self.iv, pad=None, padmode=pyDes.PAD_PKCS5)
        d 
= k.encrypt(data)
        d 
= base64.encodestring(d)
        
return d
    
def decrypt(self, data):
        k 
= pyDes.triple_des(self.key, pyDes.CBC, self.iv, pad=None, padmode=pyDes.PAD_PKCS5)
        data 
= base64.decodestring(data)
        d 
= k.decrypt(data)
        
return d
if __name__ == '__main__':
    data 
= "1123"
    des 
= DES('12345678','1234567812345678')
    encryptdata 
= des.encrypt(data.encode('utf-8'))
    
print  encryptdata
    decryptdata 
= des.decrypt(encryptdata)
    
print decryptdata


posted on 2013-01-06 14:28 漂漂 阅读(5294) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理