本代码目前使用的很好
现在想做一下改动:
1.加密后的字符串和原来的字符串长度相同。
2.加密后的字符串不包括小写字母及'-','=';
请指点一二:
unit Encryption;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,Math,StrUtils;const
{ Code Table of The Cipher Text }
CodeTable: array[0..63] of Char =
('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '-', '=' );
{ Min Length of Plain Text }
MIN_PLAIN_LONG = 12;
type
{ Encryption Type Set }
TEncryType = (etCommon,etExtend);
{ Encryption Class Define }
TEncryption = class
private
FKeyWord:string;
FEncryType:TEncryType;
FEncryTimes:integer;
{ Private declarations }
function CommonEncipher(PlainText:string):string; { Common Encipher Solution }
function ExtendEncipher(PlainText:string):string; { Extend Encipher Solution }
function CommonDecipher(CipherText:string):string; { Common Decipher Solution }
function ExtendDecipher(CipherText:string):string;
protected
{ Protected declarations }
public
{ Public declarations }
function Encipher(PlainText:string):string; { Encipher Function }
function Decipher(CipherText:string):string; { Decipher Function }
function SimpleEncrypt(code: string): string;
function SimpleDecrypt(code: string): string; { Extend Decipher Solution }
published
{ Published declarations }
property KeyWord : string read FKeyWord write FkeyWord; { Property : KeyWord }
property EncryType : TEncryType read FEncryType write FEncryType; { Property : Encry Type}
property EncryTimes : integer read FEncryTimes write FEncryTimes default 1; { Property : EncryTimes}
end;implementationfunction TEncryption.Encipher(PlainText:string):string;
var i_times:integer;
str_temp:string;
begin
try
str_temp := PlainText;
case FEncryType of
etCommon : for i_times := 1 to FEncryTimes do
str_temp := CommonEncipher(str_temp);
etExtend : for i_times := 1 to FEncryTimes do
str_temp := ExtendEncipher(str_temp);
end;
except
str_temp := '';
end;
Encipher := str_temp;
end;function TEncryption.Decipher(CipherText:string):string;
var i_times:integer;
str_temp:string;
begin
try
str_temp := CipherText;
case FEncryType of
etCommon : for i_times := 1 to FEncryTimes do
str_temp := CommonDecipher(str_temp);
etExtend : for i_times := 1 to FEncryTimes do
str_temp := ExtendDecipher(str_temp);
end;
except
str_temp := '';
end;
Decipher := str_temp;
end;
{ Power Function }
function MyPower(Base,Extend:integer):integer;
var i,j:integer;
begin
j := 1;
for i:=1 to extend do
j := j * base;
MyPower := j;
end;
{ Function to Get Index No of Char in Code Table }
function indexoftable(s:char):integer;
var t:integer;
begin
t := ord(s);
case s of
'0'..'9': t:= t - 48;
'a'..'z': t:= t - 61;
'A'..'Z': t:= t - 55;
'-' : t:= 62;
'=' : t:=63;
end;
indexoftable := t;
end;
{ Common Encipher Solution }
function TEncryption.CommonEncipher(PlainText:string):string;
var i,j,k:integer; { VAR for Cycle }
i_keylong:integer; { Long of Key }
i_PlainTextLong:integer; { Long of Plain Text }
str_temp:string; { Temp VAR to Store String }
bit_buffer:array of boolean; { Buffer of Plain Text }
key_buffer:array of boolean; { Buffer of Key Text }
i_8bit:integer; { 8bit in a Byte }
i_long1:integer; { VAR to Store Length of Plain Text }
begin
str_temp := PlainText;
{ Get Length of Plain Text }
i_PlainTextLong := Length(str_temp); { Deal with too short Plain Text }
if i_PlainTextLong<MIN_PLAIN_LONG then
begin
for i:=1 to MIN_PLAIN_LONG - i_PlainTextLong do
str_temp := str_temp + 'W';
i_PlainTextLong := MIN_PLAIN_LONG ;
end; { Set Plain Text Buffer }
if ((8*i_PlainTextLong)mod 6)<>0 then
k := 12 + 6 + 6*((8*i_PlainTextLong) div 6)
else
k := 12 + 8 * i_PlainTextLong; SetLength(bit_buffer,k); { Fill in Plain Text Buffer }
For i:=0 to i_PlainTextLong - 1 do
begin
i_8bit := ord(str_temp[i + 1]);
for j:=0 to 7 do
begin
bit_buffer[i*8 + j] := ((i_8bit Mod 2)=1);
i_8bit := (i_8bit div 2);
end;
end; { Set Key Text Buffer }
i_keylong := Length(FKeyWord);
SetLength(key_buffer, 8 * i_keylong); For i:=0 to i_keylong - 1 do
begin
i_8bit := ord(FKeyWord[i + 1]);
for j:=0 to 7 do
begin
key_buffer[i*8 + j] := ((i_8bit Mod 2)=1);
i_8bit := (i_8bit div 2);
end;
end; { Hide PlainText Long in the Code }
k := High(bit_buffer) + 1;
j := 7*Length(PlainText); i_long1 := ( j div 64 ); for i:=k - 12 to k - 7 do
begin
bit_buffer[i] := ((i_long1 Mod 2)=1);
i_long1 := (i_long1 div 2);
end; i_long1 := ( j mod 64 );
for i:= k - 6 to k - 1 do
begin
bit_buffer[i] := ((i_long1 Mod 2)=1);
i_long1 := (i_long1 div 2);
end; { Compute Converse Modul Adding of 2 }
for i := k - 1 downto 0 do
begin
if key_buffer[ ((k - 1 -i) mod (8*i_keylong))] then
bit_buffer[i] := not bit_buffer[i];
end; { Compute Modul Adding of 2 , Get Cipher Text} str_temp := '';
i_8bit := 0; for i := 0 to k -1 do
begin
if key_buffer[ (i mod (8*i_keylong))] then
bit_buffer[i] := not bit_buffer[i];
if bit_buffer[i] then
i_8bit := i_8bit + MyPower(2,(i mod 6));
if (i mod 6)=5 then
begin
str_temp := str_temp + CodeTable[i_8bit];
i_8bit := 0;
end;
end;
CommonEncipher := str_temp ;
end;{ Common Decipher Solution }
现在想做一下改动:
1.加密后的字符串和原来的字符串长度相同。
2.加密后的字符串不包括小写字母及'-','=';
请指点一二:
unit Encryption;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,Math,StrUtils;const
{ Code Table of The Cipher Text }
CodeTable: array[0..63] of Char =
('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '-', '=' );
{ Min Length of Plain Text }
MIN_PLAIN_LONG = 12;
type
{ Encryption Type Set }
TEncryType = (etCommon,etExtend);
{ Encryption Class Define }
TEncryption = class
private
FKeyWord:string;
FEncryType:TEncryType;
FEncryTimes:integer;
{ Private declarations }
function CommonEncipher(PlainText:string):string; { Common Encipher Solution }
function ExtendEncipher(PlainText:string):string; { Extend Encipher Solution }
function CommonDecipher(CipherText:string):string; { Common Decipher Solution }
function ExtendDecipher(CipherText:string):string;
protected
{ Protected declarations }
public
{ Public declarations }
function Encipher(PlainText:string):string; { Encipher Function }
function Decipher(CipherText:string):string; { Decipher Function }
function SimpleEncrypt(code: string): string;
function SimpleDecrypt(code: string): string; { Extend Decipher Solution }
published
{ Published declarations }
property KeyWord : string read FKeyWord write FkeyWord; { Property : KeyWord }
property EncryType : TEncryType read FEncryType write FEncryType; { Property : Encry Type}
property EncryTimes : integer read FEncryTimes write FEncryTimes default 1; { Property : EncryTimes}
end;implementationfunction TEncryption.Encipher(PlainText:string):string;
var i_times:integer;
str_temp:string;
begin
try
str_temp := PlainText;
case FEncryType of
etCommon : for i_times := 1 to FEncryTimes do
str_temp := CommonEncipher(str_temp);
etExtend : for i_times := 1 to FEncryTimes do
str_temp := ExtendEncipher(str_temp);
end;
except
str_temp := '';
end;
Encipher := str_temp;
end;function TEncryption.Decipher(CipherText:string):string;
var i_times:integer;
str_temp:string;
begin
try
str_temp := CipherText;
case FEncryType of
etCommon : for i_times := 1 to FEncryTimes do
str_temp := CommonDecipher(str_temp);
etExtend : for i_times := 1 to FEncryTimes do
str_temp := ExtendDecipher(str_temp);
end;
except
str_temp := '';
end;
Decipher := str_temp;
end;
{ Power Function }
function MyPower(Base,Extend:integer):integer;
var i,j:integer;
begin
j := 1;
for i:=1 to extend do
j := j * base;
MyPower := j;
end;
{ Function to Get Index No of Char in Code Table }
function indexoftable(s:char):integer;
var t:integer;
begin
t := ord(s);
case s of
'0'..'9': t:= t - 48;
'a'..'z': t:= t - 61;
'A'..'Z': t:= t - 55;
'-' : t:= 62;
'=' : t:=63;
end;
indexoftable := t;
end;
{ Common Encipher Solution }
function TEncryption.CommonEncipher(PlainText:string):string;
var i,j,k:integer; { VAR for Cycle }
i_keylong:integer; { Long of Key }
i_PlainTextLong:integer; { Long of Plain Text }
str_temp:string; { Temp VAR to Store String }
bit_buffer:array of boolean; { Buffer of Plain Text }
key_buffer:array of boolean; { Buffer of Key Text }
i_8bit:integer; { 8bit in a Byte }
i_long1:integer; { VAR to Store Length of Plain Text }
begin
str_temp := PlainText;
{ Get Length of Plain Text }
i_PlainTextLong := Length(str_temp); { Deal with too short Plain Text }
if i_PlainTextLong<MIN_PLAIN_LONG then
begin
for i:=1 to MIN_PLAIN_LONG - i_PlainTextLong do
str_temp := str_temp + 'W';
i_PlainTextLong := MIN_PLAIN_LONG ;
end; { Set Plain Text Buffer }
if ((8*i_PlainTextLong)mod 6)<>0 then
k := 12 + 6 + 6*((8*i_PlainTextLong) div 6)
else
k := 12 + 8 * i_PlainTextLong; SetLength(bit_buffer,k); { Fill in Plain Text Buffer }
For i:=0 to i_PlainTextLong - 1 do
begin
i_8bit := ord(str_temp[i + 1]);
for j:=0 to 7 do
begin
bit_buffer[i*8 + j] := ((i_8bit Mod 2)=1);
i_8bit := (i_8bit div 2);
end;
end; { Set Key Text Buffer }
i_keylong := Length(FKeyWord);
SetLength(key_buffer, 8 * i_keylong); For i:=0 to i_keylong - 1 do
begin
i_8bit := ord(FKeyWord[i + 1]);
for j:=0 to 7 do
begin
key_buffer[i*8 + j] := ((i_8bit Mod 2)=1);
i_8bit := (i_8bit div 2);
end;
end; { Hide PlainText Long in the Code }
k := High(bit_buffer) + 1;
j := 7*Length(PlainText); i_long1 := ( j div 64 ); for i:=k - 12 to k - 7 do
begin
bit_buffer[i] := ((i_long1 Mod 2)=1);
i_long1 := (i_long1 div 2);
end; i_long1 := ( j mod 64 );
for i:= k - 6 to k - 1 do
begin
bit_buffer[i] := ((i_long1 Mod 2)=1);
i_long1 := (i_long1 div 2);
end; { Compute Converse Modul Adding of 2 }
for i := k - 1 downto 0 do
begin
if key_buffer[ ((k - 1 -i) mod (8*i_keylong))] then
bit_buffer[i] := not bit_buffer[i];
end; { Compute Modul Adding of 2 , Get Cipher Text} str_temp := '';
i_8bit := 0; for i := 0 to k -1 do
begin
if key_buffer[ (i mod (8*i_keylong))] then
bit_buffer[i] := not bit_buffer[i];
if bit_buffer[i] then
i_8bit := i_8bit + MyPower(2,(i mod 6));
if (i mod 6)=5 then
begin
str_temp := str_temp + CodeTable[i_8bit];
i_8bit := 0;
end;
end;
CommonEncipher := str_temp ;
end;{ Common Decipher Solution }
function TEncryption.CommonDecipher(CipherText:string):string;
var i,j,k:integer; { Temp Integer VAR }
i_keylong:integer; { Long of Key }
i_CipherTextLong:integer; { Long of Cipher Text }
i_PlainTextLong:integer; { Long of Cipher Text }
str_temp:string; { Temp String }
bit_buffer:array of boolean; { Buffer of Cipher Text }
key_buffer:array of boolean; { Buffer of Key Text }
i_8bit:integer; { 8bit in a Byte. (VAR)}
i1,i2:integer;
begin { Set Key Text Buffer }
i_keylong := Length(FKeyWord);
SetLength(key_buffer, 8 * i_keylong); For i:=0 to i_keylong - 1 do
begin
i_8bit := ord(FKeyWord[i + 1]);
for j:=0 to 7 do
begin
key_buffer[i*8 + j] := ((i_8bit Mod 2)=1);
i_8bit := (i_8bit div 2);
end;
end; { Set Cipher Text Buffer }
str_temp := CipherText;
i_CipherTextLong := Length(CipherText); k := 6 * i_CipherTextLong;
SetLength(bit_buffer,k); For i:=0 to i_CipherTextLong - 1 do
begin
i_8bit := Indexoftable(str_temp[i + 1]);
for j:=0 to 5 do
begin
bit_buffer[i*6 + j] := ((i_8bit mod 2)=1);
i_8bit := i_8bit div 2;
end;
end; { Compute Modul Adding of 2 , Get Cipher Text}
for i := 0 to k - 1 do
begin
if key_buffer[(i mod (8*i_keylong))] then
bit_buffer[i] := not bit_buffer[i];
end;
{ Compute Converse Modul Adding of 2 }
for i := k - 1 downto 0 do
begin
if key_buffer[ ((k - 1 -i) mod (8*i_keylong))] then
bit_buffer[i] := not bit_buffer[i];
end; { Get Length of Plain Text }
i1 := 0;
for i := k -12 to k - 7 do
begin
if bit_buffer[i] then
i1 := i1 + MyPower(2,( i + 12 - k));
end; i2 := 0;
for i := k - 6 to k - 1 do
begin
if bit_buffer[i] then
i2 := i2 + MyPower(2,( i + 6 - k ));
end;
i1 := 64*i1 + i2;
i1 := i1 div 7; i_PlainTextLong := i1; { Get Plain Text }
str_temp := '';
i_8bit := 0;
for i := 0 to 6 * i_CipherTextLong -1 do
begin
if bit_buffer[i] then
i_8bit := i_8bit + MyPower(2,(i mod 8));
if (i mod 8)=7 then
begin
str_temp := str_temp + Chr(i_8bit);
i_8bit := 0;
end;
end; str_temp := Copy(str_temp,1,i_PlainTextLong);
{ Return }
CommonDecipher := str_temp ;
end;function TEncryption.ExtendEncipher(PlainText:string):string;
begin
ExtendEncipher := PlainText ;
end;function TEncryption.ExtendDecipher(CipherText:string):string;
begin
ExtendDecipher := CipherText;
end;
end.
楼主为什么有这样的要求呢?要想不增加长度干脆换个算法吧,我觉得不可能的。还有就是你不想要"="和"-",但是你总需要占位符不是?那就把"="和"-"改成其他的符号吧。