获取IP地址在进行MD5加密

package test.加密;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @NAME: ChenSiYu
 * @DATE: 2022/4/27
 **/
public class 获取windows的mac地址 {
    /**
     * windows的
     * 根据mac地址进行加密
     * @param mac
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static final String getOnlyId(String mac) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] md5 = md.digest(mac.getBytes());
        StringBuilder id = new StringBuilder();
        for (int i = 0; i < md5.length; i++) {
            String hex = Integer.toHexString(md5[i] & 0xFF);
            if (hex.length() < 2) {
                hex = "0" + hex;
            }
            id.append(hex);
        }
        return id.toString().toUpperCase();
    }

    /**
     *  windows
     * 获取本机的mac地址
     * @return 返回mac地址
     * @throws IOException
     */
    public static final String getMacAddress() throws IOException {
        NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
        byte[] ha = ni.getHardwareAddress();
        StringBuilder mac = new StringBuilder();
        for (int i = 0; i < ha.length; i++) {
            String hex = Integer.toHexString(ha[i] & 0xFF);
            if (hex.length() < 2) {
                hex = "0" + hex;
            }
            mac.append(hex).append("-");
        }
        return mac.deleteCharAt(mac.length() - 1).toString().toUpperCase();
    }
    
    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
        System.out.println(getOnlyId(getMacAddress()));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您解答这个问题。以下是一段用VS编写的登陆/注册程序,它可以读取CPU和网卡地址作为注册信息,并使用MD5格式加密: ```c++ #include <iostream> #include <string> #include <Windows.h> #include <iphlpapi.h> #include <winsock2.h> #include <openssl/md5.h> #pragma comment(lib, "iphlpapi.lib") #pragma comment(lib, "ws2_32.lib") using namespace std; string getCPUID() { string cpuID; __asm { mov eax, 0 cpuid mov cpuID, ebx mov cpuID, edx mov cpuID, ecx } return cpuID; } string getMACAddress() { PIP_ADAPTER_INFO pAdapterInfo; ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO); pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO)); if (pAdapterInfo == NULL) { return ""; } if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { free(pAdapterInfo); pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen); if (pAdapterInfo == NULL) { return ""; } } if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR) { string macAddress = pAdapterInfo->Address[0]; for (int i = 1; i < pAdapterInfo->AddressLength; i++) { macAddress += ":"; macAddress += pAdapterInfo->Address[i]; } free(pAdapterInfo); return macAddress; } free(pAdapterInfo); return ""; } string md5(string str) { unsigned char md[MD5_DIGEST_LENGTH]; MD5((unsigned char*)str.c_str(), str.length(), md); char buf[MD5_DIGEST_LENGTH * 2 + 1]; for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { sprintf_s(buf + i * 2, 3, "%02x", md[i]); } return buf; } int main() { string username, password; cout << "请输入用户名:"; cin >> username; cout << "请输入密码:"; cin >> password; string cpuID = getCPUID(); string macAddress = getMACAddress(); string registrationInfo = cpuID + macAddress; string encryptedRegistrationInfo = md5(registrationInfo); cout << "用户名:" << username << endl; cout << "密码:" << password << endl; cout << "CPU ID:" << cpuID << endl; cout << "MAC地址:" << macAddress << endl; cout << "注册信息:" << registrationInfo << endl; cout << "加密后的注册信息:" << encryptedRegistrationInfo << endl; return 0; } ``` 这段代码可以获取CPU ID和MAC地址,并将它们组合成注册信息,然后使用MD5格式加密。请注意,这段代码只是一个示例,实际应用中需要更多的安全措施来保护用户的隐私和安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值