From  http://www.delphigeist.com/2009/09/text-encryption-with-xor.html

Text encryption with XOR

 
Ever wanted to encrypt a text message?
In order to do that we need some helper functions like transforming the string to it's hex representation after encryption so we don't loose any characters plus it looks very good:

function StringToHexStr(const value: string): string;
begin
  SetLength(Result, Length(value) *2);
  if Length(value) > 0 then
    BinToHex(PChar(value), PChar(Result), Length(value));
end; function HexStrToString(const value: string): string;
begin
  SetLength(Result, Length(value) div 2);
  if Length(value) > 0 then
    HexToBin(PChar(value), PChar(Result), Length(value));
end;

ok... now we need a hash function so we hash our password string

function hashKey(const Key: String): Integer;
var
  Index: Integer;
begin
  Result := 0;;
  for Index := 1 to Length(Key) do
    Result := ((Result shl 7) or (Result shr 25)) + Ord(Key[Index]);
end;

Note that you can use any hash functions you like as long as it's result type is Cardinal or Integer(unsigned long or signed long) this hash function is taken from (RemObjects Software) PascalScript's "uPSUtils.pas" unit, now we need the algorithm

function __encrypt(const Key, Source: String): String;
// this function should not be used directly
// use EncryptText and DecryptText
const
  szBuffer = SizeOf(Integer); (* 4 bytes *)
  szByteBuffer = SizeOf(Byte); (* 1 byte *)
var
  byteBuffer,
  buffer,
  index,
  theKey: Integer;
  StreamOut,
  StreamIn: TStringStream;
begin
  (* hash the key and store it on local integer variable *)
  theKey := hashKey(Key);
  (* create two TStringStream's:
     - one for the actual data
     - the other one for the encrypted/decrypted data *)
  StreamIn := TStringStream.Create(Source);
  StreamOut := TStringStream.Create('');
  (* make sure position is set to ZERO !! *)
  StreamIn.Position := 0;
  StreamOut.Position := 0;   (* now loop WHILE number of bytes read is less than
     number of total bytes AND the difference between
     position and size is greater or equal to szBuffer
     which is 4 bytes *)
  while (StreamIn.Position < StreamIn.Size) and
    ((StreamIn.Size -StreamIn.Position) >= szBuffer) do begin
    (* read 4 bytes at a time into a local integer variable *)
    StreamIn.ReadBuffer(buffer, szBuffer);
    (* the XOR encryption/decryption *)
    buffer := buffer xor theKey;
    buffer := buffer xor $E0F;
    (* write data to output stream *)
    StreamOut.WriteBuffer(buffer, szBuffer);
  end;   (* check if we have some bytes left, there's a fat
     chance we do... *)
  if (StreamIn.Size -StreamIn.Position) >= 1 then
    for index := StreamIn.Position to StreamIn.Size -1 do begin
      (* we should have 1, 2 or 3 bytes left MAX, so we
         read 1 byte at a time *)
      StreamIn.ReadBuffer(byteBuffer, szByteBuffer);
      (* the XOR encryption/decryption *)
      byteBuffer := byteBuffer xor $F;
      (* write data to output stream *)
      StreamOut.WriteBuffer(byteBuffer, szByteBuffer);
    end;   (* set output stream's postion to ZERO so we can
     read it's data *)
  StreamOut.Position := 0;
  (* read data from output stream and return it's value *)
  Result := StreamOut.ReadString(StreamOut.Size);   (* free allocated memory *)
  FreeAndNil(StreamIn);
  FreeAndNil(StreamOut);
end;

the encryption and decryption functions

(* this function should be used ONLY for encryption *)
function EncryptText(const Key, Source: String): String;
begin
  (* return the encrypted data *)
  Result := __encrypt(Key, Source);
  (* convert string to hex string *)
  Result := StringToHexStr(Result);
end; (* this function should be used ONLY for decryption *)
function DecryptText(const Key, Source: String): String;
begin
  (* convert each hex string to string *)
  Result := HexStrToString(Source);
  (* return the decrypted data *)
  Result := __encrypt(Key, Result);
end;

Here's the encryption result of string "http://delphigeist.blogspot.com"

124CE8194017B30D1F54EC01135FF900094CB20B1657FB1A0A57E8476C6062

delphi 加密 XOR的更多相关文章

  1. DELPHI加密字串(异或运算加密)

    首先有两个自定的转换函数: function myStrToHex(s:string):string; //字串转16进制 var TmpStr:string; i:integer; begin Tm ...

  2. Delphi加密解密算法

    // 加密方法一(通过密钥加密解密)function EncryptString(Source, Key: string): string;function UnEncryptString(Sourc ...

  3. python访问互联网

    1.python有一个网络包urllib,里面有很多网络模块,其中我们常用的就是urllib.request (module)这个模块 2.引入要是用的模块:import urllib.request ...

  4. delphi简单单向字符串加密函数

    delphi用久了有的时候得给密码加密一下,简单点就行了,这个函数还是不错的. const XorKey:array[0..7] of Byte=($B2,$09,$AA,$55,$93,$6D,$8 ...

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

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

  6. Delphi字符串加密/解密

    unit uEncrypt_Decrypt;   interface   uses SysUtils;   const XorKey: array[0..7] of Byte = ($B2, $09, ...

  7. Delphi编写的等长加密与解密

    最近在看一本关于网络游戏服务端开发的书,虽然该书是个空架子,里面没有多少实际的内容(此书评价不好),但其中关于等长加密与解密的代码还是有一定的借鉴作用的.他山之石,可以攻玉.因为书中是C++的代码,所 ...

  8. java和delphi共用的des加密解密

    java: import antlr.StringUtils;import org.jeecgframework.core.util.StringUtil; import java.security. ...

  9. 透过 Delphi 使用二进位金钥做 AES 加密.

    从 1994 年开始,笔者就开始接触加密与网路安全的世界,从鲁立忠老师的指导当中获益良多,后来在台湾的元智大学就读研究所的时候,也以此为研究主题. 在当时,电子商务是显学,Visa跟 Master C ...

随机推荐

  1. Vue中的“混合”——mixins使用方法

    混合是一种灵活的分布式复用 Vue 组件的方式.混合对象可以包含任意组件选项.以组件使用混合对象时,所有混合对象的选项将被混入该组件本身的选项.当组件和混合对象含有同名选项时,这些选项将以恰当的方式混 ...

  2. golang 自定义importpath

    golang 的包导入和其他语言有好多不一样的地方,以下是一个自定义的导入 golang 自定义导入说明 一个官方的说明 比较简单,就不翻译了,主要是说我们可以通过添加meta 数据告诉包如何进行加载 ...

  3. StreamSets 管理 SDC Edge上的pipeline

    可选的方式: ui (data colelctor) 发送命令 UI 主要是创建edge pipeline 的时候进行edge server 的配置 默认是 http://localhost:1863 ...

  4. Net分布式系统整体框架

    Net分布式系统之一:系统整体框架介绍 一.设计目的 从事.Net平台开发系统已有8年多了,一直思考搭建.Net分布式系统架构.基于window平台搭建的大型分布式系统不多,之前了解过myspace. ...

  5. 4G的添加

    (ZTE 中兴 ME3760_V2 ) 1. 复制myLte文件到相关目录,设置权限为755 2.修改openwrt1407/package/base-files/files/etc/rc.local ...

  6. 微信小程序的视频教程

    极客学院小程序视频教程: 链接:https://pan.baidu.com/s/1VpKnvnsn-T6Nd79bsi4ugg 密码:0ta9 小程序项目实战: 链接:https://pan.baid ...

  7. C# 解析 json Newtonsoft果然强大,代码写的真好

    C# 解析 json JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的 ...

  8. 【Hibernate学习笔记-4】在hibernate.cfg.xml中配置C3P0数据源

    jar包 hibernate.cfg.xml <?xml version="1.0" encoding="GBK"?> <!DOCTYPE h ...

  9. MySQL优化技巧之四:mysql数据库开发常见问题及优化[转]

    mysql 数据库是被广泛应用的关系型数据库,其体积小.支持多处理器.开源并免费的特性使其在 Internet 中小型网站中的使用率尤其高.在使用 mysql 的过程中不规范的 SQL 编写.非最优的 ...

  10. 杂项-自动化测试工具:Selenium(浏览器自动化测试框架)

    ylbtech-杂项-自动化测试工具:Selenium(浏览器自动化测试框架) Selenium 是一个用于Web 应用程序测试的工具.Selenium 测试直接运行在浏览器中,就像真正的用户在操作一 ...