如何加密RTF格式的文件,只能在程序中再解密打开浏览(100分)

  • 主题发起人 主题发起人 chen202
  • 开始时间 开始时间
下面的Demo可以满足你的要求吗?<br><br>unit EncryptIt;<br><br>interface<br>uses<br>&nbsp; &nbsp; Classes;<br>const<br>&nbsp; &nbsp; &nbsp;C1 = 52845;<br>&nbsp; &nbsp; &nbsp;C2 = 22719;<br><br>function Encrypt(const S: String; Key: Word): String;<br>function Decrypt(const S: String; Key: Word): String;<br>procedure EncryptFile(INFName, OutFName : String; Key : Word);<br>procedure DecryptFile(INFName, OutFName : String; Key : Word);<br><br>implementation<br><br>function Encrypt(const S: String; Key: Word): String;<br>var<br>&nbsp; I: Integer;<br>begin<br>&nbsp; Result := S;<br>&nbsp; for I := 1 to Length(S) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Result := char(byte(S) xor (Key shr 8));<br>&nbsp; &nbsp; &nbsp; Key := (byte(Result) + Key) * C1 + C2;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>function Decrypt(const S: String; Key: Word): String;<br>var<br>&nbsp; I: Integer;<br>begin<br>&nbsp; Result := S;<br>&nbsp; for I := 1 to Length(S) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Result := char(byte(S) xor (Key shr 8));<br>&nbsp; &nbsp; &nbsp; Key := (byte(S) + Key) * C1 + C2;<br>&nbsp; &nbsp; end;<br>end;<br><br><br>procedure EncryptFile(INFName, OutFName : String; Key : Word);<br>var<br>&nbsp; MS, SS : TMemoryStream;<br>&nbsp; X : Integer;<br>&nbsp; C : Byte;<br>begin<br>&nbsp; MS := TMemoryStream.Create;<br>&nbsp; SS := TMemoryStream.Create;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; MS.LoadFromFile(INFName);<br>&nbsp; &nbsp; &nbsp; MS.Position := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; for X := 0 TO MS.Size - 1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MS.Read(C, 1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C := (C xor (Key shr 8));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Key := (C + Key) * C1 + C2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SS.Write(C,1);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; SS.SaveToFile(OutFName);<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; SS.Free;<br>&nbsp; &nbsp; &nbsp; MS.Free;<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure DecryptFile(INFName, OutFName : String; Key : Word);<br>var<br>&nbsp; MS, SS : TMemoryStream;<br>&nbsp; X : Integer;<br>&nbsp; C, O : Byte;<br>begin<br>&nbsp; MS := TMemoryStream.Create;<br>&nbsp; SS := TMemoryStream.Create;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; MS.LoadFromFile(INFName);<br>&nbsp; &nbsp; &nbsp; MS.Position := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; for X := 0 to MS.Size - 1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MS.Read(C, 1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; O := C;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C := (C xor (Key shr 8));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Key := (O + Key) * C1 + C2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SS.Write(C,1);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; SS.SaveToFile(OutFName);<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; SS.Free;<br>&nbsp; &nbsp; &nbsp; MS.Free;<br>&nbsp; &nbsp; end;<br>end;<br><br>end.<br>
 
[^]<br>呵呵,应该可以的,不过速度比较慢哦。
 
用二进制方式更改rtf的文件头,随便改成什么,那就打不开了,然后想读的时候再改回去。
 
good !<br>Thanks
 
不好意思,忘了给你加了,<br>速度确实比较慢,有快一些的方法吗?
 
为何文件中的图形经过加密、解密后显示不出来呢?<br>如何才能解决?
 
后退
顶部