• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > c++实现MD5算法实现代码

c++实现MD5算法实现代码

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了md5加密算法c++实现,c++ md5,des加密算法c++代码,银行家算法c++代码,kmp算法c++代码等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

测试结果和百度百科测试例子一致。

实现过程中需要注意事项:最后把四个变量A B C D 链接成结果时 ,注意变量高低位的先后顺序,具体参考 LinkResult()方法。

md5.h

#include <iostream>
#include <string>
using namespace std;

class MD5
{
    public:
        typedef unsigned char uchar8; //make sure it is 8bit
        typedef char char8; //make sure it is 8bit
        MD5();

        void init();

        void UpdateMd5(const uchar8 input[], const int length);     
        void UpdateMd5(const char8 input[], const int length);    

        void Finalize();

        void ComputMd5(const uchar8 input[], const int length);
        void ComputMd5(const char8 input[], const int length);

        string GetMd5();

        void printMd5();

       
    private:
        typedef unsigned int uint32;       //make sure it is 32 bit;
        typedef unsigned long long uint64; //make sure it is 64 bit;
        uint32 A, B, C, D;
        const static int blockLen_ = 64;    // 512/8                                 
        //the remain after last updata (because md5 may be computed segment by segment)
        uchar8 remain_[blockLen_];                   
        int remainNum_ ;         // the number of remain_,  < 64
        uint64 totalInputBits_;
        uchar8 md5Result_[16];   //bit style md5 result,totally 128 bit
        char md5Result_hex_[33]; //hexadecimal style result; md5Result_hex_[32]='\0'
        bool isDone_;            // indicate the comput is finished;

        inline uint32 RotateLeft(const uint32 x, int n);
        inline uint32 F(const uint32 x, const uint32 y, const uint32 z);
        inline uint32 G(const uint32 x, const uint32 y, const uint32 z);
        inline uint32 H(const uint32 x, const uint32 y, const uint32 z);
        inline uint32 I(const uint32 x, const uint32 y, const uint32 z);
        inline void FF(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
                       const uint32 Mj, const int s, const uint32 ti);
        inline void GG(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
                       const uint32 Mj, const int s, const uint32 ti);
        inline void HH(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
                       const uint32 Mj, const int s, const uint32 ti);
        inline void II(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
                       const uint32 Mj, const int s, const uint32 ti);
                      

        void UcharToUint(uint32 output[], const uchar8 input[], const unsigned int transLength);

        void FourRound(const uchar8 block[]);   

        void LinkResult();

};

/* user guide
   you can comput the md5 by using the funtion ComputMd5
   eg:
       MD5 m;
       MD5::char8 str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
       m.ComputMd5(str,sizeof(str) - 1);  
       m.printMd5();

    if you want to comput segment by segment,you can do as follow, and init() is suggested
    the begging,and Finalize() must call in the end:

        MD5 M;
        m.init();
        MD5::uchar8 str1[] = "ABCDEFGHIJKLMN";
        MD5::uchar8 str2[] = "OPQRSTUVWXYZabcdefghijk";
        MD5::uchar8 str3[] = "lmnopqrstuvwxyz";
        m.UpdateMd5(str1,sizeof(str1) - 1);
        m.UpdateMd5(str2,sizeof(str2) - 1);
        m.UpdateMd5(str3,sizeof(str3) - 1);
        m.Finalize();
        m.printMd5();

    if you want to comput the md5 of a file, you can use the interface of this program.
*/

#endif
</div>

md5.cpp

#include<iostream>
using namespace std;

const int S[4][4] = {7, 12, 17, 22,
                     5, 9, 14, 20,
                     4, 11, 16, 23,
                     6, 10, 15, 21};
void MD5::init()
{
    A = 0x67452301;
    B = 0xefcdab89;
    C = 0x98badcfe;
    D = 0x10325476;
    remainNum_ = 0;
    remain_[0] = '\0';
    md5Result_hex_[0] = '\0';
 &nb

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C++ MD5的源码实例详解
  • C++中四种加密算法之AES源代码
  • C++实现简单遗传算法
  • C++实现一维向量旋转算法
  • 基于C++实现的各种内部排序算法汇总
  • C++实现各种排序算法类汇总
  • c++实现MD5算法实现代码

相关文章

  • 2017-05-28构建mfc窗体的简单示例
  • 2017-05-28C++设计模式编程中的观察者模式使用示例
  • 2017-05-28CFile与CStdioFile的文件读写使用方法详解
  • 2017-05-28C++实现哈夫曼树简单创建与遍历的方法
  • 2017-05-28简单讲解C语言中宏的定义与使用
  • 2017-05-28C语言 存储类详解及示例代码
  • 2017-05-28C语言中的sizeof操作符用法及和strlen的区别
  • 2017-05-28C语言中的内存泄露 怎样避免与检测
  • 2017-05-28构造函数不能声明为虚函数的原因及分析
  • 2017-05-28使用C语言实现CRC校验的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C语言 二叉树的链式存储实例
    • Linux环境g++编译GDAL动态库操作方法
    • C与C++之间相互调用实例方法讲解
    • C++可变参数的函数与模板实例分析
    • C语言单向链表的表示与实现实例详解
    • C++统计中英文大小写字母、数字、空格及其他字符个数的方法
    • C++ explicit构造函数实例解析
    • C++实现翻转单词顺序
    • C语言设置和取得socket状态的相关函数用法
    • 用贪心法求解背包问题的解决方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有