Friday, March 25, 2011

Delphi unit to encrypt / decrypt string - Needs DCP library from torry


unit uCrypt;


interface

function Encrypt(Value : String; Passphrase : String) : String;
function Decrypt(Value : String; Passphrase : String) : String;

implementation

uses

DCPcrypt2, DCPblockciphers, DCPblowfish, DCPsha512, Math, SysUtils, Classes;

function Encrypt(Value : String; Passphrase : String) : String;
var
Salt: array[0..7] of byte;
CipherIV: array of byte;
HashDigest: array of byte;
Hash : TDCP_sha512;
Cipher : TDCP_blowfish;
i : Integer;
Input, Output : TStringStream;
begin

Input := TStringStream.Create(Value);
Output := TStringStream.Create;

Hash := TDCP_sha512.Create(nil);
Hash.Algorithm := 'SHA512';
Hash.Id := 30;
Hash.HashSize := 512;

Cipher := TDCP_blowfish.Create(nil);
Cipher.BlockSize := 64;
Cipher.CipherMode := cmCBC;
Cipher.Id := 5;
Cipher.MaxKeySize := 448;

SetLength(HashDigest,Hash.HashSize div 8);
for i := 0 to 7 do
Salt[i] := Random(256);

Output.WriteBuffer(Salt,Sizeof(Salt));

Hash.Init;
Hash.Update(Salt[0],Sizeof(Salt)); // hash the salt
Hash.UpdateStr(Passphrase); // and the passphrase
Hash.Final(HashDigest[0]);
SetLength(CipherIV,Cipher.BlockSize div 8);
for i := 0 to (Length(CipherIV) - 1) do
CipherIV[i] := Random(256); // again just random values for the IV
Output.WriteBuffer(CipherIV[0],Length(CipherIV)); // write out the IV so we can decrypt!
Cipher.Init(HashDigest[0],Min(Cipher.MaxKeySize,Hash.HashSize),CipherIV); // initialise the cipher with the hash as key
Cipher.EncryptStream(Input,Output,Input.Size); // encrypt the entire file
Cipher.Burn;

Result := Output.DataString;

FreeAndNil(Hash);
FreeAndNil(Cipher);
FreeAndNil(Input);
FreeAndNil(Output);

end;

function Decrypt(Value : String; Passphrase : String) : String;
var
Cipher: TDCP_blowfish;
CipherIV: array of byte;
Hash: TDCP_sha512;
HashDigest: array of byte;
Salt: array[0..7] of byte;
i : Integer;
Input, Output : TStringStream;
begin

Input := TStringStream.Create(Value);
Input.Position := 0;
Output := TStringStream.Create;

Hash := TDCP_sha512.Create(nil);
Hash.Algorithm := 'SHA512';
Hash.Id := 30;
Hash.HashSize := 512;

Cipher := TDCP_blowfish.Create(nil);
Cipher.BlockSize := 64;
Cipher.CipherMode := cmCBC;
Cipher.Id := 5;
Cipher.MaxKeySize := 448;

SetLength(HashDigest,Hash.HashSize div 8);

Input.ReadBuffer(Salt[0],Sizeof(Salt)); // read the salt in from the file
Hash.Init;
Hash.Update(Salt[0],Sizeof(Salt)); // hash the salt
Hash.UpdateStr(Passphrase); // and the passphrase
Hash.Final(HashDigest[0]);

SetLength(CipherIV,TDCP_blockcipher(Cipher).BlockSize div 8);
Input.ReadBuffer(CipherIV[0],Length(CipherIV)); // read the initialisation vector from the file
Cipher.Init(HashDigest[0],Min(Cipher.MaxKeySize,Hash.HashSize),CipherIV); // initialise the cipher
Cipher.DecryptStream(Input,Output,Input.Size - Input.Position); // decrypt!
Cipher.Burn;

Result := Output.DataString;

FreeAndNil(Hash);
FreeAndNil(Cipher);
FreeAndNil(Input);
FreeAndNil(Output);

end;

end.

Delphi unicode string encryption , decryption and saving/loading via memorystream

this post shows example on how to encrypt and decrypt unicode string.

I was having a hard time getting unicode string properly saved and loaded.

and also how to save and load them from a file using tmemorystream


function EncryptStr(const S: String; Key: Word): String;
var I: Integer;
const C1 = 53761;
C2 = 32618;
begin
Result := S;
for I := 1 to Length(S) do begin
Result[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(Result[I]) + Key) * C1 + C2;
end;
end;

function DecryptStr(const S: String; Key: Word): String;
var I: Integer;
const C1 = 53761;
C2 = 32618;
begin
Result := S;
for I := 1 to Length(S) do begin
Result[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(S[I]) + Key) * C1 + C2;
end;
end;


example

var

s, x: UTF8String

begin

s := UTF8String(EncryptStr('asdagvaasbarbarb',12345)); //12345 is word type for password

x := UTF8String(DecryptStr(s,12345));


how to save and load using tmemorystream

len := length(s); //len is cardinal
ms := TMemoryStream.Create;
ms.WriteBuffer(len,4);
ms.WriteBuffer(s[1],len);
ms.SaveToFile('myFile.Dat');
ms.Free;

ms := TMemoryStream.Create;
ms.LoadFromFile(myfile.dat');
ms.Position := 0;
ms.readBuffer(len, 4);
setLength(s, len);
ms.ReadBuffer(s[1], len);

s will contain the encrypted string.

Sunday, March 20, 2011

Errors that might occur during replication

1) Altering table might cause "CLR 2.0.5.5.,x,zxczxc not installed properly" error. I have not found any fix yet

2) Replicating Blob fields are set to max 65K bytes by defaut. You can set the max size by a simple statement. I forgot how. Do some googling


Wednesday, March 16, 2011

SQL Replication over Internet

many sites will tell you to create a server alias to configure as subscriber.

But you will still get "NOT SUPPORTED" message when you try.

what those sites forgot to tell you is to name the alias the same computer name as the subscriber.

if the subscriber's computer name is ABC, name the server alias as ABC also.


Credits :

http://www.sqlservercentral.com/Forums/Topic343045-291-1.aspx

Post by david-719753