0
我寫了一個小程序,用於加密和解密用戶給出的一些字符串。解密並加密循環中的字符串
加密功能正在工作!但現在我必須解密它。我的問題是我無法真正想象如何實現這一步。
這裏是我的加密代碼:
function encrypt(origin: string; cols: byte; fillChar: char): string;
var
a, c, d, e, rest, restfill, langewort: integer;
temp1, temp2: string;
begin
langewort := length(origin);
rest := length(origin) mod cols;
restfill := cols - rest;
if (rest = 0) then
restfill := cols - 1
else
begin
for c := 1 to restfill do
begin
origin := origin + fillChar;
end;
end;
temp1 := '';
for d := 1 to cols do
begin
for e := 0 to restfill - 1 do
begin
temp1 := temp1 + origin[d + cols * e];
end;
end;
encrypt := temp1;
end;
比方說我們用這個字符串Stringnum,加密之後,我們有這樣的:Snmtg $ RN $ IU $ 解密函數必須扭轉它和刪除fillChar。
我會先從:
function decrypt(origin: string; cols: byte; fillChar: char): string;
var
a, c, d, e, rest, restfill, langewort: integer;
temp1, temp2: string;
begin
langewort := length(origin);
rest := length(origin) mod cols;
restfill := cols - rest;
if (rest = 0) then
restfill := cols - 1
else
begin
for c := 1 to restfill do
begin
origin := origin + fillChar;
end;
end;
temp1 := '';
for d := 1 to cols do
begin
for e := 0 to restfill - 1 do
begin
temp1 := temp1 + origin[d - cols * e]; //<- change made here ?
end;
end;
decrypt := temp1;
end;
你試過了嗎?出了什麼問題?請準確地添加你想要做的事情,以及爲什麼你發佈的代碼不起作用,或者將解密函數標記爲僞代碼(如果是這樣的話)。 –
解密方法的問題在於,其餘變量與加密函數中的變量不同,因爲它是根據原始字符串的長度計算的,原始字符串由於在加密過程中添加了更多字符而變得更大。所以你應該保留加密字符串中的一個字符來存儲原始字符串的長度。 – SilverWarior
第一步是學習文本和二進制文件的區別 –