MD5加密实现

  #pragma once

//Author:ArcherSC

//JLU

namespace ENCRPYTION
{
class MD5
{
private:
    unsigned int m_Content[16];
    unsigned int m_ContentLen;
    unsigned int m_TotalLen;
    static unsigned int m_T[64];
    unsigned int m_A, m_B, m_C, m_D;
private:
    inline void FF(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s);
    inline void GG(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s);
    inline void HH(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s);
    inline void II(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s);
    inline unsigned int roateLeft(unsigned int a, unsigned int c);

    void transform();
public:
    //初始化
    void beginCal();
    //表明不再输入数据了,将添加填充位
    void endCal();
    //初始化后,输入数据,在调用endCal前,都可以继续添加数据
    void addData(const char* data, unsigned int size);
    //结束运算后,取得编码
    char* getCode();
    void    clearMemoryHelper(char* p);
};
}
#include "MD5.h"
#include <cstdlib>
#include <cstring>

//Author:ArcherSC

//JLU

namespace ENCRPYTION
{
using namespace std;
unsigned int MD5::m_T[64] = {
          0xD76aa478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE,
          0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
          0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE,
          0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
          0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA,
          0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8,
          0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED,
          0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
          0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C,
          0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
          0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05,
          0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
          0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039,
          0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
          0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1,
          0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391
          };

void MD5::FF(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s)
{
    a = b + roateLeft(a + ((b & c) | ((~b) & d)) + m_Content[k] + m_T[i], s);
}
void MD5::GG(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s)
{
    a = b + roateLeft(a + ((b & d) | (c & (~d))) + m_Content[k] + m_T[i], s);
}
void MD5::HH(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s)
{
    a = b + roateLeft(a + (b ^ c ^ d) + m_Content[k] + m_T[i], s);
}
void MD5::II(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s)
{
    a = b + roateLeft(a + (c ^ (b | (~d))) + m_Content[k] + m_T[i], s);
}
unsigned int MD5::roateLeft(unsigned int a, unsigned int c)
{
    return ( (a << c) | (a >> (32-c)) );
}

void MD5::transform()
{
    unsigned int AA, BB, CC, DD;
    AA = m_A;
    BB = m_B;
    CC = m_C;
    DD = m_D;
    //1 round
    FF(AA, BB, CC, DD, 0, 0, 7);
    FF(DD, AA, BB, CC, 1, 1, 12);
    FF(CC, DD, AA, BB, 2, 2, 17);
    FF(BB, CC, DD, AA, 3, 3, 22);
    FF(AA, BB, CC, DD, 4, 4, 7);
    FF(DD, AA, BB, CC, 5, 5, 12);
    FF(CC, DD, AA, BB, 6, 6, 17);
    FF(BB, CC, DD, AA, 7, 7, 22);
    FF(AA, BB, CC, DD, 8, 8, 7);
    FF(DD, AA, BB, CC, 9, 9, 12);
    FF(CC, DD, AA, BB, 10, 10, 17);
    FF(BB, CC, DD, AA, 11, 11, 22);
    FF(AA, BB, CC, DD, 12, 12, 7);
    FF(DD, AA, BB, CC, 13, 13, 12);
    FF(CC, DD, AA, BB, 14, 14, 17);
    FF(BB, CC, DD, AA, 15, 15, 22);
    //2 round
    GG(AA, BB, CC, DD, 1, 16, 5);
    GG(DD, AA, BB, CC, 6, 17, 9);
    GG(CC, DD, AA, BB, 11, 18, 14);
    GG(BB, CC, DD, AA, 0, 19, 20);
    GG(AA, BB, CC, DD, 5, 20, 5);
    GG(DD, AA, BB, CC, 10, 21, 9);
    GG(CC, DD, AA, BB, 15, 22, 14);
    GG(BB, CC, DD, AA, 4, 23, 20);
    GG(AA, BB, CC, DD, 9, 24, 5);
    GG(DD, AA, BB, CC, 14, 25, 9);
    GG(CC, DD, AA, BB, 3, 26, 14);
    GG(BB, CC, DD, AA, 8, 27, 20);
    GG(AA, BB, CC, DD, 13, 28, 5);
    GG(DD, AA, BB, CC, 2, 29, 9);
    GG(CC, DD, AA, BB, 7, 30, 14);
    GG(BB, CC, DD, AA, 12, 31, 20);

    //3 round
    HH(AA, BB, CC, DD, 5, 32, 4);
    HH(DD, AA, BB, CC, 8, 33, 11);
    HH(CC, DD, AA, BB, 11, 34, 16);
    HH(BB, CC, DD, AA, 14, 35, 23);
    HH(AA, BB, CC, DD, 1, 36, 4);
    HH(DD, AA, BB, CC, 4, 37, 11);
    HH(CC, DD, AA, BB, 7, 38, 16);
    HH(BB, CC, DD, AA, 10, 39, 23);
    HH(AA, BB, CC, DD, 13, 40, 4);
    HH(DD, AA, BB, CC, 0, 41, 11);
    HH(CC, DD, AA, BB, 3, 42, 16);
    HH(BB, CC, DD, AA, 6, 43, 23);
    HH(AA, BB, CC, DD, 9, 44, 4);
    HH(DD, AA, BB, CC, 12, 45, 11);
    HH(CC, DD, AA, BB, 15, 46, 16);
    HH(BB, CC, DD, AA, 2, 47, 23);

    //4 round
    II(AA, BB, CC, DD, 0, 48, 6);
    II(DD, AA, BB, CC, 7, 49, 10);
    II(CC, DD, AA, BB, 14, 50, 15);
    II(BB, CC, DD, AA, 5, 51, 21);
    II(AA, BB, CC, DD, 12, 52, 6);
    II(DD, AA, BB, CC, 3, 53, 10);
    II(CC, DD, AA, BB, 10, 54, 15);
    II(BB, CC, DD, AA, 1, 55, 21);
    II(AA, BB, CC, DD, 8, 56, 6);
    II(DD, AA, BB, CC, 15, 57, 10);
    II(CC, DD, AA, BB, 6, 58, 15);
    II(BB, CC, DD, AA, 13, 59, 21);
    II(AA, BB, CC, DD, 4, 60, 6);
    II(DD, AA, BB, CC, 11, 61, 10);
    II(CC, DD, AA, BB, 2, 62, 15);
    II(BB, CC, DD, AA, 9, 63, 21);
    m_A = m_A + AA;
    m_B = m_B + BB;
    m_C = m_C + CC;
    m_D = m_D + DD;
}
//初始化
void MD5::beginCal()
{
    memset(m_Content, 0, 64);
    m_ContentLen = 0;
    m_TotalLen = 0;
    m_A = 0x67452301;
    m_B = 0xEFCDAB89;
    m_C = 0x98BADCFE;
    m_D = 0x10325476;
}

void MD5::endCal()
{
    memset((char*)m_Content + m_ContentLen, 0x80, 1);
    if (m_ContentLen < 56){
     memset((char*)m_Content + m_ContentLen + 1, 0, 55 - m_ContentLen);
    }
    else{
     memset((char*)m_Content + m_ContentLen + 1, 0, 63 - m_ContentLen);
     transform();
     memset((char*)m_Content, 0, 56);
    }
    unsigned int h, l;
    h = 0;
    l = m_TotalLen;
    h |= ((l & 0x80000000) >> 31);
    h <<= 1;
    l <<= 1;
    h |= ((l & 0x80000000) >> 31);
    h <<= 1;
    l <<= 1;
    h |= ((l & 0x80000000) >> 31);
    h <<= 1;
    l <<= 1;
    memcpy(m_Content + 14, &l, 4);
    memcpy(m_Content + 15, &h, 4);
    transform();
}

void MD5::addData(const char* data, unsigned int size)
{
    unsigned int i = 0;
    unsigned int remain = 64 - m_ContentLen;
    if (remain > size)
     remain = size;
    memcpy(((char*)m_Content) + m_ContentLen, data, remain);
    i += remain;
    m_ContentLen += remain;
    m_TotalLen += remain;
    if (m_ContentLen < 64)
     return;

    transform();

    while (i + 64 <= size){
     memcpy(m_Content, data + i, 64);
     transform();
     i += 64;
     m_TotalLen += 64;
    }

    m_ContentLen = size - i;
    m_TotalLen += m_ContentLen;
    memcpy(m_Content, data + i, m_ContentLen);
}

char* MD5::getCode()
{
    char * code = new char[16];
    memcpy(code, &m_A, 4);
    memcpy(code + 4, &m_B, 4);
    memcpy(code + 8, &m_C, 4);
    memcpy(code + 12, &m_D, 4);
    return code;
}
void    MD5::clearMemoryHelper(char* p)
{
    delete []p;
}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,可以使用以下代码来实现MD5加密和登录注册功能: 1. MD5加密方法: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Utils { public static String encrypt(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(input.getBytes()); byte[] digest = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b & 0xff)); } return sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } } ``` 2. 注册功能: ```java import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Registration { private Map<String, String> users; public Registration() { users = new HashMap<>(); } public void register() { Scanner scanner = new Scanner(System.in); System.out.println("请输入用户名:"); String username = scanner.nextLine(); System.out.println("请输入密码:"); String password = scanner.nextLine(); String encryptedPassword = MD5Utils.encrypt(password); users.put(username, encryptedPassword); System.out.println("注册成功!"); } } ``` 3. 登录功能: ```java import java.util.Map; import java.util.Scanner; public class Login { private Map<String, String> users; public Login(Map<String, String> users) { this.users = users; } public void login() { Scanner scanner = new Scanner(System.in); System.out.println("请输入用户名:"); String username = scanner.nextLine(); System.out.println("请输入密码:"); String password = scanner.nextLine(); String encryptedPassword = MD5Utils.encrypt(password); String storedPassword = users.get(username); if (storedPassword != null && storedPassword.equals(encryptedPassword)) { System.out.println("登录成功!"); } else { System.out.println("用户名或密码错误!"); } } } ``` 使用示例: ```java public class Main { public static void main(String[] args) { Registration registration = new Registration(); Login login = new Login(registration.getUsers()); registration.register(); login.login(); } } ``` 以上代码实现了简单的MD5加密的注册和登录功能。当用户进行注册时,会将密码进行MD5加密后存储;而在登录时,会将用户输入的密码进行MD5加密后与存储的加密密码进行比对。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值