• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

delphi加密XOR

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi滚动条组件TScrollBar介绍发布时间:2022-07-18
下一篇:
delphiTreeComboBox解决树的单击事件和小三角单击事件冲突问题发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap