• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

DELPHI的简单DES实现

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一个小项目中要用到DES加解密,临时抱佛脚,在网上找了一番,还是好多的,从简单到复杂都有。

比较完整的实现有

PassByYou888大神的: https://github.com/PassByYou888/CoreCipher;但是没有包装,也比较大。

还有:https://github.com/winkelsdorf/DelphiEncryptionCompendium;这个中间的数据全部是用RawByteString来实现,还没仔细看

DCryptLib,这个没找到在哪下载,其它语言的Git上倒是有

各种博客上也有好多,比如:https://blog.csdn.net/yubiaohyb/article/details/50630035 ,这个能用,只是要求用ansistring,有时候不方便。

小项目,就不用大的了,用简单的就好。

就把 https://blog.csdn.net/yubiaohyb/article/details/50630035 这个包装下,直接用uncodestring

  1 unit DESEncrypt;
  2 
  3 interface
  4 
  5 uses
  6   System.SysUtils, System.Classes, System.Types, System.NetEncoding;
  7 
  8 type
  9   TKeyByte = array [0..5] of Byte;
 10   TDesMode = (dmEncry, dmDecry);
 11 
 12   TJkCryptograph = class
 13   private
 14     class var FCoverLen: Integer;
 15     class var FSubKey: array [0..15] of TKeyByte;
 16     class procedure InitPermutation(var inData: array of Byte);
 17     class procedure Permutation(var inData: array of Byte);
 18     class procedure PermutationChoose1(inData: array of Byte; var outData: array of Byte);
 19     class procedure PermutationChoose2(inData: array of Byte; var outData: array of Byte);
 20     class procedure ConversePermutation(var inData: array of Byte);
 21     class procedure Expand(inData: array of Byte; var outData: array of Byte);
 22     class function  Sigma(si, inByte: Byte): Byte;
 23     class procedure CycleMove(var inData: array of Byte; bitMove: Byte);
 24     class procedure MakeKey(inKey: array of Byte; var outKey: array of TKeyByte);
 25     class procedure CryptData(inData, subKey: array of Byte; var outData: array of Byte);
 26     class procedure DesCryptData(desMode: TDesMode; inData: array of Byte; var outData: array of Byte);
 27 
 28   public
     //下面几个方法是包装的,方便现在的版本用 29 class function EncryptData(const AData, AKey: TBytes): TBytes; 30 class function DecryptData(const ACryptData, AKey: TBytes): TBytes; 31 32 class function EncryptStr(const AText, AKey: string): string; 33 class function DecryptStr(const AEncryptText, AKey: string): string; 34 class function EncryptStrHex(const AText, AKey: string): string; 35 class function DecryptStrHex(const AEncryptHexText, AKey: string): string; 36 class function EncryptStrBase64(const AText, AKey: string): string; 37 class function DecryptStrBase64(const AEncryptBase64Text, AKey: string): string; 38 end; 39 40 implementation 41 42 const 43 DefKEY = '12345678'; 44 45 BitIP: array[0..63] of Byte = 46 (57, 49, 41, 33, 25, 17, 9, 1, 47 59, 51, 43, 35, 27, 19, 11, 3, 48 61, 53, 45, 37, 29, 21, 13, 5, 49 63, 55, 47, 39, 31, 23, 15, 7, 50 56, 48, 40, 32, 24, 16, 8, 0, 51 58, 50, 42, 34, 26, 18, 10, 2, 52 60, 52, 44, 36, 28, 20, 12, 4, 53 62, 54, 46, 38, 30, 22, 14, 6 ); 54 55 BitCP: array[0..63] of Byte = 56 ( 39, 7, 47, 15, 55, 23, 63, 31, 57 38, 6, 46, 14, 54, 22, 62, 30, 58 37, 5, 45, 13, 53, 21, 61, 29, 59 36, 4, 44, 12, 52, 20, 60, 28, 60 35, 3, 43, 11, 51, 19, 59, 27, 61 34, 2, 42, 10, 50, 18, 58, 26, 62 33, 1, 41, 9, 49, 17, 57, 25, 63 32, 0, 40, 8, 48, 16, 56, 24 ); 64 65 BitExp: array[0..47] of Integer = 66 ( 31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9,10, 67 11,12,11,12,13,14,15,16,15,16,17,18,19,20,19,20, 68 21,22,23,24,23,24,25,26,27,28,27,28,29,30,31,0 ); 69 70 BitPM: array[0..31] of Byte = 71 ( 15, 6,19,20,28,11,27,16, 0,14,22,25, 4,17,30, 9, 72 1, 7,23,13,31,26, 2, 8,18,12,29, 5,21,10, 3,24 ); 73 74 sBox: array[0..7] of array[0..63] of Byte = 75 ( ( 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 76 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 77 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 78 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 ), 79 80 ( 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 81 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 82 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 83 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 ), 84 85 ( 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 86 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 87 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 88 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 ), 89 90 ( 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 91 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 92 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 93 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 ), 94 95 ( 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 96 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 97 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 98 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 ), 99 100 ( 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 101 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 102 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 103 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 ), 104 105 ( 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 106 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 107 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 108 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 ), 109 110 ( 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 111 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 112 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 113 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 ) ); 114 115 BitPMC1: array[0..55] of Byte = 116 ( 56, 48, 40, 32, 24, 16, 8, 117 0, 57, 49, 41, 33, 25, 17, 118 9, 1, 58, 50, 42, 34, 26, 119 18, 10, 2, 59, 51, 43, 35, 120 62, 54, 46, 38, 30, 22, 14, 121 6, 61, 53, 45, 37, 29, 21, 122 13, 5, 60, 52, 44, 36, 28, 123 20, 12, 4, 27, 19, 11, 3 ); 124 125 BitPMC2: array[0..47] of Byte = 126 ( 13, 16, 10, 23, 0, 4, 127 2, 27, 14, 5, 20, 9, 128 22, 18, 11, 3, 25, 7, 129 15, 6, 26, 19, 12, 1, 130 40, 51, 30, 36, 46, 54, 131 29, 39, 50, 44, 32, 47, 132 43, 48, 38, 55, 33,

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MATLAB的矩阵运算发布时间:2022-07-18
下一篇:
【数学建模】MATLAB语法发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap