AES encryption

Discussions related to Visual Prolog
User avatar
Tonton Luc
VIP Member
Posts: 501
Joined: 16 Oct 2001 23:01

AES encryption

Unread post by Tonton Luc » 29 Mar 2016 13:58

Hi,

Is it possible to make this type of encryption using VP 7.3 :

Algorithme : AES
Mode : CBC
Padding : PKCS5Padding
Bloc size : 128 bits

Does this following is an AES scrypting ?

Code: Select all

implement crypt     open core   constants     className = "crypt/crypt".     classVersion = "".   class facts   v0_ : unsigned := 0.   v1_ : unsigned := 0.   sum_ :unsigned := 0.   clauses     classInfo(className, classVersion).   encrypt(OrigBin, Password) = AnsBin:-     hash_password(Password, K0, K1, K2, K3),     Len = binary::getSize(OrigBin),             Ptr = uncheckedConvert(pointer, OrigBin),   % Make exact copy so un-processed bytes remain the same     AnsBin = binary::createAtomicFromPointer(Ptr, Len),     PairMax = Len div 8,            % Process pairs of unsigned (i.e. 8 bytes at a time)       foreach Pair = std::fromTo(1, PairMax) do         Pair2 = Pair + Pair,         N0 = Pair2 - 2,         N1 = Pair2 - 1,         v0_ := binary::getIndexed_unsigned(OrigBin, N0),         v1_ := binary::getIndexed_unsigned(OrigBin, N1),         sum_ := 0,         foreach _N = std::fromTo(1, 32) do             sum_ := add(sum_, 0x9e3779b9),      % Delta = 0x9e3779b9             Temp0 = bit::bitXor((bit::bitXor(addL4(v1_, K0), add(v1_, sum_))), addR5(v1_, K1)),             v0_ := add(v0_, Temp0),             Temp1 = bit::bitXor((bit::bitXor(addL4(v0_, K2), add(v0_, sum_))), addR5(v0_, K3)),             v1_ := add(v1_, Temp1)         end foreach,               % Loop 32 times end         binary::setIndexed_unsigned(AnsBin, N0, v0_),         binary::setIndexed_unsigned(AnsBin, N1, v1_)     end foreach.               % Loop Pair end     decrypt(OrigBin, Password) = AnsBin:-     hash_password(Password, K0, K1, K2, K3),     Len = binary::getSize(OrigBin),     Ptr = uncheckedConvert(pointer, OrigBin),     AnsBin = binary::createAtomicFromPointer(Ptr, Len),     PairMax = Len div 8,            % Process pairs of unsigned     foreach Pair = std::fromTo(1, PairMax) do         Pair2 = Pair + Pair,         N0 = Pair2 - 2,         N1 = Pair2 - 1,         v0_ := binary::getIndexed_unsigned(OrigBin, N0),         v1_ := binary::getIndexed_unsigned(OrigBin, N1),         sum_ := 0xC6EF3720,     % = (Delta << 5)         foreach _N = std::fromTo(1, 32) do             Temp1 = bit::bitXor((bit::bitXor(addL4(v0_, K2), add(v0_, sum_))), addR5(v0_, K3)),             v1_  := sub(v1_, Temp1),             Temp0 = bit::bitXor((bit::bitXor(addL4(v1_, K0), add(v1_, sum_))), addR5(v1_, K1)),             v0_  := sub(v0_, Temp0),             sum_ := sub(sum_, 0x9e3779b9)       % Delta = 0x9e3779b9,         end foreach,      % Loop 32 times end         binary::setIndexed_unsigned(AnsBin, N0, v0_),         binary::setIndexed_unsigned(AnsBin, N1, v1_)     end foreach.      % Loop Pair end   class predicates   add: (unsigned, unsigned) -> unsigned Result.        % Safe Addition with modulo   sub: (unsigned, unsigned) -> unsigned Result.        % Safe Subtraction   clauses   add(U32orig, U32incr) = convert(unsigned, Ans) :-         U64orig = convert(unsigned64, U32orig),      % Prevent integer overflow error         Ans = (U64orig + U32incr) mod 4294967296.   % 2^32     sub(Orig, Decr) = Ans :-         if Orig >= Decr then             Ans = Orig - Decr         else             Ans = (0xffffffff - Decr) + Orig + 1         end if.   class predicates   addR5 : (unsigned Orig, unsigned Incr) -> unsigned.   % bitRight by 5 bits then incr   addL4 : (unsigned Orig, unsigned Incr) -> unsigned.   % bitLeft  by 4 bits then incr   clauses   addR5(U, Incr) = add(Incr, bit::bitRight(U, 5)).     addL4(U, Incr) = add(Incr, bit::bitLeft(U, 4)).   class predicates   hash_password: (string Password, unsigned K1, unsigned K2, unsigned K3, unsigned K4) procedure (i,o,o,o,o).   clauses   hash_password(Password, K0, K1, K2, K3):-         PasswordSalted = string::concat(Password, "Put your salt here"),         HashBin = cryptography::hashString(crypt_native::calg_md5, PasswordSalted),         K0 = binary::getIndexed_unsigned8(HashBin, 0),         K1 = binary::getIndexed_unsigned8(HashBin, 1),         K2 = binary::getIndexed_unsigned8(HashBin, 2),         K3 = binary::getIndexed_unsigned8(HashBin, 3). end implement crypt

User avatar
Jan de Lint
VIP Member
Posts: 107
Joined: 6 Mar 2000 0:01

Unread post by Jan de Lint » 16 Apr 2016 8:32

Hi Tonton,
I would not code encrypting myself. There are open source encrypting programs of all sorts which provide also DLL's. I used one of those until VP provided a more native solution.
If you have a 'foreign' DLL, you can rather easily use it, be it 32 or 64 bits. However I would advise you to use VP version 7.5.
]an

User avatar
Tonton Luc
VIP Member
Posts: 501
Joined: 16 Oct 2001 23:01

Unread post by Tonton Luc » 18 Apr 2016 6:37

Hi Jan,

And which one do you use ? Chilkatcrypt2 ?

User avatar
Jan de Lint
VIP Member
Posts: 107
Joined: 6 Mar 2000 0:01

Unread post by Jan de Lint » 23 Apr 2016 17:48

I used the openssl library.
See Wiki
You might encounter problems incorporating a foreign dll in VP prior to 7.5 however.
]an

Post Reply