⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ascryptos.int

📁 CryptoKit使用简便的加密解密控件。(源代码
💻 INT
字号:
unit ASCryptos;
interface
uses
  SysUtils, Windows, Classes,
  ASGtMath, ASUtils;

// *****************************************************************************
// RSA
// *****************************************************************************

const
  RSA_DEF_SMALLPUBLICEXP =  3;
  RSA_MIN_NUMBITS      =  256;
  RSA_DEF_NUMBITS      = 1024;
  RSA_MAX_NUMBITS      = 4096;

const
  RSA_EME_OAEP_DELIM:   Byte =  01;
  RSA_EME_OAEP_PADC:    Byte =  00;

  RSA_EME_PKCS1_BEGIN:  Byte =  02;
  RSA_EME_PKCS1_END:    Byte =  00;

  RSA_EMSA_PKCS1_BEGIN: Byte =  01;
  RSA_EMSA_PKCS1_END:   Byte =  00;
  RSA_EMSA_PKCS1_PADC:  Byte = $FF;

type
  TRSAEncryptionScheme = (esPkcs1, esOaep);

// *****************************************************************************
// Symmetric
// *****************************************************************************

type
  TCipherMode = (cmECB, cmCBC, cmCFB, cmOFB);

const
  DEF_BLOCK_BYTES = 8;

// *****************************************************************************
// DES
// *****************************************************************************

const
  DES_USERKEY_BYTES =   8;

  DES_EXTKEY_BYTES  = 128;
  DES_BLOCK_BYTES   =   8;

  DES_TRIPLE_CODEMASK = 5;
  DES_MAX_QUANT       = 8 * sizeof(DWord);

type
  PDesExtendedKey = ^DesExtendedKey;
  DesExtendedKey = array[0..DES_EXTKEY_BYTES-1] of Byte;

  PDesTripleExtendedKey = ^DesTripleExtendedKey;
  DesTripleExtendedKey = array[0..2] of DesExtendedKey;

  PDesMultipleExtendedKey = ^DesMultipleExtendedKey;
  DesMultipleExtendedKey = array[0..DES_MAX_QUANT-1] of DesExtendedKey;

  PDesData = ^DesData;
  DesData  = array[0..DES_BLOCK_BYTES-1] of Byte;

  procedure DESKeySetup(Key: PByteArray; ExtKey: PDesExtendedKey);
  procedure DESTripleKeySetup(Key: PByteArray; ExtKey: PDesTripleExtendedKey);
  procedure DESExtKeySetup(Key: PByteArray; KeySize: DWord; ExtKey: PDesMultipleExtendedKey);

  procedure DESBlockCipher(ExtKey: PDesExtendedKey; Data: PByteArray; encrypting: Boolean);
  procedure DESTripleBlockCipher(ExtKey: PDesTripleExtendedKey; Data: PByteArray; encrypting: Boolean);
  procedure DESExtBlockCipher(ExtKey: PDesMultipleExtendedKey; Data: PByteArray; UserKeySize, mask: DWord; encrypting: Boolean);

// *****************************************************************************
// RC2
// *****************************************************************************

const
  RC2_MIN_KEY_BYTES = 1;
  RC2_MAX_KEY_BYTES = 128;

  RC2_EXTKEY_BYTES  = 128;
  RC2_EXTKEY_WORDS  = (RC2_EXTKEY_BYTES shr 1);

  RC2_BLOCK_BYTES   = 8;
  RC2_BLOCK_WORDS   = (RC2_BLOCK_BYTES shr 1);

  RC2_MIN_EFFBITS   =  40;
  RC2_DEF_EFFBITS   = 128;
  
type
  PRC2ExtendedKey = ^RC2ExtendedKey;
  RC2ExtendedKey = record
    case Integer of
      0: (B: array[0..RC2_EXTKEY_BYTES-1] of Byte);
      1: (W: array[0..RC2_EXTKEY_WORDS-1] of Word);
  end;

  PRC2Data = ^RC2Data;
  RC2Data = record
    case Integer of
      0: (B: array[0..RC2_BLOCK_BYTES-1] of Byte);  // 8 bytes
      1: (W: array[0..RC2_BLOCK_WORDS-1] of Word);  // 4 words (16 bit)
  end;

  procedure RC2KeySetup(Key: PByteArray; KeySize, EffectiveBits: DWord; ExtKey: PRC2ExtendedKey);
  procedure RC2BlockEncrypt(ExtKey: PRC2ExtendedKey; Data: PRC2Data);
  procedure RC2BlockDecrypt(ExtKey: PRC2ExtendedKey; Data: PRC2Data);

// *****************************************************************************
// RC4
// *****************************************************************************

const
  RC4_MIN_KEY_BYTES = 1;

  RC4_EXTKEY_DWORDS = 256;
  RC4_EXTKEY_BYTES  = RC4_EXTKEY_DWORDS * sizeof(DWord);
  RC4_BLOCK_BYTES   = 8;

type
  PRC4ExtendedKey = ^RC4ExtendedKey;
  RC4ExtendedKey = record
    x, y: DWord;
    data: array[0..RC4_EXTKEY_DWORDS-1] of DWord;
  end;

  PRC4Data = ^RC4Data;
  RC4Data  = array[0..RC4_BLOCK_BYTES-1] of Byte;

  procedure RC4KeySetup(Key: PByteArray; KeySize: DWord; ExtKey: PRC4ExtendedKey);
  procedure RC4BlockCipher(ExtKey: PRC4ExtendedKey; Data: PRC4Data; DataSize: DWord);

// *****************************************************************************
// RC5
// *****************************************************************************

const
  RC5_EXTKEY_DWORDS  = 64;
  RC5_EXTKEY_BYTES   = (RC5_EXTKEY_DWORDS * sizeof(DWord));
  RC5_BLOCK_BYTES    = 8;

  RC5_MAX_KEY_BYTES  = RC5_EXTKEY_BYTES;

  RC5_DEF_ROUNDS     = 12;
  RC5_MAX_ROUNDS     = 31;

type
  PRC5ExtendedKey = ^RC5ExtendedKey;
  RC5ExtendedKey  = record
    S: array[0..RC5_EXTKEY_DWORDS-1] of DWord;	// S-box
    numRounds: Integer;				// Number of rounds
  end;

  PRC5Data = ^RC5Data;
  RC5Data = record
    case Integer of
      0: (A1, A2: DWord);
      2: (B: array[0..7] of Byte);
  end;

  procedure RC5KeySetup(Key: PByteArray; KeySize: DWord; NumRounds: Integer; ExtKey: PRC5ExtendedKey);
  procedure RC5BlockEncrypt(ExtKey: PRC5ExtendedKey; Data: PRC5Data);
  procedure RC5BlockDecrypt(ExtKey: PRC5ExtendedKey; Data: PRC5Data);

// *****************************************************************************
// Blowfish
// *****************************************************************************

const
  BF_BLOCK_BYTES   =  8;
  BF_BLOCK_DWORDS  = BF_BLOCK_BYTES shr 2;
  BF_NUM_ROUNDS    = 16;

  BF_MAX_KEY_BYTES = (BF_NUM_ROUNDS+2) * 4;

type
  PBFExtendedKey = ^BFExtendedKey;
  BFExtendedKey = record
    P: array[0..BF_NUM_ROUNDS+2-1] of DWord;
    S: array[0..4*256-1] of DWord;
  end;

  PBfData = ^BfData;
  BfData = record
    case Integer of
      0: (D: array[0..BF_BLOCK_DWORDS-1] of DWord);
      1: (B: array[0..BF_BLOCK_BYTES -1] of Byte);
  end;

  procedure BFKeySetup(Key: PByteArray; KeySize: DWord; ExtKey: PBFExtendedKey);
  procedure BFBlockEncrypt(ExtKey: PBFExtendedKey; Data: PBFData);
  procedure BFBlockDecrypt(ExtKey: PBFExtendedKey; Data: PBFData);

// *****************************************************************************
// CAST
// *****************************************************************************

const
  CAST_BLOCK_BYTES  = 8;
  CAST_BLOCK_DWORDS = CAST_BLOCK_BYTES shr 2;

  CAST_MAX_KEY_BYTES = 16;

type
  PCASTExtendedKey = ^CASTExtendedKey;
  CASTExtendedKey = array[0..31] of DWord;

  PCASTData = ^CASTData;
  CASTData = record
    case Integer of
      0: (D: array[0..CAST_BLOCK_DWORDS-1] of DWord);
      1: (B: array[0..CAST_BLOCK_BYTES-1]  of Byte);
  end;

  procedure CASTKeySetup(Key: PByteArray; KeySize: DWord; ExtKey: PCASTExtendedKey);
  procedure CASTBlockEncrypt(ExtKey: PCASTExtendedKey; Data: PCASTData);
  procedure CASTBlockDecrypt(ExtKey: PCASTExtendedKey; Data: PCASTData);

// *****************************************************************************
// Idea
// *****************************************************************************

const
  IDEA_BLOCK_BYTES   = 8;
  IDEA_BLOCK_WORDS   = IDEA_BLOCK_BYTES shr 1;

  IDEA_USERKEY_BYTES = 16;

  IDEA_ROUNDS        =  8;
  IDEA_EXTKEY_WORDS  = (6 * IDEA_ROUNDS + 4);

type
  PIdeaHalfExtKey = ^IdeaHalfExtKey;
  IdeaHalfExtKey  = array[0..IDEA_EXTKEY_WORDS] of Word;

  PIdeaExtendedKey = ^IdeaExtendedKey;
  IdeaExtendedKey  = record
    EKey: IdeaHalfExtKey;  // encoding key
    DKey: IdeaHalfExtKey;  // decoding key
  end;

  PIdeaData = ^IdeaData;
  IdeaData = record
    case Integer of
      0: (B: array[0..IDEA_BLOCK_BYTES-1] of Byte);  // 8 bytes
      1: (W: array[0..IDEA_BLOCK_WORDS-1] of Word);  // 4 words (16 bit)
  end;

  procedure IdeaKeySetup(Key: PByteArray; ExtKey: PIdeaExtendedKey);    // KeySize = 16 !
  procedure IdeaBlockEncrypt(ExtKey: PIdeaExtendedKey; Data: PIdeaData);
  procedure IdeaBlockDecrypt(ExtKey: PIdeaExtendedKey; Data: PIdeaData);


// *****************************************************************************
// Digest algorithms
// *****************************************************************************

type
  TDigestKind = (dkMDX, dkSHA);

// *****************************************************************************
// MDx
// *****************************************************************************

const
  MDX_DIGEST_SIZE = 16;

type
  TDigestMDX = array[0..MDX_DIGEST_SIZE-1] of Byte;
  TMDXVersion = (dgMD2, dgMD4, dgMD5);

  function ExtractDigestMD2(input: Pointer; inputLen: DWord): TDigestMDX;
  function ExtractDigestMD4(input: Pointer; inputLen: DWord): TDigestMDX;
  function ExtractDigestMD5(input: Pointer; inputLen: DWord): TDigestMDX;
  function ExtractDigestMDX(input: Pointer; inputLen: DWord; Version: TMDXVersion): TDigestMDX;

// *****************************************************************************
// SHA1
// *****************************************************************************

const
  SHA_DIGEST_SIZE = 20;

type
  TDigest160 = array[0..SHA_DIGEST_SIZE-1] of Byte;

  function ExtractDigestSHA1(input: Pointer; inputLen: DWord): TDigest160;

// *****************************************************************************
// RIPEMD-160
// *****************************************************************************

// The RIPEMD-160 block size and message digest sizes, in bytes

const
  RIPEMD160_DIGESTSIZE = 20;

  function ExtractDigestRIPEMD160(input: Pointer; inputLen: DWord): TDigest160;


// *****************************************************************************
// Coding algorithms
// *****************************************************************************

// *****************************************************************************
// Base64
// *****************************************************************************

  function Base64EncodedSize(SourceLen: DWord): DWord;
  function Base64Encode(Source: Pointer; SourceLen: DWord; Dest: Pointer; DestSize: DWord; var DestLen: DWord): Boolean;

  function Base64DecodedSize(EncData: Pointer; EncDataLen: DWord; var NumPad: DWord): DWord;
  function Base64Decode(EncData: Pointer; EncDataLen: DWord; Dest: Pointer; DestSize: DWord; var DestLen: DWord): Boolean;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -