跳至主要內容

標籤「C#」的 4 篇文章

查看所有標籤

C# 中的 MD5 雜湊

· 3 分鐘閱讀

這篇備忘錄記錄了在 C# 中實現 MD5 雜湊的方法。

資訊

MD5 (Message-Digest Algorithm 5) 是一種廣泛使用的加密雜湊函數,它生成一個 128 位(16 字節)的雜湊值。儘管 MD5 在加密安全方面已被認為不再安全(存在碰撞攻擊),但它仍然經常用於文件完整性校驗或非安全數據的快速雜湊。

1. 使用 System.Security.Cryptography.MD5

C# 提供了內置的 MD5 類(位於 System.Security.Cryptography 命名空間)來計算 MD5 雜湊值。

範例:對字串進行 MD5 雜湊

using System;
using System.Security.Cryptography;
using System.Text;

public class Md5Hasher
{
public static string CalculateMd5Hash(string input)
{
// 創建 MD5 雜湊算法的實例
using (MD5 md5 = MD5.Create())
{
// 將輸入字串轉換為字節數組
byte[] inputBytes = Encoding.UTF8.GetBytes(input);

// 計算雜湊值
byte[] hashBytes = md5.ComputeHash(inputBytes);

// 將字節數組轉換為十六進制字串
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2")); // "x2" 格式化為兩位十六進制
}
return sb.ToString();
}
}

public static void Main(string[] args)
{
string text = "Hello, MD5!";
string hash = CalculateMd5Hash(text);
Console.WriteLine($"原始字串: {text}");
Console.WriteLine($"MD5 雜湊: {hash}");

// 預期的 MD5 雜湊值 for "Hello, MD5!" (小寫)
// 9e2469446d3288a7c207b04cfd6e01a4
}
}

範例:對文件進行 MD5 雜湊

對文件內容計算 MD5 雜湊通常用於驗證文件是否在傳輸過程中被修改。

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public class FileMd5Hasher
{
public static string CalculateFileMd5Hash(string filePath)
{
if (!File.Exists(filePath))
{
return "文件不存在。";
}

using (MD5 md5 = MD5.Create())
{
using (FileStream stream = File.OpenRead(filePath))
{
byte[] hashBytes = md5.ComputeHash(stream);

StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
return sb.ToString();
}
}
}

public static void Main(string[] args)
{
string filePath = "example.txt"; // 替換為你的文件路徑

// 創建一個範例文件
File.WriteAllText(filePath, "這是一個測試文件的內容。");

string fileHash = CalculateFileMd5Hash(filePath);
Console.WriteLine($"文件路徑: {filePath}");
Console.WriteLine($"文件 MD5 雜湊: {fileHash}");

// 清理範例文件
File.Delete(filePath);
}
}

2. 安全注意事項

  • 不適用於密碼儲存:由於 MD5 存在嚴重的安全漏洞(例如碰撞攻擊),絕對不應該將其用於儲存用戶密碼。對於密碼,應使用專為此目的設計的單向雜湊函數,如 PBKDF2、bcrypt 或 scrypt。
  • 不適用於數字簽名或認證:同樣,MD5 不應在需要強加密安全的場景中使用。
  • 用途:主要用途應該限於文件完整性校驗(驗證文件是否被意外更改)或生成非安全數據的快速雜湊。

總結

C# 中的 MD5 類提供了一種簡單直觀的方式來計算 MD5 雜湊值。雖然它在安全性方面存在限制,但在正確的應用場景下(如文件校驗),它仍然是一個有用的工具。務必牢記其安全限制,避免在加密敏感的上下文中使用它。

C 語言的二維陣列處理

· 1 分鐘閱讀

用結構體將行數、列數與儲存數值的記憶體整合在一起,會更方便操作。

#include <stdio.h>
#include <stdlib.h>

typedef struct {
float *data;
int col_size;
int row_size;
} Mat;

void MatInit(Mat *mat, int row_size, int col_size) {
mat->row_size = row_size;
mat->col_size = col_size;
mat->data = (float *)calloc(row_size * col_size, sizeof(float));
}

float *MatAt(Mat *mat, int i, int j) {
return mat->data + i * mat->col_size + j;
}

void MatFree(Mat *mat) { free(mat->data); }

int main(void) {
Mat mat;
MatInit(&mat, 30, 5); // 初始化 30 行 5 列的矩陣
*MatAt(&mat, 0, 0) = 50; // 將第 0 行第 0 列設為 50
printf("%f\n", *MatAt(&mat, 0, 0)); // 顯示第 0 行第 0 列的數值

MatFree(&mat);

return 0;
}

GCD 程式

· 2 分鐘閱讀

這篇筆記記錄了 GCD(最大公約數)的 C 語言程式。

什麼是 GCD?

資訊

GCD(Greatest Common Divisor),中文稱為最大公約數,是指兩個或多個整數共有的約數中最大的一個。

例如,24 和 36 的公約數有 1、2、3、4、6、12,其中最大的公約數是 12。

歐幾里得演算法 (Euclidean Algorithm)

計算 GCD 最常見且有效的方法是歐幾里得演算法。其原理基於以下定理:

$$ \gcd(a, b) = \gcd(b, a \bmod b) $$

當 $b = 0$ 時,$\gcd(a, 0) = a$。

C 語言程式碼實現

遞迴版本:

// gcd.c
int gcd(int a, int b){
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}

非遞迴版本:

// gcd_iterative.c
int gcd_iterative(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

測試程式

#include <stdio.h>

// 遞迴版本的 GCD 函數
int gcd(int a, int b){
return !b ? a : gcd(b, a % b);
}

// 非遞迴版本的 GCD 函數
int gcd_iterative(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}


int main(void){
printf("GCD(24, 36) = %d
", gcd(24, 36)); // 輸出 12
printf("GCD_iterative(24, 36) = %d
", gcd_iterative(24, 36)); // 輸出 12
printf("GCD(48, 18) = %d
", gcd(48, 18)); // 輸出 6
printf("GCD_iterative(48, 18) = %d
", gcd_iterative(48, 18)); // 輸出 6
return 0;
}

總結

GCD 是數論中的一個基本概念,歐幾里得演算法提供了一種高效的計算方法。無論是遞迴還是非遞迴實現,其核心思想都是利用模運算將問題規模不斷縮小,直到其中一個數變為零。

建立 DLL 並編譯 C

· 1 分鐘閱讀

建立 DLL 並編譯 C 的備忘錄

函式庫

原始碼檔案

// gcd.c
int gcd(int a, int b){
return !b ? a : gcd(b, a % b);
}

標頭檔

// gcd.h
#ifndef TEST_H
#define TEST_H

int gcd(int a, int b);

#endif

程式碼

#include <stdio.h>
#include "gcd.h"

int main(void){
printf("%d
", gcd(24, 36));
}

編譯

建立 DLL

gcc gcd.c -shared -o gcd.dll

編譯

gcc main.c -lgcd -L.
標籤: