字符串与数字的转换

string conversion with number

shift函数应该根据给定的数字移位字符

   function shift_char(c: Character; n:Integer) return Character is
      I : Integer;
   begin
        if Is_Lower(c) then
            I:= ((((Character'Pos(c) rem 26) + n) mod 26) + 97);
        elsif Is_Upper(c) then
            I:= ((((Character'Pos(c) rem 26) + n) mod 26) + 65);
        end if;

      return Character'val(I);
   end;

如前所述here,您首先需要将每个范围中的最低字符映射到零。代替 rem 26 减去 初始字符的位置,如下面的变体所示:

function Shift_Char (C : Character; N : Integer) return Character is
   Lower_A : constant Integer := Character'Pos (Lower'First);
   Upper_A : constant Integer := Character'Pos (Upper'First);
   I       : Integer          := Character'Pos (C);
begin
   if Is_Lower (C) then
      I := ((((I - Lower_A) + N) mod 26) + Lower_A);
   end if;
   if Is_Upper (C) then
      I := ((((I - Upper_A) + N) mod 26) + Upper_A);
   end if;
   return Character'Val (I);
end Shift_Char;

控制台:

generated single_key is : 25
generated multi_keys list is : 16 8 25 11

before encryption with caesar: Ada Is Cool
encrypted:Zcz Hr Bnnk
decrypted: Ada Is Cool
before encryption with vigenere: Ada Is Cool
encrypted: Qlz Ti Knzb
decrypted: Ada Is Cool