unit uEncrypt_Decrypt;
 
interface
 
uses SysUtils;
 
const XorKey: array[0..7] of Byte = ($B2, $09, $AA, $55, $93, $6D, $84, $47);
 
//通过密钥Key加密
function EncryptString(Source, Key: string): string;
function UnEncryptString(Source, Key: string): string;
 
//异或加密
function Enc(str: string): string;
function Dec(str: string): string;
 
 
implementation
 
function EncryptString(Source, Key: string): string;
var
  KeyLen,KeyPos,Offset,SrcPos,SrcAsc,Range: integer;
  Dest: string;
begin
  KeyLen := Length(Key);
  if KeyLen = 0 then
    Key := 'delphi';
  KeyPos := 0;
  Range := 256;
  randomize;
  Offset := random(Range);
  Dest := format('%1.2x', [Offset]);
  for SrcPos := 1 to Length(Source) do
  begin
    SrcAsc := (Ord(Source[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;
 
function UnEncryptString(Source, Key: string): string;
var
  KeyLen,KeyPos,Offset,SrcPos,SrcAsc,TmpSrcAsc: integer;
  Dest: string;
begin
  KeyLen := Length(Key);
  if KeyLen = 0 then
    Key := 'delphi';
  KeyPos := 0;
  Offset := strtoint('$' + copy(Source, 1, 2));
  SrcPos := 3;
  repeat
    SrcAsc := strtoint('$' + copy(Source, 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;
  until
    SrcPos >= Length(Source);
  result := Dest;
end;
 
function Enc(str: string): string;
var
  i, j: Integer;
begin
  Result := '';
  j := 0;
  for i := 1 to Length(str) do
  begin
    Result := Result + IntToHex(Byte(str[i]) xor XorKey[j], 2);
    j := (j + 1) mod 8;
  end;
end;
 
function Dec(str: string): string;
var
  i, j: Integer;
begin
  Result := '';
  j := 0;
  for i := 1 to Length(str) div 2 do
  begin
    Result := Result + Char(StrToInt('$' + Copy(str, i * 2 - 1, 2)) xor
    XorKey[j]);
    j := (j + 1) mod 8;
  end;
end;
 
end.
 
============================================

Delphi字符串加密/解密的更多相关文章

  1. 从网上整理的一些delphi字符串加密解密方法

    function Encode(Str: string): string; var //加密 TmpChr: AnsiChar; i, Len: integer; begin Result := St ...

  2. C# 字符串加密解密函数

    原文:C# 字符串加密解密函数 using System; using System.Text;using System.Security.Cryptography; using System.IO; ...

  3. 简单的JavaScript字符串加密解密

    简单的JavaScript字符串加密解密 <div> <input type="text" id="input" autofocus=&quo ...

  4. java字符串加密解密

    java字符串加密解密 字符串加密解密的方式很多,每一种加密有着相对的解密方法.下面要说的是java中模拟php的pack和unpack的字符串加密解密方法. java模拟php中pack: /** ...

  5. C# 字符串加密解密方法

    这个是加密的算法的命名空间,使用加密算法前要引用该程序集  System.Security.Cryptography using System;using System.Data;using Syst ...

  6. NET实现RSA AES DES 字符串 加密解密以及SHA1 MD5加密

    本文列举了    数据加密算法(Data Encryption Algorithm,DEA) 密码学中的高级加密标准(Advanced EncryptionStandard,AES)RSA公钥加密算法 ...

  7. php使用内置的mcrypt_encrypt和mcrypt_decrypt进行字符串加密解密

    <?php /*****************************加密*******************************/$key = "miyao";// ...

  8. 在JavaWeb项目中URL中字符串加密解密方案

    URL由来: 一般来说,URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和符号.比如,世界上有英文字母的网址 “http://www.abc.com”,但是没有希腊字母的网址“htt ...

  9. [Python] 字符串加密解密

    1. 最简单的方法是用base64: import base64 s1 = base64.encodestring('hello world') s2 = base64.decodestring(s1 ...

随机推荐

  1. Java练习题01

    问题: 利用二维非矩阵数组输出下面的数字 0 1 2 2 3 4 3 4 5 6 代码: public class Page98{     public static void main(String ...

  2. codevs 1219 骑士游历 1997年

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 设有一个n*m的棋盘(2≤n≤50,2≤m≤50),如下图,在棋盘上有一个中国象 ...

  3. (转)编码剖析Spring管理Bean的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52832434 在Spring的第一个案例中,我们已经知道了怎么将bean交给Spring容器进 ...

  4. (转)SpringMVC学习(九)——SpringMVC中实现文件上传

    http://blog.csdn.net/yerenyuan_pku/article/details/72511975 这一篇博文主要来总结下SpringMVC中实现文件上传的步骤.但这里我只讲单个文 ...

  5. Android(java)学习笔记160:开发一个多界面的应用程序之清单文件

    清单文件的重要参数:     <intent-filter>             代表的应用程序的入口界面           <action android:name=&quo ...

  6. spring使用elasticsearchrepository时间格式的问题Invalid format: "XXXX-XX-XX" is malformed at "-XX-XX"

    Invalid format: "XXXX-XX-XX" is malformed at "-XX-XX" 新手,刚接触elasticsearch遇到的问题. ...

  7. java中HashMap,LinkedHashMap,TreeMap,HashTable的区别

    java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMap Hashtable LinkedHashMap 和TreeMap Map主要用于存储健值对,根 ...

  8. 2018 CCPC 女生赛 hdoj6288 缺失的数据范围

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6288 Summarize:1.二分查找答案: 2.自带log函数精度不够,需自己写: 3.注意二分递归 ...

  9. win10和office2013激活

    1.去网上找kms,也可以在这下载————http://pan.baidu.com/s/1sjEAvwD————PS:找好对应的版本 2.首次运行时,只能点击激活windows VL和office 2 ...

  10. 【pwnable】asm之write up

    首先查看源代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <s ...