自己獨立設計的字符串加密算法
作者:成曉旭
這是本人自行設計的第一個數據加密算法,當初是想設計成分組的對稱加密算法,但後來工作一忙,就沒有實現,就草草完成便開始應用起來了。但是目前的混淆度已經能夠滿足絕大多數的簡單加密應用。
1、 簡介:
設計一個算法,對字符串進行加密和解決,要求有一定的混淆度,並且,無須復雜的密鑰管理。為簡化應用,我將混淆碼(類似於密鑰)直接混淆存儲在加密後密文中。
2、 算法設計:
開始想將此算法設計成分組的對稱加密算法,所以在算法的核心處理輪盡可能地選擇“對稱”的計算方法:比較異或、字節內轉換、對稱倒。
此算法設計成三大步驟:輸入調整,核心加、解密過程,輸出調整。
1、 輸入調整:就是用混淆碼將源串調整成長度為8的整數倍的新串,以作為核心處理過程的輸入。
2、 輸出調整:就是將通過核心處理過程計算後的結果串,按輸出要求進行轉換。加密時,就是轉換成希望的密文串;解密時,轉換成希望的明文串。
3、 核心處理過程:是整個算法的核心過程,主要包括位異或、左右倒置、交換字節、移位等幾個子過程。
3.1:位異或:對串的每一個Byte位進行異或運算;
3.2:左右倒置:對串進行鏡像對稱處理,將串分成前後兩部分,完成對換。
3.3:交換字節:對每一個Byte的前、後半字節進行鏡像對稱倒置。
3.4:移位:對串進行移位處理。
具體的加、解密處理過程如下圖所示:
3、 算法點評:
1、 此算法沒有實現正在的對稱加密算法。只需要對輸入、輸出調整過程進行優化,並且,重新調整核心處理過程的計算順序,就可以了。
2、 源碼演示的版本有些缺陷:最大處理串長度為255,應該進行優化;並且混淆碼過於粗糙,也有待優化;對混淆碼的應用不全面,處理之後,混淆碼主要集中在密文的部分區段,分布不夠均勻,混淆程度也不夠充分。
4、 算法源碼:
//------------------------------------------------------------------------------
//
// 產品名稱:自有版權的字符串加密算法
// 產品簡介:將字符串按自行獨立設計的加密算法進行加、解密處理
// 產品作者:成曉旭
// E-Main: CXXSoft@sohu.com
// 產品版本:1.0版
// 版權所有:成曉旭
// 備註: 任何人使用此類時,請保留此段自述文件,謝謝!
// 單元文件:unSecurity.pas
// 單元說明:算法類TCXXStrSecurity的定義及實現
// 開發時間:2004-12-25
// 設計本加、解密算法,並用原型程序測試、實現
// 修改時間:2005-01-15
// 增加加、解密返回、輸入串碼制屬性更新功能
//------------------------------------------------------------------------------
unit unSecurity;
interface
uses
SysUtils;
type
TCXXStrSecurity = class
private
//本算法的加密最終結果標誌(true:字節碼串/false:字符串)
isByteResult:boolean;
//本算法的字符串長度位數目(16制式)
lenStrWidth:Byte;
//本算法要求的最小模糊字符串
minTextLen:Word;
//本算法處理的最大串長度
maxStrLen:Word;
//本算法的串移位位數
bitStrMoved:Byte;
//根據本算法的處理規則,以加密前明文進行串調整
function TransFillText(const strText:string):string;
//根據本算法的處理規則,以加密後密文進行串調整
function ReTransFillText(const strText:string; const mvSize: Byte):string;
//將字符串轉換成Ascii碼串的方法
function TransStringToNumber(const strText:string):string;
//將一個字節的前後兩個半字節互換的方法
function ChangeNumber(const byt:Byte):Byte;
//將字符串循環移動的方法(左移/右移)
function MoveTextByCircle(const strText:string;const mvSize:Byte;const isFromHead:boolean):string;
//將字符串內每個字節的前後兩個半字節互換的方法
function ExChangeNumber(const strText:string):string;
//將字符串進行前後倒置的方法
function RevertString(const strText:string):string;
//將字符串的相鄰兩位進行調換的方法
function TransOneByte(const strText:string):string;
//將Ascii碼串轉換後常規字符串的方法
function TransNumberToString(const strText:string):string;
//將字符串進行位異或處理方法
function XORString(const strText:string):string;
public
//本算法的加、解密處理成功標誌(true:成功,否則:失敗)
isOK:boolean;
//本算法的處理過程消息
Msg:string;
constructor Create(const isReturnByte:boolean);
//字符串加密方法
function EncodeString(const strText:string):string;overload;
//字符串加密方法
function EncodeString(const strText:string;const isByteStr:boolean):string;overload;
//字符串解密方法
function DecodeString(const strPassword:string):string;overload;
//字符串解密方法
function DecodeString(const strPassword:string;const isByteStr:boolean):string;overload;
end;
implementation
...{ TCXXStrSecurity }
function TCXXStrSecurity.ChangeNumber(const byt: Byte): Byte;
begin
Result := (byt mod 16) * 16 + (byt div 16);
end;
constructor TCXXStrSecurity.Create(const isReturnByte:boolean);
const
default_MoveBit = 5;
begin
minTextLen := 6;
lenStrWidth := 2;
maxStrLen := 255;
bitStrMoved := 5;
isByteResult := isReturnByte;
end;
function TCXXStrSecurity.EncodeString(const strText: string): string;
var
str:string;
begin
str := '';
str := TransFillText(strText);
str := XORString(str);
str := RevertString(str);
str := TransOneByte(str);
str := TransStringToNumber(str);
str := ExChangeNumber(str);
str := MoveTextByCircle(str,bitStrMoved,true);
if NOT isByteResult then
str := TransNumberToString(str);
Result := str;
end;
function TCXXStrSecurity.ExChangeNumber(const strText: string): string;
var
len,i:Word;
begin
len := Length(strText);
for i := 0 to len div 2 - 1 do
begin
Result := Result + IntToHex(ChangeNumber(StrToInt('$'+Copy(strText,i*2+1,2))),2);
end;
end;
function TCXXStrSecurity.MoveTextByCircle(const strText: string;
const mvSize: Byte; const isFromHead: boolean): string;
var
len:Word;
begin
len := Length(strText);
if isFromHead then
Result := Copy(strText,mvSize+1,len-mvSize) + Copy(strText,1,mvSize)
else
Result := Copy(strText,len-mvSize+1,mvSize) + Copy(strText,1,len-mvSize);
end;
function TCXXStrSecurity.DecodeString(const strPassword: string): string;
var
str:string;
begin
str := strPassword;
if NOT isByteResult then
str := TransStringToNumber(str);
str := MoveTextByCircle(str,bitStrMoved,false);
str := ExChangeNumber(str);
str := TransNumberToString(str);
str := TransOneByte(str);
str := RevertString(str);
str := XORString(str);
str := ReTransFillText(str,bitStrMoved);
Result := str;
end;
function TCXXStrSecurity.ReTransFillText(const strText: string; const mvSize: Byte): string;
var
len:Word;
begin
len := StrToInt('$'+Copy(strText,1,lenStrWidth));
Result := Copy(strText,lenStrWidth+1,len);
end;
function TCXXStrSecurity.RevertString(const strText: string): string;
var
i,len:word;
t:char;
pch:PChar;
begin
pch := PChar(strText);
len := Length(strText);
for i := 0 to len div 2 -1 do
begin
//ChangeChar(pch[i],pch[len-1-i]);
t := pch[i];
pch[i] := pch[len-1-i];
pch[len-1-i] := t;
end;
Result := String(pch);
end;
function TCXXStrSecurity.TransFillText(const strText: string): string;
var
i,oLen:Word;
str,strPower:string;
begin
strPower := FormatDateTime('HHMMSS',Now());
//strPower := RevertString(strPower);
//strPower := TransOneByte(strPower);
str := strText;
oLen := Length(str);
i := 1;
while(Length(str) < minTextLen) do
begin
str := str + strPower[i];
Inc(i);
end;
Result := IntToHex(oLen,lenStrWidth)+str;
end;
function TCXXStrSecurity.TransNumberToString(const strText: string): string;
var
i:word;
begin
Result := '';
for i := 0 to Length(strText) div 2 - 1 do
begin
Result := Result +CHR(StrToInt('$'+Copy(strText,i*2+1,2)));
end;
end;
function TCXXStrSecurity.TransOneByte(const strText: string): string;
var
i,len:word;
t:char;
pch:PChar;
begin
pch := PChar(strText);
len := Length(strText);
for i := 0 to len div 2 - 1 do
begin
t := pch[2*i];
pch[2*i] := pch[2*i+1];
pch[2*i+1] := t;
end;
Result := String(pch);
end;
function TCXXStrSecurity.TransStringToNumber(const strText: string): string;
var
len,i:Word;
str:string;
begin
len := Length(strText);
str := '';
for i := 1 to len do
begin
str := str + IntToHex(Ord(strText[i]),2);
end;
Result := str;
end;
function TCXXStrSecurity.XORString(const strText: string): string;
var
len,k:word;
b:Byte;
begin
Result := '';
len := Length(strText);
for k := 1 to len do
begin
b := Ord(strText[k]);
if k mod 2 =0 then
b := b xor k
else
b := b xor (len-k);
Result := Result + CHR(b);
end;
end;
function TCXXStrSecurity.DecodeString(const strPassword: string;
const isByteStr: boolean): string;
begin
isByteResult := isByteStr;
Result := DecodeString(strPassword);
end;
function TCXXStrSecurity.EncodeString(const strText: string;
const isByteStr: boolean): string;
begin
isByteResult := isByteStr;
Result := EncodeString(strText);
end;
end.
5、 應用適應器源碼:
//------------------------------------------------------------------------------
//
// 產品名稱:自有版權的字符串加密算法
// 產品簡介:將字符串按自行獨立設計的加密算法進行加、解密處理
// 產品作者:成曉旭
// E-Main: CXXSoft@sohu.com
// 產品版本:1.0版
// 版權所有:成曉旭
// 備註: 任何人使用此類時,請保留此段自述文件,謝謝!
// 單元文件:unSecurityAdapter.pas
// 單元說明:算法接口類TCXXStrSecurity的定義及實現
// 開發時間:2006-06-27
// 增加接口類,封閉對算法類的管理細節,以方便客戶使用
//------------------------------------------------------------------------------
unit unSecurityAdapter;
interface
uses
unSecurity;
type
TSecurityAdapter = class
private
public
//字符串加密方法
class function EncodeString(const strText:string):string;
//字符串解密方法
class function DecodeString(const strPassword:string):string;
end;
implementation
var
security:TCXXStrSecurity;
...{ TSecurityAdapter }
class function TSecurityAdapter.DecodeString(
const strPassword: string): string;
begin
Result := '';
if Assigned(security) then
Result := security.DecodeString(strPassword,true);
end;
class function TSecurityAdapter.EncodeString(const strText: string): string;
begin
Result := '';
if Assigned(security) then
Result := security.EncodeString(strText,true);
end;
initialization
security := TCXXStrSecurity.Create(false);
finalization
security.Free();
security := nil;
end.
免責聲明:本文為網絡用戶發布,其觀點僅代表作者個人觀點,與本站無關,本站僅提供信息存儲服務。文中陳述內容未經本站證實,其真實性、完整性、及時性本站不作任何保證或承諾,請讀者僅作參考,並請自行核實相關內容。