如题:
function E_DES_Str(Str, Key: string): string;
var
  StrByte, OutByte, KeyByte: array[0..7] of Byte;
  I, j: Integer;
begin
  if (Length(Str) > 0) and (Ord(Str[Length(Str)]) = 0) then
    raise Exception.Create('Error: the last char is NULL char.');//这里为什么要进行验证??
  if Length(Key) < 8 then
    while Length(Key) < 8 do
      Key := Key + Chr(0);
  while Length(Str) mod 8 <> 0 do
    Str := Str + Chr(0);  for j := 0 to 7 do
    KeyByte[j] := Ord(Key[j + 1]);
  makeKey(KeyByte, subKey);  Result := '';  for I := 0 to Length(Str) div 8 - 1 do
  begin
    for j := 0 to 7 do
      StrByte[j] := Ord(Str[I * 8 + j + 1]);
    desData(dmEncry, StrByte, OutByte);
    for j := 0 to 7 do
      Result := Result + Chr(OutByte[j]);
  end;
end;
这是最常见的网上的Delphi对DES加密方法中的一个加密函数,该函数第一条语句就是验证串的结尾是不是一个空字符,为什么要这样验证呢?如果是空字符的话,对加密有什么影响吗?求解!!!

解决方案 »

  1.   

    为空的话后面的计算是不能进行下去的  Length(Str)=0  楼主可以跟踪下。
      

  2.   

    不是的,不是length(str)=0啊,是最后一位为空的时候,也就是说串的最后一位是空的情况的时候为什么有提示呢,是不是以空字符结尾的串不能加密呢?
      

  3.   

    我要关心的是,如果以空结尾的串,就不可以进行DES加密了吗?