zoukankan      html  css  js  c++  java
  • 字符串加密解密方法

      

    function Decrypt(Src: string; Key: string): string;
    var
      KeyLen, KeyPos, Offset, SrcPos, SrcAsc, TmpSrcAsc: Integer;
      Dest: string;
    begin
      KeyLen := Length(Key);
      if KeyLen = 0 then
        Key := cPasswordKey;
      KeyPos := 0;
      Offset := StrToInt('$' + Copy(Src, 1, 2));
      SrcPos := 3;
      while SrcPos < Length(Src) do
      begin
        SrcAsc := StrToInt('$' + Copy(Src, SrcPos, 2));
        if KeyPos < KeyLen then
          KeyPos := KeyPos + 1
         else
          KeyPos := 1;
        TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
        if TmpSrcAsc <= Offset then
          TmpSrcAsc := 255 + TmpSrcAsc - Offset
        else
          TmpSrcAsc := TmpSrcAsc - Offset;
        Dest := Dest + Chr(TmpSrcAsc);
        Offset := SrcAsc;
        SrcPos := SrcPos + 2;
      end;
      Result := Dest;
    end;


    function Encrypt(Src: string; Key: string): string;
    var
      KeyLen, KeyPos, Offset, SrcPos, SrcAsc: Integer;
      Dest: string;
    begin
      KeyLen := Length(Key);
      if KeyLen = 0 then
        Key := cPasswordKey;
      KeyPos := 0;
      Randomize;
      Offset := Random(256);
      Dest := Format('%1.2x', [Offset]);
      for SrcPos := 1 to Length(Src) do
      begin
        SrcAsc := (Ord(Src[SrcPos]) + Offset) mod 255;
        if KeyPos < KeyLen then
          KeyPos:= KeyPos + 1
        else
          KeyPos:=1;
        SrcAsc := SrcAsc xor Ord(Key[KeyPos]);
        Dest := Dest + Format('%1.2x', [SrcAsc]);
        Offset := SrcAsc;
      end;
      Result := Dest;
    end;

  • 相关阅读:
    网络负载均衡LVS
    JS 模仿红绿灯(控制台)
    【转】wrk 压力测试的 lua脚本
    linux开机 自动挂载和启动jar包
    【转】jprofiler linux配置需要监听的程序的端口
    时间复杂度总结
    Windows Subsystem for Linux (WSL) 安装
    敬畏用户
    Golang语言HTTP客户端实践
    Groovy入门常用语法
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940597.html
Copyright © 2011-2022 走看看