⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ciphers.html

📁 delphi下一整套加密组件
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    <p>This example shows how you can encrypt the contents of a TMemo and leave the contents printable.
    <pre>
  <b>procedure</b> TForm1.btnEncryptClick(Sender: TObject);
  <b>var</b>
    i: <b>integer</b>;
    Cipher: TDCP_rc4;
    KeyStr: string;
  <b>begin</b>
    KeyStr:= '';
    <b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b>  <em>// get the passphrase</em>
    <b>begin</b>
      Cipher:= TDCP_rc4.Create(Self);
      Cipher.InitStr(KeyStr,TDCP_sha1);         <em>// initialize the cipher with a hash of the passphrase</em>
      <b>for</b> i:= 0 <b>to</b> Memo1.Lines.Count-1 <b>do</b>       <em>// encrypt the contents of the memo</em>
        Memo1.Lines[i]:= Cipher.EncryptString(Memo1.Lines[i]);
      Cipher.Burn;
      Cipher.Free;
    <b>end</b>;
  <b>end</b>;
  
  <b>procedure</b> TForm1.btnDecryptClick(Sender: TObject);
  <b>var</b>
    i: <b>integer</b>;
    Cipher: TDCP_rc4;
    KeyStr: string;
  <b>begin</b>
    KeyStr:= '';
    <b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b>  <em>// get the passphrase</em>
    <b>begin</b>
      Cipher:= TDCP_rc4.Create(Self);
      Cipher.InitStr(KeyStr,TDCP_sha1);         <em>// initialize the cipher with a hash of the passphrase</em>
      <b>for</b> i:= 0 <b>to</b> Memo1.Lines.Count-1 <b>do</b>       <em>// decrypt the contents of the memo</em>
        Memo1.Lines[i]:= Cipher.DecryptString(Memo1.Lines[i]);
      Cipher.Burn;
      Cipher.Free;
    <b>end</b>;
  <b>end</b>;
    </pre>
    <hr>
    <p><font size="+2"><a name="Example2">Example 2: File encryption</a></font>
    <p>This example shows how you can encrypt the contents of a file, takes the input and output file names from two edit boxes: boxInputFile and boxOutputFile.
    <pre>
  <b>procedure</b> TForm1.btnEncryptClick(Sender: TObject);
  <b>var</b>
    Cipher: TDCP_rc4;
    KeyStr: string;
    Source, Dest: TFileStream;
  <b>begin</b>
    KeyStr:= '';
    <b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b>  <em>// get the passphrase</em>
    <b>begin</b>
      <b>try</b>
        Source:= TFileStream.Create(boxInputFile.Text,fmOpenRead);
        Dest:= TFileStream.Create(boxOutputFile.Text,fmCreate);
        Cipher:= TDCP_rc4.Create(Self);
        Cipher.InitStr(KeyStr,TDCP_sha1);              <em>// initialize the cipher with a hash of the passphrase</em>
        Cipher.EncryptStream(Source,Dest,Source.Size); <em>// encrypt the contents of the file</em>
        Cipher.Burn;
        Cipher.Free;
        Dest.Free;
        Source.Free;
        MessageDlg('File encrypted',mtInformation,[mbOK],0);
      <b>except</b>
        MessageDlg('File IO error',mtError,[mbOK],0);
      <b>end</b>;
    <b>end</b>;
  <b>end</b>;
  
  <b>procedure</b> TForm1.btnDecryptClick(Sender: TObject);
  <b>var</b>
    Cipher: TDCP_rc4;
    KeyStr: string;
    Source, Dest: TFileStream;
  <b>begin</b>
    KeyStr:= '';
    <b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b>  <em>// get the passphrase</em>
    <b>begin</b>
      <b>try</b>
        Source:= TFileStream.Create(boxInputFile.Text,fmOpenRead);
        Dest:= TFileStream.Create(boxOutputFile.Text,fmCreate);
        Cipher:= TDCP_rc4.Create(Self);
        Cipher.InitStr(KeyStr,TDCP_sha1);              <em>// initialize the cipher with a hash of the passphrase</em>
        Cipher.DecryptStream(Source,Dest,Source.Size); <em>// decrypt the contents of the file</em>
        Cipher.Burn;
        Cipher.Free;
        Dest.Free;
        Source.Free;
        MessageDlg('File decrypted',mtInformation,[mbOK],0);
      <b>except</b>
        MessageDlg('File IO error',mtError,[mbOK],0);
      <b>end</b>;
    <b>end</b>;
  <b>end</b>;
    </pre>
    <hr>
    <p><font size="+2"><a name="Example3">Example 3: General encryption</a></font>
    <p>This hypothetical example shows how you might encrypt a packet of information before transmission across a network.
    <pre>
  <b>type</b>
    TSomePacket= <b>record</b>
      Date: <b>double</b>;
      ToUserID: <b>integer</b>;
      FromUserID: <b>integer</b>;
      MsgLen: <b>integer</b>;
      Msg: string;
    <b>end</b>;
    
  <b>procedure</b> EncryptPacket(Cipher: TDCP_cipher; <b>var</b> Packet: TSomePacket);
  <em>// encrypt the information packet with the cipher
  // if the cipher isn't initialized then prompt for passphrase</em>
  <b>begin</b>
    <b>if</b> Cipher= <b>nil then</b>
      <b>raise</b> Exception.Create('Cipher hasn''t been created!')
    <b>else
    begin</b>
      <b>if not</b> Cipher.Initialized <b>then</b>        <em>// check the cipher has been initialized</em>
        Cipher.InitStr(InputBox('Passphrase','Enter passphrase',''),TDCP_sha1);
      <b>if</b> Cipher <b>is</b> TDCP_blockcipher <b>then</b>    <em>// if a block cipher use CFB 8bit as encrypting small packets</em>
        TDCP_blockcipher(Cipher).CipherMode:= cmCFB8bit; 
      <em>// encrypt the record part by part, could do this in one go if it was a packed record</em>
      Cipher.Encrypt(Packet.Date,Packet.Date,Sizeof(Packet.Date));  
      Cipher.Encrypt(Packet.ToUserID,Packet.ToUserID,Sizeof(Packet.ToUserID));
      Cipher.Encrypt(Packet.FromUserID,Packet.FromUserID,Sizeof(Packet.FromUserID));
      Cipher.Encrypt(Packet.MsgLen,Packet.MsgLen,Sizeof(Packet.MsgLen));
      Cipher.Encrypt(Packet.Msg[1],Packet.Msg[1],Length(Packet.Msg));  <em>// slightly different for strings</em>
      <em>// don't bother resetting the cipher, instead keep the chaining information</em>
    <b>end</b>;  
  <b>end</b>;
    </pre>
    <p>&nbsp;
    <p><a href="Index.html">Index</a>, <a href="BlockCiphers.html">Block Ciphers</a>, <a href="Hashes.html">Hashes</a>
    <p>&nbsp;
    <p><em>DCPcrypt is copyrighted &copy; 1999-2002 David Barton.<br>
       All trademarks are property of their respective owners.</em>
  </body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -