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;

  • 相关阅读:
    CentOS-7 本地yum源挂载
    VMware 安装 Centos7
    linux永久关闭SELinux*
    linux添加用户
    git分支常用命令
    git 常用命令
    springboot 自定义interceptor 对请求进行拦截
    ORACLE 远程导出DMP文件
    解决idea2018无法安装lombok的问题
    centos下快速安装mysql
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940597.html
Copyright © 2011-2022 走看看