Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.clipper.visual-objects > #8585 > unrolled thread

AES256-ICM

Started byFranz Rachbauer <office@rachbauer.org>
First post2015-10-29 15:18 +0100
Last post2016-01-01 18:16 +0100
Articles 13 on this page of 33 — 7 participants

Back to article view | Back to comp.lang.clipper.visual-objects


Contents

  AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-10-29 15:18 +0100
    Re: AES256-ICM "Willie Moore" <williem@wmconsulting.com> - 2015-10-29 09:46 -0500
    Re: AES256-ICM Carlos Rocha <JDOTCarlosDOTRochaDOT1@SpiedBy.gmail.com> - 2015-10-29 15:45 +0000
    Re: AES256-ICM Carlos Rocha <JDOTCarlosDOTRochaDOT1@SpiedBy.gmail.com> - 2015-10-29 23:44 +0000
      Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-11-02 10:38 +0100
        Re: AES256-ICM Carlos Rocha <JDOTCarlosDOTRochaDOT1@SpiedBy.gmail.com> - 2015-11-02 20:00 +0000
          Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-11-10 09:21 +0100
    Re: AES256-ICM sherlocksoftware@gmail.com - 2015-10-30 17:19 -0700
      Re: AES256-ICM Carlos Rocha <JDOTCarlosDOTRochaDOT1@SpiedBy.gmail.com> - 2015-10-31 01:43 +0000
      Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-11-02 10:35 +0100
        Re: AES256-ICM sherlocksoftware@gmail.com - 2015-11-02 03:46 -0800
          Re: AES256-ICM sherlocksoftware@gmail.com - 2015-11-02 05:32 -0800
          Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-11-02 15:13 +0100
            Re: AES256-ICM Sherlock <sherlock@sherlock.com.au> - 2015-11-03 03:54 -0800
              Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-11-03 14:14 +0100
                Re: AES256-ICM Sherlock <sherlock@sherlock.com.au> - 2015-11-03 05:35 -0800
    Re: AES256-ICM "Jean Raymond" <jean_ray67@hotmail.com> - 2015-10-31 23:29 -0400
    Re: AES256-ICM Sherlock <sherlock@sherlock.com.au> - 2015-11-04 17:24 -0800
      Re: AES256-ICM "Wolfgang Riedmann" <wriedmann@gmail.com> - 2015-11-05 08:22 +0100
        Re: AES256-ICM Sherlock <sherlock@sherlock.com.au> - 2015-11-05 14:49 -0800
        Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-11-10 07:47 +0100
          Re: AES256-ICM "Wolfgang Riedmann" <wriedmann@gmail.com> - 2015-11-10 10:58 +0100
      Re: AES256-ICM "Willie Moore" <williem@wmconsulting.com> - 2015-11-05 06:58 -0600
    Re: AES256-ICM Sherlock <sherlock@sherlock.com.au> - 2015-11-04 19:47 -0800
      Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-11-10 08:19 +0100
      Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-12-29 09:11 +0100
        Re: AES256-ICM Sherlock <sherlock@sherlock.com.au> - 2015-12-29 19:49 -0800
          Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-12-30 09:36 +0100
            Re: AES256-ICM Sherlock <sherlock@sherlock.com.au> - 2015-12-30 13:53 -0800
              Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2015-12-31 09:32 +0100
                Re: AES256-ICM Sherlock <sherlock@sherlock.com.au> - 2015-12-31 01:11 -0800
                  Re: AES256-ICM Sherlock <sherlock@sherlock.com.au> - 2016-01-01 04:32 -0800
                  Re: AES256-ICM Franz Rachbauer <office@rachbauer.org> - 2016-01-01 18:16 +0100

Page 2 of 2 — ← Prev page 1 [2]


#8630

FromFranz Rachbauer <office@rachbauer.org>
Date2015-11-10 07:47 +0100
Message-ID<n1s3uc$va3$1@speranza.aioe.org>
In reply to#8610
Hi Wolfgang
should the castings be
	RETURN LoByte(WORD(_AND(DWORD(b1), DWORD(b2))))
or better
	RETURN LoByte(_AND(WORD(b1), WORD(b2)))

Franz

Am 05.11.2015 um 08:22 schrieb Wolfgang Riedmann:
> Hi Phil,
>
>> Here it is... please report any issues you may have.. but good old VO
>> code does it again.
>
> thank you very much for sharing.
>
> To compile in VO 2.8 SP4 without warnings I had to add some word()
> castings in lowbyte calls.
>
> I'll try to investgate the DLL issue. Do you are loading DLLs
> dynamically (LoadLibrary) or statically (using VO libs)?
>
> Wolfgang
>
>

[toc] | [prev] | [next] | [standalone]


#8633

From"Wolfgang Riedmann" <wriedmann@gmail.com>
Date2015-11-10 10:58 +0100
Message-ID<dadtf3F9h18U1@mid.individual.net>
In reply to#8630
Hi Franz,

I have opted for the first one.


Wolfgang


> Hi Wolfgang
> should the castings be
> 	RETURN LoByte(WORD(_AND(DWORD(b1), DWORD(b2))))
> or better
> 	RETURN LoByte(_AND(WORD(b1), WORD(b2)))
> 
> Franz
> 
> Am 05.11.2015 um 08:22 schrieb Wolfgang Riedmann:
> > Hi Phil,
> > 
> > > Here it is... please report any issues you may have.. but good
> > > old VO code does it again.
> > 
> > thank you very much for sharing.
> > 
> > To compile in VO 2.8 SP4 without warnings I had to add some word()
> > castings in lowbyte calls.
> > 
> > I'll try to investgate the DLL issue. Do you are loading DLLs
> > dynamically (LoadLibrary) or statically (using VO libs)?
> > 
> > Wolfgang
> > 
> > 



-- 

[toc] | [prev] | [next] | [standalone]


#8611

From"Willie Moore" <williem@wmconsulting.com>
Date2015-11-05 06:58 -0600
Message-ID<n1fjq1$qqs$1@speranza.aioe.org>
In reply to#8608
Phil,

For anyone wanting to do this in the dotNet arena. Here is the 
encryptionHelper class written in Vulcan. You expose by setting the 
comvisible property in the assembly.prg


Regards,
Willie
Grafx Development Team

// from assembly.prg

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisibleAttribute( true )]


// EncryptionHelper.prg

BEGIN NAMESPACE Common.NonGui

   CLASS EncryptionHelper

      #region Properties
      /// <summary>
      /// Static property to let the user overiide the default seed. This 
property will affect all instaances. If you use it, please
      /// make sure that you pass the same seed into any methods if you 
choose to use the methods that pass in the seed or
      /// you will get unpredicible results
      /// </summary>
      /// <returns></returns>
      STATIC PROPERTY Seed AS STRING Auto
      #endregion
      #region Constructors
      /// <summary>
      /// Static constructor to make sure that the seed is always set. 
Please note that by using the Seed property, this will overwrite
      /// the default seed for ALL instances
      /// </summary>
      STATIC CONSTRUCTOR()
         Seed := "A Very Long time AGO!"
         RETURN

      /// <summary>
      /// default constructor so class can be serialzed if needed
      /// </summary>
      CONSTRUCTOR()
         SUPER()
         RETURN
      #endregion
      #region Decryption Methods
      /// <summary>
      ///  Override for decrypt that does not require a passed seed. This 
method also does a check to see if the string is really encrypted and it not
      /// it just returns the passed string instead of decrypting it
      /// </summary>
      /// <param name='cipherText'></param>
      /// <returns></returns>
      STATIC METHOD TryDecrypt(cipherText AS STRING) AS STRING
         LOCAL cReturn AS STRING
         IF cipherText:Contains("<HTML>") .or. cipherText:Contains("<html>") 
.or. cipherText:Contains("<body") .or. cipherText:Contains("<BODY")
            cReturn := cipherText
         ELSE
            cReturn := Decrypt(cipherText,Seed)
         ENDIF
         RETURN cReturn

      /// <summary>
      ///  Override for decrypt that does not require a passed seed. This 
method also does a check to see if the string is really encrypted and it not
      /// it just returns the passed string instead of decrypting it
      /// </summary>
      /// <param name='cipherText'></param>
      /// <returns></returns>
      STATIC METHOD Decrypt(cipherText AS STRING) AS STRING
         RETURN Decrypt(cipherText,Seed)

      /// <summary>
      /// regular called decrypt method where you pass in the text to 
decrypt and the seed that is needed for the decryption
      /// </summary>
      /// <param name='cipherText'></param>
      /// <param name='Password'></param>
      /// <returns></returns>
      STATIC METHOD Decrypt(cipherText AS STRING, Password AS STRING) AS 
STRING
         LOCAL cipherData AS BYTE[]
         LOCAL bytes      AS System.Security.Cryptography.Rfc2898DeriveBytes 
//PasswordDeriveBytes
         LOCAL buffer2    AS BYTE[]
         //
         cipherData := System.Convert.FromBase64String(cipherText)
         //bytes      := 
System.Security.Cryptography.PasswordDeriveBytes{Password, <BYTE>{0X49, 
0X76, 0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 0X76}}
         bytes      := 
System.Security.Cryptography.Rfc2898DeriveBytes{Password, <BYTE>{0X49, 0X76, 
0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 0X76}}
         buffer2    := Decrypt(cipherData, bytes:GetBytes(0X20), 
bytes:GetBytes(0X10))
         RETURN System.Text.Encoding.Unicode:GetString(buffer2)

      /// <summary>
      /// override of decrypt that allows the string tpo decrypt to be 
passed in as a byte array
      /// </summary>
      /// <param name='cipherData'></param>
      /// <param name='Password'></param>
      /// <returns></returns>
      STATIC METHOD Decrypt(cipherData AS BYTE[], Password AS STRING) AS 
BYTE[]
         LOCAL bytes AS System.Security.Cryptography.Rfc2898DeriveBytes 
//PasswordDeriveBytes
         //
         //bytes := 
System.Security.Cryptography.PasswordDeriveBytes{Password, <BYTE>{0X49, 
0X76, 0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 0X76}}
         bytes := System.Security.Cryptography.Rfc2898DeriveBytes{Password, 
<BYTE>{0X49, 0X76, 0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 
0X76}}
         RETURN Decrypt(cipherData, bytes:GetBytes(0X20), 
bytes:GetBytes(0X10))

      /// <summary>
      /// This is the version of code that actually does the decryption 
using AES from the framework classes
      /// </summary>
      /// <param name='cipherData'></param>
      /// <param name='Key'></param>
      /// <param name='IV'></param>
      /// <returns></returns>
      STATIC METHOD Decrypt(cipherData AS BYTE[], Key AS BYTE[], IV AS 
BYTE[]) AS BYTE[]
         LOCAL stream   AS System.IO.MemoryStream
         LOCAL rijndael AS System.Security.Cryptography.Rijndael
         LOCAL stream2  AS System.Security.Cryptography.CryptoStream
         //
         stream       := System.IO.MemoryStream{}
         rijndael     := System.Security.Cryptography.Rijndael.Create()
         rijndael:Key := Key
         rijndael:IV  := IV
         stream2      := System.Security.Cryptography.CryptoStream{stream, 
rijndael:CreateDecryptor(), 
System.Security.Cryptography.CryptoStreamMode.Write}
         stream2:Write(cipherData, 0, cipherData:Length)
         stream2:Close()
         RETURN stream:ToArray()

      /// <summary>
      /// This method will take an encrypted file and decrypt it to the 
specified file using the see that is passed in
      /// </summary>
      /// <param name='fileIn'></param>
      /// <param name='fileOut'></param>
      /// <param name='Password'></param>
      STATIC METHOD Decrypt(fileIn AS STRING, fileOut AS STRING, Password AS 
STRING) AS VOID
         LOCAL num2     AS LONG
         LOCAL stream   AS System.IO.FileStream
         LOCAL stream2  AS System.IO.FileStream
         LOCAL bytes    AS System.Security.Cryptography.Rfc2898DeriveBytes 
//PasswordDeriveBytes
         LOCAL rijndael AS System.Security.Cryptography.Rijndael
         LOCAL stream3  AS System.Security.Cryptography.CryptoStream
         LOCAL count    AS LONG
         LOCAL buffer   AS BYTE[]
         //
         stream       := System.IO.FileStream{fileIn, 
System.IO.FileMode.Open, System.IO.FileAccess.Read}
         stream2      := System.IO.FileStream{fileOut, 
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write}
         bytes        := 
System.Security.Cryptography.Rfc2898DeriveBytes{Password, <BYTE>{0X49, 0X76, 
0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 0X76}}
//       bytes        := 
System.Security.Cryptography.PasswordDeriveBytes{Password, <BYTE>{0X49, 
0X76, 0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 0X76}}
         rijndael     := System.Security.Cryptography.Rijndael.Create()
         rijndael:Key := bytes:GetBytes(0X20)
         rijndael:IV  := bytes:GetBytes(0X10)
         stream3      := System.Security.Cryptography.CryptoStream{stream2, 
rijndael:CreateDecryptor(), 
System.Security.Cryptography.CryptoStreamMode.Write}
         count        := 0X1000
         buffer       := BYTE[]{count}
         num2         := 1
         WHILE num2 <> 0
            //
            num2 := stream:Read(buffer, 0, count)
            stream3:Write(buffer, 0, num2)
         ENDDO
         stream3:Close()
         stream:Close()
         RETURN

      /// <summary>
      /// This method is an override of the 3 parameter decrypt method for 
files with the exception that it uses the stored seed instead of
      /// requiring that one be passed in
      /// </summary>
      /// <param name='fileIn'></param>
      /// <param name='fileOut'></param>
      STATIC METHOD DecryptFile(fileIn AS STRING,fileOut AS STRING) AS VOID
         RETURN Decrypt(fileIn, fileOut, Seed)
      #endregion
      #region Encryption Methods
      /// <summary>
      ///  Override for encrypt that does not require a passed seed
      /// </summary>
      /// <param name='cipherText'></param>
      /// <returns></returns>
      STATIC METHOD Encrypt(clearText AS STRING) AS STRING
         RETURN Encrypt(clearText,Seed)

      /// <summary>
      /// main encrypt method. it accepts a string to encrypt along with the 
seed used for the encryption
      /// </summary>
      /// <param name='clearText'></param>
      /// <param name='Password'></param>
      /// <returns></returns>
      STATIC METHOD Encrypt(clearText AS STRING, Password AS STRING) AS 
STRING
         LOCAL clearData AS BYTE[]
         LOCAL bytes     AS System.Security.Cryptography.Rfc2898DeriveBytes 
//PasswordDeriveBytes
         //
         clearData := System.Text.Encoding.Unicode:GetBytes(clearText)
         //bytes     := 
System.Security.Cryptography.PasswordDeriveBytes{Password, <BYTE>{0X49, 
0X76, 0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 0X76}}
         bytes     := 
System.Security.Cryptography.Rfc2898DeriveBytes{Password, <BYTE>{0X49, 0X76, 
0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 0X76}}
         RETURN System.Convert.ToBase64String(Encrypt(clearData, 
bytes:GetBytes(0X20), bytes:GetBytes(0X10)))

      /// <summary>
      /// override to Encrypt that allows for a byte array to be passed in 
instead of a string
      /// </summary>
      /// <param name='clearData'></param>
      /// <param name='Password'></param>
      /// <returns></returns>
      STATIC METHOD Encrypt(clearData AS BYTE[], Password AS STRING) AS 
BYTE[]
         LOCAL bytes AS System.Security.Cryptography.Rfc2898DeriveBytes 
//PasswordDeriveBytes
         //
         //bytes := 
System.Security.Cryptography.PasswordDeriveBytes{Password, <BYTE>{0X49, 
0X76, 0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 0X76}}
         bytes := System.Security.Cryptography.Rfc2898DeriveBytes{Password, 
<BYTE>{0X49, 0X76, 0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 
0X76}}
         RETURN Encrypt(clearData, bytes:GetBytes(0X20), 
bytes:GetBytes(0X10))

      /// <summary>
      /// this method does all of the encryption work using the framework 
AES classes
      /// </summary>
      /// <param name='clearData'></param>
      /// <param name='Key'></param>
      /// <param name='IV'></param>
      /// <returns></returns>
      STATIC METHOD Encrypt(clearData AS BYTE[], Key AS BYTE[], IV AS 
BYTE[]) AS BYTE[]
         LOCAL stream   AS System.IO.MemoryStream
         LOCAL rijndael AS System.Security.Cryptography.Rijndael
         LOCAL stream2  AS System.Security.Cryptography.CryptoStream
         //
         stream       := System.IO.MemoryStream{}
         rijndael     := System.Security.Cryptography.Rijndael.Create()
         rijndael:Key := Key
         rijndael:IV  := IV
         stream2      := System.Security.Cryptography.CryptoStream{stream, 
rijndael:CreateEncryptor(), 
System.Security.Cryptography.CryptoStreamMode.Write}
         stream2:Write(clearData, 0, clearData:Length)
         stream2:Close()
         RETURN stream:ToArray()

      /// <summary>
      /// this method will read in the file passed and encryt it and write 
the encrypted file to the fileOut specified
      /// </summary>
      /// <param name='fileIn'></param>
      /// <param name='fileOut'></param>
      /// <param name='Password'></param>
      STATIC METHOD Encrypt(fileIn AS STRING, fileOut AS STRING, Password AS 
STRING) AS VOID
         LOCAL num2     AS LONG
         LOCAL stream   AS System.IO.FileStream
         LOCAL stream2  AS System.IO.FileStream
         LOCAL bytes    AS System.Security.Cryptography.Rfc2898DeriveBytes 
//PasswordDeriveBytes
         LOCAL rijndael AS System.Security.Cryptography.Rijndael
         LOCAL stream3  AS System.Security.Cryptography.CryptoStream
         LOCAL count    AS LONG
         LOCAL buffer   AS BYTE[]
         //
         stream       := System.IO.FileStream{fileIn, 
System.IO.FileMode.Open, System.IO.FileAccess.Read}
         stream2      := System.IO.FileStream{fileOut, 
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write}
         //bytes        := 
System.Security.Cryptography.PasswordDeriveBytes{Password, <BYTE>{0X49, 
0X76, 0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 0X76}}
         bytes        := 
System.Security.Cryptography.Rfc2898DeriveBytes{Password, <BYTE>{0X49, 0X76, 
0X61, 110, 0X20, 0X4D, 0X65, 100, 0X76, 0X65, 100, 0X65, 0X76}}
         rijndael     := System.Security.Cryptography.Rijndael.Create()
         rijndael:Key := bytes:GetBytes(0X20)
         rijndael:IV  := bytes:GetBytes(0X10)
         stream3      := System.Security.Cryptography.CryptoStream{stream2, 
rijndael:CreateEncryptor(), 
System.Security.Cryptography.CryptoStreamMode.Write}
         count        := 0X1000
         buffer       := BYTE[]{count}
         num2         := 1   // make sure we make it through the loop 
atleast once
         WHILE num2 <> 0
            //
            num2 := stream:Read(buffer, 0, count)
            stream3:Write(buffer, 0, num2)
         ENDDO
         stream3:Close()
         stream:Close()
         RETURN


      /// <summary>
      /// Override for the 3 parameter encrypt that uses the stored see 
instead of making the user pass it. This is a file encryption
      /// </summary>
      /// <param name='fileIn'></param>
      /// <param name='fileOut'></param>
      STATIC METHOD EncryptFile(fileIn AS STRING,fileOut AS STRING) AS VOID
         RETURN Encrypt(fileIn, fileOut, Seed)

      #endregion
   END CLASS


END NAMESPACE // Common.NonGui 

[toc] | [prev] | [next] | [standalone]


#8609

FromSherlock <sherlock@sherlock.com.au>
Date2015-11-04 19:47 -0800
Message-ID<8962991e-c67e-4a97-b792-eb38cfc78502@googlegroups.com>
In reply to#8585
All it works.. but for some reason cannot work out why when code is in DLL it crashes... but same code in a library or in app.. works.

Please solve this for me..

Phil McGuinness
---------------

FUNCTION Start()

    LOCAL oCon AS Console
    LOCAL cKey, cInput, cEncrypted, cDecrypted, cOption  AS STRING 
    LOCAL oAes256 AS AES256
    LOCAL lTest := TRUE AS LOGIC  
    
    cKey := "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f"
    cInput := "00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff"  
    
    ? "TestAES256"
	? "============"
	? "Sometimes to see the results, you can copy the text from the console"
	? "to Notepad and have a closer look. To do that, right-click on console"
	? "window's title bar, go to Edit > Select All, then again right-click and go to"
	? "Edit > Copy."
    ? ""  
    ? "ONLINE validation routine: http://aes.online-domain-tools.com/"
    ? "" 
    ? "Input 16 : " + cInput
    ? "Key   32 : " + cKey
    ? "----------------------------------------" 
    oAes256 := AES256{ConvHex2Normal(cKey)}      
    // --------------------------------------------------------------------------------
    cEncrypted := oAes256:Encrypt(ConvHex2Normal(cInput))
    cDecrypted := oAes256:Decrypt(cEncrypted)
    
    ? "Encrypted: " + IIF(SLen(cEncrypted) > 0, Lower(AsHexString(cEncrypted)), "")
    ? "Decrypted: " + IIF(SLen(cDecrypted) > 0, Lower(AsHexString(cDecrypted)), "")   
    ? ""
    ? "This was demo of one 16-byte block encoding."
    ? "" 
    // -------------------------------------------------------------------------------
  
    cOption := _Wait("Test variable length strings? (y/n) ")
    IF cOption == "y" 
        cOption := _Wait("Remove trailing nulls from decrypted text (y/n) ")
        ? ""       
        ? ""
        oCon := Console{}
        
        DO WHILE lTest
            oCon:WriteLine("String to encrypt (Enter = exit)")
            oCon:WriteLine("1..............f1..............f1..............f1..............f")
            cInput := oCon:Read()
            
            IF SLen(cInput) > 0            
                cEncrypted := oAes256:Encrypt(cInput)
                cDecrypted := oAes256:Decrypt(cEncrypted, IIF(cOption == "y", TRUE, FALSE))
                
                oCon:WriteLine("")
                oCon:WriteLine("Input    : " + cInput + "<")
                oCon:WriteLine("Decrypted: " + cDecrypted + "<")
                oCon:WriteLine("")
                oCon:WriteLine("Encrypted: " + Lower(AsHexString(cEncrypted)))
                oCon:WriteLine("------------------------------------") 
                oCon:WriteLine("")
            ELSE
                lTest := FALSE
            ENDIF
        ENDDO        
    ENDIF
    
    oAes256 := NULL_OBJECT      // Not necessary here, just to remind you that this object allocates some static memory which is freed on Axit()

RETURN NIL         

FUNCTION Conv2Array(cList AS STRING, cDim := "," AS STRING) AS ARRAY                
// Spolit string into an array
// This is taken from Sherlock Library in order to ensure we have no external dependencies

    LOCAL nPos AS DWORD     	// Position of cDelimiter in cList
    LOCAL aList := {}  	AS ARRAY     	// Define an empty array

    // Loop while there are more items to extract
    DO WHILE ( nPos := At( cDim, cList )) != 0
        // Add the item to aList and remove it from cList
        AAdd( aList, SubStr( cList, 1, nPos - 1 ))
        cList := SubStr( cList, nPos + 1 )
    ENDDO
    AAdd( aList, AllTrim(cList) )                         	// Add final element
    //
RETURN  aList                              		// Return the array   
                                                                        
FUNCTION ConvHex2Normal(cHexString AS STRING) AS STRING
    LOCAL cOut :=  "" AS STRING
    LOCAL a AS ARRAY
    LOCAL x, dwChar AS DWORD
    
    a := Conv2Array(cHexString, " ")
    FOR x := 1 TO ALen(a)
        dwChar := DWORD(Val("0x" + AsString(a[x])))
        cOut += CHR(dwChar)             
    NEXT
    
RETURN cOut    

[toc] | [prev] | [next] | [standalone]


#8631

FromFranz Rachbauer <office@rachbauer.org>
Date2015-11-10 08:19 +0100
Message-ID<n1s5qi$2ss$2@speranza.aioe.org>
In reply to#8609
Hi Phil,
you're great, thank you so much! But I have a question to AES256 
crypting: Why it is necessary to use a cKey of length 32?

Franz

Am 05.11.2015 um 04:47 schrieb Sherlock:
> cKey := "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f"

[toc] | [prev] | [next] | [standalone]


#8847

FromFranz Rachbauer <office@rachbauer.org>
Date2015-12-29 09:11 +0100
Message-ID<n5tf7m$rqg$1@speranza.aioe.org>
In reply to#8609
Hi Phil,
if I use a cKey with length others than 32 in your AES256 Class I get an 
error in SystemLibrary (__Str2Hex). Why only cKey with length 32 is 
allowed? Is this defined for AES256 or is it a problem in your class?

Regards, Franz

[toc] | [prev] | [next] | [standalone]


#8850

FromSherlock <sherlock@sherlock.com.au>
Date2015-12-29 19:49 -0800
Message-ID<02301a1a-4ba8-449a-8199-9f67b85612d7@googlegroups.com>
In reply to#8847
Franz

snip[ if I use a cKey with length others than 32 in your AES256 Class I get an 
error in SystemLibrary (__Str2Hex). Why only cKey with length 32 is allowed? Is this defined for AES256 or is it a problem in your class? ]

A few things,.. this solves the issue with code in a DLL not working and was the fact the init was strongly typed.. nobody picked that up.

Other question try with this code first.

Phil
----


METHOD Init(cKeyUsual) CLASS AES256     

    LOCAL k AS BYTE PTR
    LOCAL rcon AS BYTE
    LOCAL i AS DWORD
    LOCAL cKey AS STRING 
                         
    cKey := AsString(cKeyUsual)                         
    IF SLen(cKey) == 32
    	  
    	//? 'slen(ckey)' , cKey, 32     
    	
        k := SELF:Str2ByteArray(cKey, 32)
        //
        SELF:key := MemAlloc(32)
        SELF:enckey := MemAlloc(32)
        SELF:deckey := MemAlloc(32)
        SELF:sbox := MemAlloc(256)
        SELF:sboxinv := MemAlloc(256)
        //
      //  _Wait()    
        //      
        IF (k == NULL_PTR) .OR. (SELF:key == NULL_PTR) .OR. (SELF:enckey == NULL_PTR) .OR. (SELF:deckey == NULL_PTR) .OR. (SELF:sbox == NULL_PTR) .OR. (SELF:sboxinv == NULL_PTR)
            SELF:FreeArrays()                                                                                                                                                                                                                                               
            IF k != NULL_PTR
                MemFree(k)
            ENDIF
        ELSE                     
            SELF:InitArrays()
            
            FOR i := 1 TO 32    
                SELF:key[i] := 0
                SELF:enckey[i] := k[i]
                SELF:deckey[i] := k[i]            
            NEXT
                            
            rcon := 1
            
            FOR i := 1 TO 7
                SELF:aes_expandEncKey(SELF:deckey, @rcon)
            NEXT  
            MemFree(k)              
            SELF:lInitialised := TRUE
        ENDIF
    ENDIF
    RegisterAxit(SELF)
    
RETURN SELF

CLASS AES256 
//   Byte-oriented AES-256 implementation.
//   All lookup tables replaced with 'on the fly' calculations. 
//
//   Copyright (c) 2007-2009 Ilya O. Levin, http://www.literatecode.com
//   Other contributors: Hal Finney
//  
//   Implemented in VO (c) 2015 Sherlock Software http://www.sherlock.com.au 
//
//   Permission to use, copy, modify, and distribute this software for any
//   purpose with or without fee is hereby granted, provided that the above
//   copyright notice and this permission notice appear in all copies.
//
//   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
//   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
//   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
//   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
//   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
//   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
//   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// ONLINE validation routine: http://aes.online-domain-tools.com/
    
    DECLARE METHOD Encrypt          // Encrypts string of any length (it is internally filled up by NULL characters (ASCII code 0) up to next multiple of 16 
    DECLARE METHOD Decrypt          // Decrypts string of any length as returned by function Encrypt so the string's length must be multiple of 16
    
    // ------------------------------------- Private stuff -------------------------------------------
    PROTECT key AS BYTE PTR  
    PROTECT enckey AS BYTE PTR  
    PROTECT deckey AS BYTE PTR
    
    PROTECT sbox AS BYTE PTR  
    PROTECT sboxinv AS BYTE PTR
    PROTECT lInitialised := FALSE AS LOGIC      // Encryption/Decryption works only if this is true. This is set to true only when the encryption key is 32-char long
      
    // --------------------------------------- Protect methods ----------------------------------------
    DECLARE METHOD EncryptEcbArray  // Encrypts 16-chaaracter long part of byte array from given offset
    DECLARE METHOD DecryptEcbArray  // Decrypts 16-chaaracter long part of byte array from given offset
    
    DECLARE METHOD InitArrays
    DECLARE METHOD Str2ByteArray
    DECLARE METHOD ByteArray2Str   
    DECLARE METHOD rj_xtime
    DECLARE METHOD FD
    DECLARE METHOD aes_subBytes 
    DECLARE METHOD aes_subBytes_inv
    DECLARE METHOD aes_addRoundKeyX
    DECLARE METHOD aes_addRoundKey_cpy
    DECLARE METHOD aes_shiftRows
    DECLARE METHOD aes_shiftRows_inv
    DECLARE METHOD aes_mixColumns
    DECLARE METHOD aes_mixColumns_inv
    DECLARE METHOD aes_expandEncKey
    DECLARE METHOD aes_expandDecKey
    
    DECLARE METHOD ByteAnd
    DECLARE METHOD ByteOr
    DECLARE METHOD ByteXor
    DECLARE METHOD ByteNot
    DECLARE METHOD ByteShiftL
    DECLARE METHOD ByteShiftR
    
    DECLARE METHOD FreeArrays
    
PROTECT METHOD InitArrays() AS VOID PASCAL CLASS AES256
    LOCAL a AS ARRAY
    LOCAL i AS DWORD 
     
    a := { 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0 , ;
    0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15 , ;
    0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, ;
    0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, ;
    0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, ;
    0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, ;
    0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, ;
    0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, ;
    0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, ;
    0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, ;
    0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB }      // You can initialise an array with only up to 255 members
    AAdd(a, 0x16)
    
    FOR i := 1 TO 256
        SELF:sbox[i] := a[i]
    NEXT
    
    a := { 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, ;
    0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E, ;
    0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, ;
    0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84, ;
    0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, ;
    0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73, ;
    0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, ;
    0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, ;
    0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, ;
    0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61, ;
    0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C }      // You can initialise an array with only up to 255 members
    AAdd(a, 0x7D)
    
    FOR i := 1 TO 256
        SELF:sboxinv[i] := a[i]
    NEXT
    //
RETURN  

PROTECT METHOD Str2ByteArray( cStr AS STRING, dwLen AS DWORD ) AS BYTE PTR PASCAL CLASS AES256

    LOCAL buf AS BYTE PTR
    LOCAL i AS DWORD         
     
    buf := MemAlloc(dwLen)
    IF buf != NULL_PTR 
        FOR i := 1 TO dwLen
            buf[i] := BYTE(Asc(SubStr(cStr, i, 1)))
        NEXT
    ENDIF              
    //
RETURN buf     

PROTECT METHOD aes_expandEncKey(k AS BYTE PTR, rc REF BYTE) AS VOID PASCAL CLASS AES256 

    LOCAL i AS DWORD
    
    k[1] := SELF:ByteXor(SELF:ByteXor(k[1], sbox[k[30] + 1]), rc)  
    k[2] := SELF:ByteXor(k[2], sbox[k[31] + 1]) 
    k[3] := SELF:ByteXor(k[3], sbox[k[32] + 1]) 
    k[4] := SELF:ByteXor(k[4], sbox[k[29] + 1]) 
    rc := SELF:rj_xtime(rc)
    
    i := 5
    DO WHILE i < 17
        k[i] := SELF:ByteXor(k[i], k[i - 4])
        k[i + 1] := SELF:ByteXor(k[i + 1], k[i - 3])
        k[i + 2] := SELF:ByteXor(k[i + 2], k[i - 2])
        k[i + 3] := SELF:ByteXor(k[i + 3], k[i - 1])
        i += 4
    ENDDO 
    
    k[17] := SELF:ByteXor(k[17], sbox[k[13] + 1]) 
    k[18] := SELF:ByteXor(k[18], sbox[k[14] + 1]) 
    k[19] := SELF:ByteXor(k[19], sbox[k[15] + 1]) 
    k[20] := SELF:ByteXor(k[20], sbox[k[16] + 1])
    
    i := 21
    DO WHILE i < 33
        k[i] := SELF:ByteXor(k[i], k[i - 4])
        k[i + 1] := SELF:ByteXor(k[i + 1], k[i - 3])
        k[i + 2] := SELF:ByteXor(k[i + 2], k[i - 2])
        k[i + 3] := SELF:ByteXor(k[i + 3], k[i - 1])
        i += 4
    ENDDO 
RETURN 
      
PROTECT METHOD rj_xtime(x AS BYTE) AS BYTE PASCAL CLASS AES256 
    LOCAL y AS BYTE
    
    y := SELF:ByteShiftL(x, 1)
RETURN IIF(_AND(x, 0x80) > 0, _XOR(y, 0x1B), y)

PROTECT METHOD aes_subBytes(buf AS BYTE PTR) AS VOID PASCAL CLASS AES256
    LOCAL i AS DWORD 
    
    FOR i := 16 DOWNTO 1
        buf[i] := SELF:sbox[buf[i] + 1]
    NEXT
RETURN
 
PROTECT METHOD aes_subBytes_inv(buf AS BYTE PTR) AS VOID PASCAL CLASS AES256
    LOCAL i AS DWORD 
    
    FOR i := 16 DOWNTO 1
        buf[i] := SELF:sboxinv[buf[i] + 1]
    NEXT
RETURN

PROTECT METHOD aes_addRoundKey_cpy(buf AS BYTE PTR, key AS BYTE PTR, cpk AS BYTE PTR) AS VOID PASCAL CLASS AES256
    LOCAL i AS DWORD 
    
    FOR i := 16 DOWNTO 1                      
        cpk[i] := key[i]
        buf[i] := SELF:ByteXor(buf[i], cpk[i])
        cpk[16 + i] := key[16 + i]
    NEXT
RETURN

PROTECT METHOD aes_shiftRows(buf AS BYTE PTR) AS VOID PASCAL CLASS AES256  
    LOCAL i, j AS BYTE 
    
    i := buf[2]
    buf[2] := buf[6]
    buf[6] := buf[10]
    buf[10] := buf[14]
    buf[14] := i
    
    i := buf[11]
    buf[11] := buf[3]
    buf[3] := i
    
    j := buf[4]
    buf[4] := buf[16]
    buf[16] := buf[12]
    buf[12] := buf[8]
    buf[8] := j
    
    j := buf[15]
    buf[15] := buf[7]
    buf[7] := j
RETURN

PROTECT METHOD aes_shiftRows_inv(buf AS BYTE PTR) AS VOID PASCAL CLASS AES256
    LOCAL i, j AS BYTE 
    
    i := buf[2]
    buf[2] := buf[14]
    buf[14] := buf[10]
    buf[10] := buf[6]
    buf[6] := i
    
    i := buf[3]
    buf[3] := buf[11]
    buf[11] := i
    
    j := buf[4]
    buf[4] := buf[8]
    buf[8] := buf[12]
    buf[12] := buf[16]
    buf[16] := j
    
    j := buf[7]
    buf[7] := buf[15]
    buf[15] := j
RETURN

PROTECT METHOD aes_mixColumns(buf AS BYTE PTR) AS VOID PASCAL CLASS AES256
    LOCAL i AS DWORD
    LOCAL a, b, c, d, e AS BYTE 
                                
    i := 1
    DO WHILE i < 17
        a := buf[i]
        b := buf[i + 1]
        c := buf[i + 2]
        d := buf[i + 3] 
        e := SELF:ByteXor(SELF:ByteXor(SELF:ByteXor(a, b), c), d)
        buf[i] := SELF:ByteXor(SELF:ByteXor(buf[i], e), SELF:rj_xtime(SELF:ByteXor(a, b)))
        buf[i + 1] := SELF:ByteXor(SELF:ByteXor(buf[i + 1], e), SELF:rj_xtime(SELF:ByteXor(b, c)))
        buf[i + 2] := SELF:ByteXor(SELF:ByteXor(buf[i + 2], e), SELF:rj_xtime(SELF:ByteXor(c, d)))
        buf[i + 3] := SELF:ByteXor(SELF:ByteXor(buf[i + 3], e), SELF:rj_xtime(SELF:ByteXor(d, a)))
        i += 4
    ENDDO                                
RETURN

PROTECT METHOD aes_mixColumns_inv(buf AS BYTE PTR) AS VOID PASCAL CLASS AES256
    LOCAL i AS DWORD
    LOCAL a, b, c, d, e, x, y, z AS BYTE 
                                
    i := 1
    DO WHILE i < 17
        a := buf[i]
        b := buf[i + 1]
        c := buf[i + 2]
        d := buf[i + 3]         
        e := SELF:ByteXor(SELF:ByteXor(SELF:ByteXor(a, b), c), d)
        z := SELF:rj_xtime(e)  
        x := SELF:ByteXor(e, SELF:rj_xtime(SELF:rj_xtime(SELF:ByteXor(SELF:ByteXor(z, a), c))))
        y := SELF:ByteXor(e, SELF:rj_xtime(SELF:rj_xtime(SELF:ByteXor(SELF:ByteXor(z, b), d))))
        buf[i] := SELF:ByteXor(SELF:ByteXor(buf[i], x), SELF:rj_xtime(SELF:ByteXor(a, b)))
        buf[i + 1] := SELF:ByteXor(SELF:ByteXor(buf[i + 1], y), SELF:rj_xtime(SELF:ByteXor(b, c)))
        buf[i + 2] := SELF:ByteXor(SELF:ByteXor(buf[i + 2], x), SELF:rj_xtime(SELF:ByteXor(c, d)))
        buf[i + 3] := SELF:ByteXor(SELF:ByteXor(buf[i + 3], y), SELF:rj_xtime(SELF:ByteXor(d, a)))
        i += 4
    ENDDO                                
RETURN

PROTECT METHOD aes_expandDecKey(k AS BYTE PTR, rc REF BYTE) AS VOID PASCAL CLASS AES256
    LOCAL i AS DWORD
    
    i := 29
    DO WHILE i > 17
        k[i] := SELF:ByteXor(k[i], k[i - 4])
        k[i + 1] := SELF:ByteXor(k[i + 1], k[i - 3])
        k[i + 2] := SELF:ByteXor(k[i + 2], k[i - 2])
        k[i + 3] := SELF:ByteXor(k[i + 3], k[i - 1])
        i -= 4
    ENDDO
    
    k[17] := SELF:ByteXor(k[17], sbox[k[13] + 1]) 
    k[18] := SELF:ByteXor(k[18], sbox[k[14] + 1]) 
    k[19] := SELF:ByteXor(k[19], sbox[k[15] + 1]) 
    k[20] := SELF:ByteXor(k[20], sbox[k[16] + 1])
    
    i := 13
    DO WHILE i > 1
        k[i] := SELF:ByteXor(k[i], k[i - 4])
        k[i + 1] := SELF:ByteXor(k[i + 1], k[i - 3])
        k[i + 2] := SELF:ByteXor(k[i + 2], k[i - 2])
        k[i + 3] := SELF:ByteXor(k[i + 3], k[i - 1])
        i -= 4
    ENDDO
    
    rc := SELF:FD(rc)
    k[1] := SELF:ByteXor(SELF:ByteXor(k[1], sbox[k[30] + 1]), rc)  
    k[2] := SELF:ByteXor(k[2], sbox[k[31] + 1]) 
    k[3] := SELF:ByteXor(k[3], sbox[k[32] + 1]) 
    k[4] := SELF:ByteXor(k[4], sbox[k[29] + 1]) 
RETURN 

PROTECT METHOD ByteAnd(b1 AS BYTE, b2 AS BYTE) AS BYTE PASCAL CLASS AES256
RETURN LoByte(_AND(DWORD(b1), DWORD(b2)))

PROTECT METHOD ByteOr(b1 AS BYTE, b2 AS BYTE) AS BYTE PASCAL CLASS AES256
RETURN LoByte(_OR(DWORD(b1), DWORD(b2)))

PROTECT METHOD ByteXor(b1 AS BYTE, b2 AS BYTE) AS BYTE PASCAL CLASS AES256
RETURN LoByte(_XOR(DWORD(b1), DWORD(b2)))

PROTECT METHOD ByteNot(b1 AS BYTE) AS BYTE PASCAL CLASS AES256
RETURN LoByte(_NOT(DWORD(b1)))

PROTECT METHOD FD(x AS BYTE) AS BYTE PASCAL CLASS AES256
    LOCAL y, w AS BYTE 
    
    y := SELF:ByteShiftR(x, 1)
    w := SELF:ByteAnd(x, 1)
RETURN SELF:ByteXor(y, IIF(w > 0, 0x8D, 0)) 

PROTECT METHOD aes_addRoundKeyX(buf AS BYTE PTR, key AS BYTE PTR, x AS DWORD) AS VOID PASCAL CLASS AES256
    LOCAL i AS DWORD 
    
    FOR i := 16 DOWNTO 1
        buf[i] := SELF:ByteXor(buf[i], key[i + x])
    NEXT
RETURN

PROTECT METHOD ByteArray2Str(buf AS BYTE PTR, dwLen AS DWORD, lRemoveTrailingNulls := FALSE AS LOGIC) AS STRING PASCAL CLASS AES256
    LOCAL cOut AS STRING
    
    IF lRemoveTrailingNulls
        DO WHILE dwLen >= 1
            IF buf[dwLen] == 0
                dwLen -= 1
            ELSE
                EXIT
            ENDIF
        ENDDO
    ENDIF
    
    cOut := Mem2String(buf, dwLen)
RETURN cOut

PROTECT METHOD ByteShiftL(b1 AS BYTE, b2 AS BYTE) AS BYTE PASCAL CLASS AES256
    LOCAL w1, w2 AS DWORD
    
    w1 := b1
    w2 := b2
RETURN LoByte(w1 << w2)

PROTECT METHOD ByteShiftR(b1 AS BYTE, b2 AS BYTE) AS BYTE PASCAL CLASS AES256
    LOCAL w1, w2 AS DWORD
    
    w1 := b1
    w2 := b2
RETURN LoByte(w1 >> w2) 

//PROCEDURE prbuf(label AS STRING, buf AS ARRAY, maxlen AS INT)
//    LOCAL s := "" AS STRING 
//    LOCAL i AS INT
//    
//     IF maxlen == 0
//         FOR i := 1 TO ALen(buf)
//             s += AsString(buf[i]) + " "
//         NEXT
//     ELSE
//         FOR i := 1 TO maxlen
//             s += AsString(buf[i]) + " "
//         NEXT
//     ENDIF
//     
//     ? "VO " + label
//     ? s
//     _Wait()
//RETURN
    
PROTECT METHOD EncryptEcbArray(buf AS BYTE PTR) AS VOID PASCAL CLASS AES256
    LOCAL rcon AS BYTE        
    LOCAL i AS DWORD
    
    IF  SELF:lInitialised
        SELF:aes_addRoundKey_cpy(buf, SELF:enckey, SELF:key) 
        rcon := 1
        i := 1
        
        DO WHILE i < 14
            SELF:aes_subBytes(buf) 
            SELF:aes_shiftRows(buf) 
            SELF:aes_mixColumns(buf)
            IF SELF:ByteAnd(i, 1) > 0
                SELF:aes_addRoundKeyX(buf, SELF:key, 16)
            ELSE
                SELF:aes_expandEncKey(SELF:key, @rcon)
                SELF:aes_addRoundKeyX(buf, SELF:key, 0)
            ENDIF
            i += 1
        ENDDO
        
        SELF:aes_subBytes(buf)
        SELF:aes_shiftRows(buf)
        SELF:aes_expandEncKey(SELF:key, @rcon)
        SELF:aes_addRoundKeyX(buf, SELF:key, 0)
    ENDIF    
RETURN 

PROTECT METHOD DecryptEcbArray(buf AS BYTE PTR) AS VOID PASCAL CLASS AES256
    LOCAL rcon AS BYTE        
    LOCAL i AS DWORD
    
    IF  SELF:lInitialised
        SELF:aes_addRoundKey_cpy(buf, SELF:deckey, SELF:key)
        SELF:aes_shiftRows_inv(buf)
        SELF:aes_subBytes_inv(buf)
        
        rcon := 0x80
        i := 13         
        
        DO WHILE i >= 1  
            IF SELF:ByteAnd(i, 1) > 0
                SELF:aes_expandDecKey(SELF:key, @rcon)
                SELF:aes_addRoundKeyX(buf, SELF:key, 16)
            ELSE
                SELF:aes_addRoundKeyX(buf, SELF:key, 0)
            ENDIF
            SELF:aes_mixColumns_inv(buf)
            SELF:aes_shiftRows_inv(buf)
            SELF:aes_subBytes_inv(buf)
            i -= 1
        ENDDO
        
        SELF:aes_addRoundKeyX(buf, SELF:key, 0)
    ENDIF
RETURN

METHOD Encrypt(cPlainText AS STRING) AS STRING PASCAL CLASS AES256
    LOCAL cOut := "" AS STRING
    LOCAL buf AS BYTE PTR
    LOCAL dwLen, dwExtra, dwOffset AS DWORD
    
    dwLen := SLen(cPlainText)
    IF (dwLen > 0) .AND. SELF:lInitialised
        dwExtra := dwLen % 16
        
        IF dwExtra > 0
            dwExtra := 16 - dwExtra
            cPlainText += Replicate(CHR(0), dwExtra)
            dwLen += dwExtra
        ENDIF
        
        buf := SELF:Str2ByteArray(cPlainText, dwLen)
        
        IF buf != NULL_PTR
            dwOffset := 0
            DO WHILE dwOffset <= (dwLen - 16)
                SELF:EncryptEcbArray(buf + dwOffset)
                dwOffset += 16
            ENDDO  
            
            cOut := SELF:ByteArray2Str(buf, dwLen)
            MemFree(buf)
        ENDIF
    ENDIF
    
RETURN cOut

METHOD Decrypt(cCipherText AS STRING, lRemoveTrailingNulls := FALSE AS LOGIC) AS STRING PASCAL CLASS AES256
    LOCAL cOut := "" AS STRING 
    LOCAL buf AS BYTE PTR
    LOCAL dwLen, dwOffset AS DWORD
    
    dwLen := SLen(cCipherText)
    IF (dwLen > 0) .AND. ((dwLen % 16) == 0) .AND. SELF:lInitialised
        buf := SELF:Str2ByteArray(cCipherText, dwLen)
        
        IF buf != NULL_PTR
            dwOffset := 0
            DO WHILE dwOffset <= (dwLen - 16)
                SELF:DecryptEcbArray(buf + dwOffset)
                dwOffset += 16
            ENDDO  
            
            cOut := SELF:ByteArray2Str(buf, dwLen, lRemoveTrailingNulls)
            MemFree(buf)
        ENDIF
    ENDIF
RETURN cOut 

METHOD Axit() CLASS AES256
    IF SELF:lInitialised 
        SELF:FreeArrays()
    ENDIF             
    UnRegisterAxit(SELF)
RETURN NIL

PROTECT METHOD FreeArrays() AS VOID PASCAL CLASS AES256
    IF SELF:sboxinv != NULL_PTR 
        MemFree(SELF:sboxinv)  
    ENDIF
    IF SELF:sbox != NULL_PTR 
        MemFree(SELF:sbox)
    ENDIF
    IF SELF:deckey != NULL_PTR 
        MemFree(SELF:deckey)
    ENDIF
    IF SELF:enckey != NULL_PTR 
        MemFree(SELF:enckey)
    ENDIF
    IF SELF:key != NULL_PTR 
        MemFree(SELF:key)
    ENDIF
RETURN

[toc] | [prev] | [next] | [standalone]


#8852

FromFranz Rachbauer <office@rachbauer.org>
Date2015-12-30 09:36 +0100
Message-ID<n6053g$fuj$1@speranza.aioe.org>
In reply to#8850
Hi Phil,
tried new code but same behaviour: crashes when cKey <> 32 byte.
Regards, Franz

[toc] | [prev] | [next] | [standalone]


#8855

FromSherlock <sherlock@sherlock.com.au>
Date2015-12-30 13:53 -0800
Message-ID<587a888c-f666-494b-89d6-3d2c12b95a80@googlegroups.com>
In reply to#8852
Franz

AES has key lengths of 16, 24 or 32 

AEL256 is not a 256 key you provide.
Reading the 32 is the max of a DWORD used in the cipher.


Phil

[toc] | [prev] | [next] | [standalone]


#8856

FromFranz Rachbauer <office@rachbauer.org>
Date2015-12-31 09:32 +0100
Message-ID<n62p7v$pb8$1@speranza.aioe.org>
In reply to#8855
Hi Phil,

Am 30.12.2015 um 22:53 schrieb Sherlock:
> Franz
>
> AES has key lengths of 16, 24 or 32
So I think, these are the key lengths (multiplied by 8) defined for 
AES128, AES192 and AES256, right?

Franz

[toc] | [prev] | [next] | [standalone]


#8857

FromSherlock <sherlock@sherlock.com.au>
Date2015-12-31 01:11 -0800
Message-ID<78a02f3c-17cd-4571-a80c-d56b9e238ba1@googlegroups.com>
In reply to#8856
Franz 

snip[ AES has key lengths of 16, 24 or 32
snip[ So I think, these are the key lengths (multiplied by 8) defined for 
 AES128, AES192 and AES256, right? ]
 
You are probably correct.. and AES256 with 32 byte key, which is 2256 key possibilities. I read 232 is about 4.3 billion and it grows exponentially after that.

So why would want to use something greater than 32 bytes ??


To recap from a previous post..   that 256bit encryption is the mathematical equivalent of 2256 key possibilities. To put that into perspective, 232 is about 4.3 billion, and it keeps growing exponentially after that. What does this mean though? Well simply put, let's say hypothetically all the super computers in the world (the ultimate brute force attack) decided to group up and tasked themselves to decrypt your AES-256 key so they could access your data. Assume they could look at 250 keys per second (which is approximately one quadrillion keys/second - a very generous assumption). A year is approximately 31,557,600 seconds. This means that by using the one billion super computers required to do this, they could check about 275 keys per year. At this rate it would take these computers 234 years (the age of our universe) to look at less than .01% of the entire key possibilities. The bottom line? No one will be breaking your encrypted data 

Phil

[toc] | [prev] | [next] | [standalone]


#8861

FromSherlock <sherlock@sherlock.com.au>
Date2016-01-01 04:32 -0800
Message-ID<60e8a578-c530-404c-8646-7d2763aedb88@googlegroups.com>
In reply to#8857
Franz 

snip[ AES has key lengths of 16, 24 or 32
snip[ So I think, these are the key lengths (multiplied by 8) defined for 
  AES128, AES192 and AES256, right? ]

Further research on this and possible combinations. [ ^ power of ] 
AES128  3.4 x 10^38   
AES192  6.2 x 10^57
AES256  1.1 x 10^77

Time to crack AES128 using Faster supercomputer (as per Wikipedia): 10.51 Pentaflops = 10.51 x 10^15 Flops [Flops = Floating point operations per second works out to be 1 billion years which is 1.02 x 10^18

Technical paper from Seagate about cracking AES 128 said if every person on the planet owns 10 computers.

* Say there are 7 billion people on the planet with 10 computers.
* Each of these computers can test 1 billion key combinations per second.
* On average, you can crack the key after testing 50% of the possibilities.

Therefore the earth's population can crack one encryption key in 77,000,000,000,000,000,000,000,000 years!

This is the 128 version and we now have AES 256

I think your safe. (g)

ps. According to research AES has never been cracked.


Phil

[toc] | [prev] | [next] | [standalone]


#8862

FromFranz Rachbauer <office@rachbauer.org>
Date2016-01-01 18:16 +0100
Message-ID<n66cai$q2d$1@speranza.aioe.org>
In reply to#8857
Hi Phil,
first: thank you for good explanation!
 >
> So why would want to use something greater than 32 bytes ??
>
Not greater than 32 bytes, for my purposes a key less than 32 should be 
sufficient.

Happy New Year!

Franz

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.clipper.visual-objects


csiph-web