Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

EncryptUtils

纯 Delphi XE2 开发的加解密工具库,哈希/编解码使用原生实现,对称/非对称加密基于 Windows CryptoAPI。

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

EncryptUtils — Delphi XE2 加解密公用函数库

概述

纯 Delphi XE2 开发的加解密工具库,哈希/编解码使用原生实现,对称/非对称加密基于 Windows CryptoAPI。

使用方式

pascal
uses EncryptLib;

引用 EncryptLib 即可调用所有加解密函数。


公共类型

pascal
TOutputFormat = (ofBase64, ofHex);  // 字符串输出格式

所有加密函数均提供 AOutput / AInput 参数指定编码格式。


一、哈希函数

MD5

pascal
function MD5Hash(const AData: string): string;           // 32 位十六进制
function MD5HashBytes(const AData: TBytes): TBytes;
function MD5HMAC(const AData, AKey: string): string;       // HMAC-MD5

SHA

pascal
type TSHAVersion = (sha1, sha224, sha256);

function SHAHash(const AData: string; AVersion: TSHAVersion = sha256): string;
function SHAHashBytes(const AData: TBytes; AVersion: TSHAVersion = sha256): TBytes;
function SHA1Hash(const AData: string): string;
function SHA256Hash(const AData: string): string;
function SHA256HMAC(const AData, AKey: string): string;

二、编解码

Base64

pascal
function Base64Encode(const AData: string): string; overload;
function Base64Encode(const AData: TBytes): string; overload;
function Base64Decode(const AData: string): string; overload;
function Base64DecodeToBytes(const AData: string): TBytes;
function Base64UrlEncode(const AData: string): string;
function Base64UrlDecode(const AData: string): string;

URL

pascal
function URLEncode(const AData: string): string;    // 空格 -> %20
function URLDecode(const AData: string): string;    // %XX 和 + 均支持

三、AES 加密

pascal
type
  TAESKeySize = (aes128, aes192, aes256);
  TAESMode = (aesECB, aesCBC, aesCFB);
  TAESPadding = (aesPKCS7, aesZeros, aesANSIX923);

function AESEncrypt(const AData, AKey, AIV: string;
  AKeySize: TAESKeySize = aes256;
  AMode: TAESMode = aesCBC;
  APadding: TAESPadding = aesPKCS7;
  AOutput: TOutputFormat = ofBase64): string;

function AESDecrypt(const AData, AKey, AIV: string;
  AKeySize: TAESKeySize = aes256;
  AMode: TAESMode = aesCBC;
  APadding: TAESPadding = aesPKCS7;
  AInput: TOutputFormat = ofBase64): string;

密钥派生:AKey 字符串通过 SHA256 哈希派生为指定长度的密钥。 IV 派生:AIV 字符串通过 SHA256 哈希取前 16 字节。


四、DES / 3DES 加密

pascal
type
  TDESAlgorithm = (desDES, des3DES);
  TDESMode = (desECB, desCBC);
  TDESPadding = (desPKCS7, desZeros);

function DESEncrypt(const AData, AKey, AIV: string;
  AAlgo: TDESAlgorithm = des3DES;
  AMode: TDESMode = desCBC;
  APadding: TDESPadding = desPKCS7;
  AOutput: TOutputFormat = ofBase64): string;

function DESDecrypt(const AData, AKey, AIV: string;
  AAlgo: TDESAlgorithm = des3DES;
  AMode: TDESMode = desCBC;
  APadding: TDESPadding = desPKCS7;
  AInput: TOutputFormat = ofBase64): string;

密钥派生:DES 取 SHA1 前 8 字节,3DES 取 SHA256 前 24 字节。


五、RSA 非对称加密

pascal
procedure RSAGenerateKeys(AKeySize: Integer;
  out APublicKey, APrivateKey: string);

function RSAEncrypt(const AData, APublicKey: string;
  AOutput: TOutputFormat = ofBase64): string;

function RSADecrypt(const AData, APrivateKey: string;
  AInput: TOutputFormat = ofBase64): string;

function RSASign(const AData, APrivateKey: string;
  AOutput: TOutputFormat = ofBase64): string;                       // SHA256

function RSAVerify(const AData, ASignature, APublicKey: string;
  AInput: TOutputFormat = ofBase64): Boolean;                       // SHA256

function RSASignSHA1(const AData, APrivateKey: string;
  AOutput: TOutputFormat = ofBase64): string;                       // SHA1

function RSAVerifySHA1(const AData, ASignature, APublicKey: string;
  AInput: TOutputFormat = ofBase64): Boolean;                       // SHA1

密钥格式:返回值 APublicKey / APrivateKey 为 Base64 编码的 CryptoAPI BLOB。 密钥生成:RSAGenerateKeys(2048, pub, priv) 生成 2048 位密钥对。


六、国密 SM(占位)

pascal
function SM3Hash(const AData: string): string;
function SM3HMAC(const AData, AKey: string): string;
function SM4Encrypt(const AData, AKey: string; AOutput: TOutputFormat = ofBase64): string;
function SM4Decrypt(const AData, AKey: string; AInput: TOutputFormat = ofBase64): string;
function SM2Encrypt(const AData, APublicKey: string; AOutput: TOutputFormat = ofBase64): string;
function SM2Decrypt(const AData, APrivateKey: string; AInput: TOutputFormat = ofBase64): string;
function SM2Sign(const AData, APrivateKey: string; AOutput: TOutputFormat = ofBase64): string;
function SM2Verify(const AData, ASignature, APublicKey: string; AInput: TOutputFormat = ofBase64): Boolean;

所有国密函数调用时将抛出 Exception('[SM] 国密算法尚未实现…')。


项目文件结构

pascal
EncryptUtils/
????????? EncryptLib.pas              ??? ??????????????????????????? uses ????????????
????????? EncryptUtils.Core.pas       ??? ???????????????????????????
????????? EncryptUtils.MD5.pas        ??? MD5
????????? EncryptUtils.SHA.pas        ??? SHA1/SHA224/SHA256
????????? EncryptUtils.Base64.pas     ??? Base64 ?????????
????????? EncryptUtils.URL.pas        ??? URL ?????????
????????? EncryptUtils.AES.pas        ??? AES-128/192/256
????????? EncryptUtils.DES.pas        ??? DES/3DES
????????? EncryptUtils.RSA.pas        ??? RSA ??????/??????/??????
????????? EncryptUtils.SM.pas         ??? ????????????

依赖项

依赖 用途 说明
System.Hash MD5/SHA 哈希 Delphi XE2 原生自带
Soap.EncdDecd Base64 编解码 Delphi XE2 原生自带
Windows (CryptoAPI) AES/DES/RSA 加解密 Windows 系统 API,无需额外 DLL