## AES Encryption/Decryption Algorithm

Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:

### AES Encryption/Decryption Algorithm

My little project for today. Its not quite as optomized as MARS, but MARS was a CPU eating monster. This cypher is quite neat because it operates on an easy to understand grid. Easier than MARS too. Decrypts and Encrypts, keys must be 128 bits long. Text can be any size.

Code: Select all

`' AES Encryption Implementation by: Chris Brown(2007) aka Zamaster'' -Takes plain text and converts it to encrypted ASCII' -Keys must be 128 bits in size, or 32 hex places/16 char places' -Set ed in RIJNDAEL_Encrypt to 2 for decryption, 1 for encryption'Sub RIJNDAEL_ConvertEXPtoKey(index as integer, ekey() as ubyte, tkey() as ubyte)    Dim as ubyte x,y,b    b = (index-1)*4    For y = 1 to 4        For x = 1 to 4            tkey(x,y) = ekey(b+x,y)        Next x    Next yEnd SubFunction RIJNDAEL_InverseS(v as ubyte) as ubyte    Static as ubyte RSBOX(0 to 255) = {_    &H52, &H09, &H6A, &HD5, &H30, &H36, &HA5, &H38, &HBF, &H40, &HA3, &H9E, &H81, &HF3, &HD7, &HFB, _    &H7C, &HE3, &H39, &H82, &H9B, &H2F, &HFF, &H87, &H34, &H8E, &H43, &H44, &HC4, &HDE, &HE9, &HCB, _    &H54, &H7B, &H94, &H32, &HA6, &HC2, &H23, &H3D, &HEE, &H4C, &H95, &H0B, &H42, &HFA, &HC3, &H4E, _    &H08, &H2E, &HA1, &H66, &H28, &HD9, &H24, &HB2, &H76, &H5B, &HA2, &H49, &H6D, &H8B, &HD1, &H25, _       &H72, &HF8, &HF6, &H64, &H86, &H68, &H98, &H16, &HD4, &HA4, &H5C, &HCC, &H5D, &H65, &HB6, &H92, _    &H6C, &H70, &H48, &H50, &HFD, &HED, &HB9, &HDA, &H5E, &H15, &H46, &H57, &HA7, &H8D, &H9D, &H84, _    &H90, &HD8, &HAB, &H00, &H8C, &HBC, &HD3, &H0A, &HF7, &HE4, &H58, &H05, &HB8, &HB3, &H45, &H06, _    &HD0, &H2C, &H1E, &H8F, &HCA, &H3F, &H0F, &H02, &HC1, &HAF, &HBD, &H03, &H01, &H13, &H8A, &H6B, _    &H3A, &H91, &H11, &H41, &H4F, &H67, &HDC, &HEA, &H97, &HF2, &HCF, &HCE, &HF0, &HB4, &HE6, &H73, _    &H96, &HAC, &H74, &H22, &HE7, &HAD, &H35, &H85, &HE2, &HF9, &H37, &HE8, &H1C, &H75, &HDF, &H6E, _       &H47, &HF1, &H1A, &H71, &H1D, &H29, &HC5, &H89, &H6F, &HB7, &H62, &H0E, &HAA, &H18, &HBE, &H1B, _       &HFC, &H56, &H3E, &H4B, &HC6, &HD2, &H79, &H20, &H9A, &HDB, &HC0, &HFE, &H78, &HCD, &H5A, &HF4, _    &H1F, &HDD, &HA8, &H33, &H88, &H07, &HC7, &H31, &HB1, &H12, &H10, &H59, &H27, &H80, &HEC, &H5F, _    &H60, &H51, &H7F, &HA9, &H19, &HB5, &H4A, &H0D, &H2D, &HE5, &H7A, &H9F, &H93, &HC9, &H9C, &HEF, _    &HA0, &HE0, &H3B, &H4D, &HAE, &H2A, &HF5, &HB0, &HC8, &HEB, &HBB, &H3C, &H83, &H53, &H99, &H61, _    &H17, &H2B, &H04, &H7E, &HBA, &H77, &HD6, &H26, &HE1, &H69, &H14, &H63, &H55, &H21, &H0C, &H7D}    Dim as integer x    x = RSBOX((v AND &HF0) + (v AND &HF))    Return xEnd FunctionFunction RIJNDAEL_S(v as ubyte) as ubyte    Static as ubyte RSBOX(0 to 255) = {_    &H63, &H7C, &H77, &H7B, &HF2, &H6B, &H6F, &HC5, &H30, &H01, &H67, &H2B, &HFE, &HD7, &HAB, &H76, _    &HCA, &H82, &HC9, &H7D, &HFA, &H59, &H47, &HF0, &HAD, &HD4, &HA2, &HAF, &H9C, &HA4, &H72, &HC0, _    &HB7, &HFD, &H93, &H26, &H36, &H3F, &HF7, &HCC, &H34, &HA5, &HE5, &HF1, &H71, &HD8, &H31, &H15, _    &H04, &HC7, &H23, &HC3, &H18, &H96, &H05, &H9A, &H07, &H12, &H80, &HE2, &HEB, &H27, &HB2, &H75, _    &H09, &H83, &H2C, &H1A, &H1B, &H6E, &H5A, &HA0, &H52, &H3B, &HD6, &HB3, &H29, &HE3, &H2F, &H84, _    &H53, &HD1, &H00, &HED, &H20, &HFC, &HB1, &H5B, &H6A, &HCB, &HBE, &H39, &H4A, &H4C, &H58, &HCF, _    &HD0, &HEF, &HAA, &HFB, &H43, &H4D, &H33, &H85, &H45, &HF9, &H02, &H7F, &H50, &H3C, &H9F, &HA8, _    &H51, &HA3, &H40, &H8F, &H92, &H9D, &H38, &HF5, &HBC, &HB6, &HDA, &H21, &H10, &HFF, &HF3, &HD2, _    &HCD, &H0C, &H13, &HEC, &H5F, &H97, &H44, &H17, &HC4, &HA7, &H7E, &H3D, &H64, &H5D, &H19, &H73, _    &H60, &H81, &H4F, &HDC, &H22, &H2A, &H90, &H88, &H46, &HEE, &HB8, &H14, &HDE, &H5E, &H0B, &HDB, _       &HE0, &H32, &H3A, &H0A, &H49, &H06, &H24, &H5C, &HC2, &HD3, &HAC, &H62, &H91, &H95, &HE4, &H79, _    &HE7, &HC8, &H37, &H6D, &H8D, &HD5, &H4E, &HA9, &H6C, &H56, &HF4, &HEA, &H65, &H7A, &HAE, &H08, _    &HBA, &H78, &H25, &H2E, &H1C, &HA6, &HB4, &HC6, &HE8, &HDD, &H74, &H1F, &H4B, &HBD, &H8B, &H8A, _    &H70, &H3E, &HB5, &H66, &H48, &H03, &HF6, &H0E, &H61, &H35, &H57, &HB9, &H86, &HC1, &H1D, &H9E, _    &HE1, &HF8, &H98, &H11, &H69, &HD9, &H8E, &H94, &H9B, &H1E, &H87, &HE9, &HCE, &H55, &H28, &HDF, _    &H8C, &HA1, &H89, &H0D, &HBF, &HE6, &H42, &H68, &H41, &H99, &H2D, &H0F, &HB0, &H54, &HBB, &H16}    Dim as integer x    x = RSBOX((v AND &HF0) + (v AND &HF))    Return xEnd FunctionSub RIJNDAEL_SubBytes(T() as ubyte)       Dim as ubyte x,y, temp    For y = 1 to 4        For x = 1 to 4            temp = T(x,y)            T(x,y) = RIJNDAEL_S(temp)        Next x    Next yEnd SubSub RIJNDAEL_InverseSubBytes(T() as ubyte)       Dim as ubyte x,y, temp    For y = 1 to 4        For x = 1 to 4            temp = T(x,y)            T(x,y) = RIJNDAEL_InverseS(temp)        Next x    Next yEnd SubSub RIJNDAEL_ShiftRows(T() as ubyte)    Swap T(1,2), T(4,2)    Swap T(2,2), T(1,2)    Swap T(3,2), T(2,2)    Swap T(1,3), T(3,3)    Swap T(2,3), T(4,3)    Swap T(1,4), T(2,4)    Swap T(3,4), T(4,4)    Swap T(1,4), T(3,4)End SubSub RIJNDAEL_InverseShiftRows(T() as ubyte)    Swap T(1,2), T(2,2)    Swap T(1,2), T(4,2)    Swap T(3,2), T(4,2)    Swap T(1,3), T(3,3)    Swap T(2,3), T(4,3)    Swap T(1,4), T(2,4)    Swap T(2,4), T(3,4)    Swap T(3,4), T(4,4)End SubFunction RIJNDAEL_Gmul(byval a as ubyte, byval b as ubyte) as ubyte    Dim as ubyte p, i, hi    For i = 1 to 8        If (b AND 1) = &H01 Then p = p XOR a        hi = a AND &H80        a = a SHL 1        If hi = &H80 Then a = a XOR &H1B        b = b SHR 1    Next i    Return pEnd FunctionSub RIJNDAEL_MixCollums(T() as ubyte)    Dim as ubyte a(1 to 4), b(1 to 4), i, q, hb    For q = 1 to 4        For i = 1 To 4            a(i) = T(q,i)            hb = T(q,i) And &H80            b(i) = a(i) Shl 1            If hb = &h80 Then b(i) = b(i) XOR &H1B        Next i        T(q,1) = b(1) XOR a(4) XOR a(3) XOR b(2) XOR a(2)        T(q,2) = b(2) XOR a(1) XOR a(4) XOR b(3) XOR a(3)        T(q,3) = b(3) XOR a(2) XOR a(1) XOR b(4) XOR a(4)         T(q,4) = b(4) XOR a(3) XOR a(2) XOR b(1) XOR a(1)    Next qEnd SubSub RIJNDAEL_InverseMixCollums(T() as ubyte)    Dim as ubyte a(1 to 4), q, i    For q = 1 to 4        For i = 1 To 4            a(i) = T(q,i)        Next         T(q,1) = RIJNDAEL_Gmul(a(1),&HE) XOR RIJNDAEL_Gmul(a(2),&HB) XOR RIJNDAEL_Gmul(a(3),&HD) XOR RIJNDAEL_Gmul(a(4),&H9)        T(q,2) = RIJNDAEL_Gmul(a(1),&H9) XOR RIJNDAEL_Gmul(a(2),&HE) XOR RIJNDAEL_Gmul(a(3),&HB) XOR RIJNDAEL_Gmul(a(4),&HD)        T(q,3) = RIJNDAEL_Gmul(a(1),&HD) XOR RIJNDAEL_Gmul(a(2),&H9) XOR RIJNDAEL_Gmul(a(3),&HE) XOR RIJNDAEL_Gmul(a(4),&HB)        T(q,4) = RIJNDAEL_Gmul(a(1),&HB) XOR RIJNDAEL_Gmul(a(2),&HD) XOR RIJNDAEL_Gmul(a(3),&H9) XOR RIJNDAEL_Gmul(a(4),&HE)    Next qEnd SubSub RIJNDAEL_AddKey(T() as ubyte, K() as ubyte)    Dim as ubyte x,y    For y = 1 to 4        For x = 1 to 4            T(x,y) = T(x,y) XOR K(x,y)        Next x    Next yEnd SubSub RIJNDAEL_ExpandKey(K1() as ubyte, K2() as ubyte)    Static as ubyte RCON(1 to 10) = {&H01, &H02, &H04, &H08, &H10, &H20, &H40, &H80, &H1B, &H36}    Dim as integer i, q, a, opt4, opt4m1, opt4m4, o4, om1, om4    K2(1,1) = K1(1,1) XOR RIJNDAEL_S(K1(4,2)) XOR RCON(1)    K2(1,2) = K1(1,2) XOR RIJNDAEL_S(K1(4,3))    K2(1,3) = K1(1,3) XOR RIJNDAEL_S(K1(4,4))    K2(1,4) = K1(1,4) XOR RIJNDAEL_S(K1(4,1))    For i = 2 to 4        For q = 1 to 4            K2(i,q) = K2(i-1,q) XOR K1(i,q)        Next q    Next i        For i = 2 to 10        opt4 = ((i-1) SHL 2) + 1        opt4m1 = opt4 - 1        opt4m4 = opt4 - 4        K2(opt4,1) = K2(opt4m4,1) XOR RIJNDAEL_S(K2(opt4m1,2)) XOR RCON(i)        K2(opt4,2) = K2(opt4m4,2) XOR RIJNDAEL_S(K2(opt4m1,3))        K2(opt4,3) = K2(opt4m4,3) XOR RIJNDAEL_S(K2(opt4m1,4))        K2(opt4,4) = K2(opt4m4,4) XOR RIJNDAEL_S(K2(opt4m1,1))        For q = 2 to 4            o4 = opt4m1 + q            om1 = o4-1            om4 = o4-4            For a = 1 to 4                K2(o4,a) = K2(om1,a) XOR K2(om4,a)            Next a        Next q    Next iEnd SubSub RIJNDAEL_TextToState(byref texts as string, T() as ubyte, dirc as integer)    Dim as string text    text = texts    Dim as ubyte x,y    If dirc = 1 Then        For y = 1 to 4            For x = 1 to 4                T(x,y) = ASC(MID\$(text,(((y-1) SHL 2) + x),1))            Next x        Next y    Else        For y = 1 to 4            For x = 1 to 4                T(y,x) = ASC(MID\$(text,(((y-1) SHL 2) + x),1))            Next x        Next y    Endifend subSub RIJNDAEL_Rotate(K() as ubyte)    Swap K(1,2), K(2,1)    Swap K(1,3), K(3,1)    Swap K(1,4), K(4,1)    Swap K(2,4), K(4,2)    Swap K(3,4), K(4,3)    Swap K(2,3), K(3,2)End Subfunction RIJNDAEL_StateToText(T() as ubyte) as string    Dim as string s    Dim as integer x,y    For y = 1 to 4        For x = 1 to 4            s += CHR\$(T(x,y))        Next x    Next y    Return sEnd functionFunction RIJNDAEL_StrToHex (byref s as string) as string    Dim as string convstr    convstr = s    Dim as uinteger i    Dim as string ftext    For i = 1 To Len(convstr)        ftext += HEX\$(ASC(MID\$(convstr,i,1)),2)    Next i    Return ftextEnd FunctionFunction RIJNDAEL_HexToStr (byref s as string) as string    Dim as string convstr    convstr = s    If LEN(convstr) MOD 2 = 1 Then convstr += "0"    Dim as uinteger i    Dim as string   f    For i = 1 To Len(convstr) Step 2        f += CHR\$(VAL("&H"+MID\$(convstr,i,2)))    Next i    Return fEnd FunctionSub RIJNDAEL_BlockEncrypt(T() as ubyte, K1() as ubyte, K2() as ubyte)    Dim as integer i    Dim as ubyte TempKey(1 to 4, 1 to 4)    RIJNDAEL_AddKey T(), K1()    RIJNDAEL_Rotate T()    For i = 1 to 9        RIJNDAEL_SubBytes T()        RIJNDAEL_ShiftRows T()        RIJNDAEL_MixCollums T()        RIJNDAEL_ConvertEXPtoKey i, K2(), TempKey()        RIJNDAEL_AddKey T(), TempKey()    Next i     RIJNDAEL_SubBytes T()    RIJNDAEL_ShiftRows T()    RIJNDAEL_ConvertEXPtoKey 10, K2(), TempKey()    RIJNDAEL_AddKey T(), TempKey()End SubSub RIJNDAEL_BlockDecrypt(T() as ubyte, K1() as ubyte, K2() as ubyte)    Dim as integer i    Dim as ubyte TempKey(1 to 4, 1 to 4)    RIJNDAEL_ConvertEXPtoKey 10, K2(), TempKey()    RIJNDAEL_AddKey T(), TempKey()    For i = 9 to 1 Step -1        RIJNDAEL_InverseShiftRows T()        RIJNDAEL_InverseSubBytes T()        RIJNDAEL_ConvertEXPtoKey i, K2(), TempKey()        RIJNDAEL_AddKey T(), TempKey()        RIJNDAEL_InverseMixCollums T()    Next i     RIJNDAEL_InverseShiftRows T()    RIJNDAEL_InverseSubBytes T()    RIJNDAEL_AddKey T(), K1()End SubFunction RIJNDAEL_Encrypt(byref pptext as string, byref key as string, ed as integer) as string    If ed < 1 Or ed > 2 Then        Beep        Return "ERROR - NO SUCH ENCRYPTION MODE"    Endif    Dim as string ptext, ctext, mtext    ptext = pptext    Dim as integer lt    lt = LEN(key)    If (lt mod 16 <> 0) Or (lt SHR 4 <> 1) Then         Return "ERROR - INVALID KEY"    Endif    Dim as integer pmod, i    lt = LEN(ptext)    pmod = lt mod 16    If pmod <> 0 or lt < 1 Then        pmod = 16 - pmod          For i = 1 to pmod            ptext += CHR\$(0)        Next i    Endif    lt = LEN(ptext)    lt = lt SHR 4    Dim as ubyte State(1 to 4, 1 to 4), KeyT(1 to 4, 1 to 4), EXPKey(1 to 40, 1 to 4)    RIJNDAEL_TextToState key, KeyT(), 2    RIJNDAEL_ExpandKey KeyT(), EXPKey()    Select Case ed    Case 1        For i = 1 to lt            mtext = MID\$(ptext, ((i-1) SHL 4) + 1, 16)            RIJNDAEL_TextToState mtext, State(), 1            RIJNDAEL_BlockEncrypt State(), KeyT(), EXPKey()            ctext += RIJNDAEL_StateToText(State())        Next i    Case 2        RIJNDAEL_Rotate KeyT()        For i = 1 to lt            mtext = MID\$(ptext, ((i-1) SHL 4) + 1, 16)            RIJNDAEL_TextToState mtext, State(), 1            RIJNDAEL_BlockDecrypt State(), KeyT(), EXPKey()            RIJNDAEL_Rotate State()            ctext += RIJNDAEL_StateToText(State())        Next i    End Select    Return ctextEnd FunctionDim as string a, b, cc = RIJNDAEL_HexToStr("0100000B00000F0000000A0000000000")a = RIJNDAEL_Encrypt("Chris Brown!!!!!", c, 1)Print ab = RIJNDAEL_Encrypt(a, c, 2)Print bsleepend`
Last edited by Zamaster on Jul 10, 2007 6:19, edited 1 time in total.
ejc.cryptography
Posts: 268
Joined: Dec 16, 2006 20:52
Contact:
@Zamaster - Did you get my email? (sent to your gmail account)
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:
No I did not, I may have written it off as spam because I recently received a flurry of college e-mails. Go ahead and re-send it, ill be on the lookout for it.
TbbW
Posts: 348
Joined: Aug 19, 2005 10:08
Contact:
*sends a email to Zamaster that contains the word Viagra*
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:
Cool!!
ejc.cryptography
Posts: 268
Joined: Dec 16, 2006 20:52
Contact:
@zamaster - I've sent the email again to your Gmail account.
csde_rats
Posts: 114
Joined: Jun 02, 2007 21:13
Contact:
English:
Hello,

Deutsch:
Hallo,
Ich bin csde_rats, und ich wollte dich fragen, ob ich auf meiner Homepage csderats.cs.funpic.de deinen AES-Algoryhtmus, den Sourcecode + die compilierte EXE zum download anbieten darf.
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:
Yes! As long as my name stays on it. You might want to allow the user to input a string instead of it just encrypting 'Chris Brown'.

This actually does not require -lang deprecated
csde_rats
Posts: 114
Joined: Jun 02, 2007 21:13
Contact:
Okay!
So you have edited se initial post?
OK
So I compile & zip it now.....

|edit|
The Code with example is the following:

Code: Select all

`' AES Encryption Implementation by: Chris Brown(2007) aka Zamaster'' -Takes plain text and converts it to encrypted ASCII' -Keys must be 128 bits in size, or 32 hex places/16 char places' -Set ed in RIJNDAEL_Encrypt to 2 for decryption, 1 for encryption'declare function aes_encrypt(Text as string, Key as string) as stringdeclare function aes_decrypt(Text as string, Key as string) as stringSub RIJNDAEL_ConvertEXPtoKey(index As Integer, ekey() As Ubyte, tkey() As Ubyte)    Dim As Ubyte x,y,b    b = (index-1)*4    For y = 1 To 4        For x = 1 To 4            tkey(x,y) = ekey(b+x,y)        Next x    Next yEnd SubFunction RIJNDAEL_InverseS(v As Ubyte) As Ubyte    Static As Ubyte RSBOX(0 To 255) = {_    &H52, &H09, &H6A, &HD5, &H30, &H36, &HA5, &H38, &HBF, &H40, &HA3, &H9E, &H81, &HF3, &HD7, &HFB, _    &H7C, &HE3, &H39, &H82, &H9B, &H2F, &HFF, &H87, &H34, &H8E, &H43, &H44, &HC4, &HDE, &HE9, &HCB, _    &H54, &H7B, &H94, &H32, &HA6, &HC2, &H23, &H3D, &HEE, &H4C, &H95, &H0B, &H42, &HFA, &HC3, &H4E, _    &H08, &H2E, &HA1, &H66, &H28, &HD9, &H24, &HB2, &H76, &H5B, &HA2, &H49, &H6D, &H8B, &HD1, &H25, _       &H72, &HF8, &HF6, &H64, &H86, &H68, &H98, &H16, &HD4, &HA4, &H5C, &HCC, &H5D, &H65, &HB6, &H92, _    &H6C, &H70, &H48, &H50, &HFD, &HED, &HB9, &HDA, &H5E, &H15, &H46, &H57, &HA7, &H8D, &H9D, &H84, _    &H90, &HD8, &HAB, &H00, &H8C, &HBC, &HD3, &H0A, &HF7, &HE4, &H58, &H05, &HB8, &HB3, &H45, &H06, _    &HD0, &H2C, &H1E, &H8F, &HCA, &H3F, &H0F, &H02, &HC1, &HAF, &HBD, &H03, &H01, &H13, &H8A, &H6B, _    &H3A, &H91, &H11, &H41, &H4F, &H67, &HDC, &HEA, &H97, &HF2, &HCF, &HCE, &HF0, &HB4, &HE6, &H73, _    &H96, &HAC, &H74, &H22, &HE7, &HAD, &H35, &H85, &HE2, &HF9, &H37, &HE8, &H1C, &H75, &HDF, &H6E, _       &H47, &HF1, &H1A, &H71, &H1D, &H29, &HC5, &H89, &H6F, &HB7, &H62, &H0E, &HAA, &H18, &HBE, &H1B, _       &HFC, &H56, &H3E, &H4B, &HC6, &HD2, &H79, &H20, &H9A, &HDB, &HC0, &HFE, &H78, &HCD, &H5A, &HF4, _    &H1F, &HDD, &HA8, &H33, &H88, &H07, &HC7, &H31, &HB1, &H12, &H10, &H59, &H27, &H80, &HEC, &H5F, _    &H60, &H51, &H7F, &HA9, &H19, &HB5, &H4A, &H0D, &H2D, &HE5, &H7A, &H9F, &H93, &HC9, &H9C, &HEF, _    &HA0, &HE0, &H3B, &H4D, &HAE, &H2A, &HF5, &HB0, &HC8, &HEB, &HBB, &H3C, &H83, &H53, &H99, &H61, _    &H17, &H2B, &H04, &H7E, &HBA, &H77, &HD6, &H26, &HE1, &H69, &H14, &H63, &H55, &H21, &H0C, &H7D}    Dim As Integer x    x = RSBOX((v And &HF0) + (v And &HF))    Return xEnd FunctionFunction RIJNDAEL_S(v As Ubyte) As Ubyte    Static As Ubyte RSBOX(0 To 255) = {_    &H63, &H7C, &H77, &H7B, &HF2, &H6B, &H6F, &HC5, &H30, &H01, &H67, &H2B, &HFE, &HD7, &HAB, &H76, _    &HCA, &H82, &HC9, &H7D, &HFA, &H59, &H47, &HF0, &HAD, &HD4, &HA2, &HAF, &H9C, &HA4, &H72, &HC0, _    &HB7, &HFD, &H93, &H26, &H36, &H3F, &HF7, &HCC, &H34, &HA5, &HE5, &HF1, &H71, &HD8, &H31, &H15, _    &H04, &HC7, &H23, &HC3, &H18, &H96, &H05, &H9A, &H07, &H12, &H80, &HE2, &HEB, &H27, &HB2, &H75, _    &H09, &H83, &H2C, &H1A, &H1B, &H6E, &H5A, &HA0, &H52, &H3B, &HD6, &HB3, &H29, &HE3, &H2F, &H84, _    &H53, &HD1, &H00, &HED, &H20, &HFC, &HB1, &H5B, &H6A, &HCB, &HBE, &H39, &H4A, &H4C, &H58, &HCF, _    &HD0, &HEF, &HAA, &HFB, &H43, &H4D, &H33, &H85, &H45, &HF9, &H02, &H7F, &H50, &H3C, &H9F, &HA8, _    &H51, &HA3, &H40, &H8F, &H92, &H9D, &H38, &HF5, &HBC, &HB6, &HDA, &H21, &H10, &HFF, &HF3, &HD2, _    &HCD, &H0C, &H13, &HEC, &H5F, &H97, &H44, &H17, &HC4, &HA7, &H7E, &H3D, &H64, &H5D, &H19, &H73, _    &H60, &H81, &H4F, &HDC, &H22, &H2A, &H90, &H88, &H46, &HEE, &HB8, &H14, &HDE, &H5E, &H0B, &HDB, _       &HE0, &H32, &H3A, &H0A, &H49, &H06, &H24, &H5C, &HC2, &HD3, &HAC, &H62, &H91, &H95, &HE4, &H79, _    &HE7, &HC8, &H37, &H6D, &H8D, &HD5, &H4E, &HA9, &H6C, &H56, &HF4, &HEA, &H65, &H7A, &HAE, &H08, _    &HBA, &H78, &H25, &H2E, &H1C, &HA6, &HB4, &HC6, &HE8, &HDD, &H74, &H1F, &H4B, &HBD, &H8B, &H8A, _    &H70, &H3E, &HB5, &H66, &H48, &H03, &HF6, &H0E, &H61, &H35, &H57, &HB9, &H86, &HC1, &H1D, &H9E, _    &HE1, &HF8, &H98, &H11, &H69, &HD9, &H8E, &H94, &H9B, &H1E, &H87, &HE9, &HCE, &H55, &H28, &HDF, _    &H8C, &HA1, &H89, &H0D, &HBF, &HE6, &H42, &H68, &H41, &H99, &H2D, &H0F, &HB0, &H54, &HBB, &H16}    Dim As Integer x    x = RSBOX((v And &HF0) + (v And &HF))    Return xEnd FunctionSub RIJNDAEL_SubBytes(T() As Ubyte)       Dim As Ubyte x,y, temp    For y = 1 To 4        For x = 1 To 4            temp = T(x,y)            T(x,y) = RIJNDAEL_S(temp)        Next x    Next yEnd SubSub RIJNDAEL_InverseSubBytes(T() As Ubyte)       Dim As Ubyte x,y, temp    For y = 1 To 4        For x = 1 To 4            temp = T(x,y)            T(x,y) = RIJNDAEL_InverseS(temp)        Next x    Next yEnd SubSub RIJNDAEL_ShiftRows(T() As Ubyte)    Swap T(1,2), T(4,2)    Swap T(2,2), T(1,2)    Swap T(3,2), T(2,2)    Swap T(1,3), T(3,3)    Swap T(2,3), T(4,3)    Swap T(1,4), T(2,4)    Swap T(3,4), T(4,4)    Swap T(1,4), T(3,4)End SubSub RIJNDAEL_InverseShiftRows(T() As Ubyte)    Swap T(1,2), T(2,2)    Swap T(1,2), T(4,2)    Swap T(3,2), T(4,2)    Swap T(1,3), T(3,3)    Swap T(2,3), T(4,3)    Swap T(1,4), T(2,4)    Swap T(2,4), T(3,4)    Swap T(3,4), T(4,4)End SubFunction RIJNDAEL_Gmul(Byval a As Ubyte, Byval b As Ubyte) As Ubyte    Dim As Ubyte p, i, hi    For i = 1 To 8        If (b And 1) = &H01 Then p = p Xor a        hi = a And &H80        a = a Shl 1        If hi = &H80 Then a = a Xor &H1B        b = b Shr 1    Next i    Return pEnd FunctionSub RIJNDAEL_MixCollums(T() As Ubyte)    Dim As Ubyte a(1 To 4), b(1 To 4), i, q, hb    For q = 1 To 4        For i = 1 To 4            a(i) = T(q,i)            hb = T(q,i) And &H80            b(i) = a(i) Shl 1            If hb = &h80 Then b(i) = b(i) Xor &H1B        Next i        T(q,1) = b(1) Xor a(4) Xor a(3) Xor b(2) Xor a(2)        T(q,2) = b(2) Xor a(1) Xor a(4) Xor b(3) Xor a(3)        T(q,3) = b(3) Xor a(2) Xor a(1) Xor b(4) Xor a(4)        T(q,4) = b(4) Xor a(3) Xor a(2) Xor b(1) Xor a(1)    Next qEnd SubSub RIJNDAEL_InverseMixCollums(T() As Ubyte)    Dim As Ubyte a(1 To 4), q, i    For q = 1 To 4        For i = 1 To 4            a(i) = T(q,i)        Next        T(q,1) = RIJNDAEL_Gmul(a(1),&HE) Xor RIJNDAEL_Gmul(a(2),&HB) Xor RIJNDAEL_Gmul(a(3),&HD) Xor RIJNDAEL_Gmul(a(4),&H9)        T(q,2) = RIJNDAEL_Gmul(a(1),&H9) Xor RIJNDAEL_Gmul(a(2),&HE) Xor RIJNDAEL_Gmul(a(3),&HB) Xor RIJNDAEL_Gmul(a(4),&HD)        T(q,3) = RIJNDAEL_Gmul(a(1),&HD) Xor RIJNDAEL_Gmul(a(2),&H9) Xor RIJNDAEL_Gmul(a(3),&HE) Xor RIJNDAEL_Gmul(a(4),&HB)        T(q,4) = RIJNDAEL_Gmul(a(1),&HB) Xor RIJNDAEL_Gmul(a(2),&HD) Xor RIJNDAEL_Gmul(a(3),&H9) Xor RIJNDAEL_Gmul(a(4),&HE)    Next qEnd SubSub RIJNDAEL_AddKey(T() As Ubyte, K() As Ubyte)    Dim As Ubyte x,y    For y = 1 To 4        For x = 1 To 4            T(x,y) = T(x,y) Xor K(x,y)        Next x    Next yEnd SubSub RIJNDAEL_ExpandKey(K1() As Ubyte, K2() As Ubyte)    Static As Ubyte RCON(1 To 10) = {&H01, &H02, &H04, &H08, &H10, &H20, &H40, &H80, &H1B, &H36}    Dim As Integer i, q, a, opt4, opt4m1, opt4m4, o4, om1, om4    K2(1,1) = K1(1,1) Xor RIJNDAEL_S(K1(4,2)) Xor RCON(1)    K2(1,2) = K1(1,2) Xor RIJNDAEL_S(K1(4,3))    K2(1,3) = K1(1,3) Xor RIJNDAEL_S(K1(4,4))    K2(1,4) = K1(1,4) Xor RIJNDAEL_S(K1(4,1))    For i = 2 To 4        For q = 1 To 4            K2(i,q) = K2(i-1,q) Xor K1(i,q)        Next q    Next i       For i = 2 To 10        opt4 = ((i-1) Shl 2) + 1        opt4m1 = opt4 - 1        opt4m4 = opt4 - 4        K2(opt4,1) = K2(opt4m4,1) Xor RIJNDAEL_S(K2(opt4m1,2)) Xor RCON(i)        K2(opt4,2) = K2(opt4m4,2) Xor RIJNDAEL_S(K2(opt4m1,3))        K2(opt4,3) = K2(opt4m4,3) Xor RIJNDAEL_S(K2(opt4m1,4))        K2(opt4,4) = K2(opt4m4,4) Xor RIJNDAEL_S(K2(opt4m1,1))        For q = 2 To 4            o4 = opt4m1 + q            om1 = o4-1            om4 = o4-4            For a = 1 To 4                K2(o4,a) = K2(om1,a) Xor K2(om4,a)            Next a        Next q    Next iEnd SubSub RIJNDAEL_TextToState(Byref texts As String, T() As Ubyte, dirc As Integer)    Dim As String text    text = texts    Dim As Ubyte x,y    If dirc = 1 Then        For y = 1 To 4            For x = 1 To 4                T(x,y) = Asc(Mid\$(text,(((y-1) Shl 2) + x),1))            Next x        Next y    Else        For y = 1 To 4            For x = 1 To 4                T(y,x) = Asc(Mid\$(text,(((y-1) Shl 2) + x),1))            Next x        Next y    EndifEnd SubSub RIJNDAEL_Rotate(K() As Ubyte)    Swap K(1,2), K(2,1)    Swap K(1,3), K(3,1)    Swap K(1,4), K(4,1)    Swap K(2,4), K(4,2)    Swap K(3,4), K(4,3)    Swap K(2,3), K(3,2)End SubFunction RIJNDAEL_StateToText(T() As Ubyte) As String    Dim As String s    Dim As Integer x,y    For y = 1 To 4        For x = 1 To 4            s += Chr\$(T(x,y))        Next x    Next y    Return sEnd FunctionFunction RIJNDAEL_StrToHex (Byref s As String) As String    Dim As String convstr    convstr = s    Dim As Uinteger i    Dim As String ftext    For i = 1 To Len(convstr)        ftext += Hex\$(Asc(Mid\$(convstr,i,1)),2)    Next i    Return ftextEnd FunctionFunction RIJNDAEL_HexToStr (Byref s As String) As String    Dim As String convstr    convstr = s    If Len(convstr) Mod 2 = 1 Then convstr += "0"    Dim As Uinteger i    Dim As String   f    For i = 1 To Len(convstr) Step 2        f += Chr\$(Val("&H"+Mid\$(convstr,i,2)))    Next i    Return fEnd FunctionSub RIJNDAEL_BlockEncrypt(T() As Ubyte, K1() As Ubyte, K2() As Ubyte)    Dim As Integer i    Dim As Ubyte TempKey(1 To 4, 1 To 4)    RIJNDAEL_AddKey T(), K1()    RIJNDAEL_Rotate T()    For i = 1 To 9        RIJNDAEL_SubBytes T()        RIJNDAEL_ShiftRows T()        RIJNDAEL_MixCollums T()        RIJNDAEL_ConvertEXPtoKey i, K2(), TempKey()        RIJNDAEL_AddKey T(), TempKey()    Next i    RIJNDAEL_SubBytes T()    RIJNDAEL_ShiftRows T()    RIJNDAEL_ConvertEXPtoKey 10, K2(), TempKey()    RIJNDAEL_AddKey T(), TempKey()End SubSub RIJNDAEL_BlockDecrypt(T() As Ubyte, K1() As Ubyte, K2() As Ubyte)    Dim As Integer i    Dim As Ubyte TempKey(1 To 4, 1 To 4)    RIJNDAEL_ConvertEXPtoKey 10, K2(), TempKey()    RIJNDAEL_AddKey T(), TempKey()    For i = 9 To 1 Step -1        RIJNDAEL_InverseShiftRows T()        RIJNDAEL_InverseSubBytes T()        RIJNDAEL_ConvertEXPtoKey i, K2(), TempKey()        RIJNDAEL_AddKey T(), TempKey()        RIJNDAEL_InverseMixCollums T()    Next i    RIJNDAEL_InverseShiftRows T()    RIJNDAEL_InverseSubBytes T()    RIJNDAEL_AddKey T(), K1()End SubFunction RIJNDAEL_Encrypt(Byref pptext As String, Byref Key As String, ed As Integer) As String    If ed < 1 Or ed > 2 Then        Beep        Return "ERROR - NO SUCH ENCRYPTION MODE"    Endif    Dim As String ptext, ctext, mtext    ptext = pptext    Dim As Integer lt    lt = Len(Key)    If (lt Mod 16 <> 0) Or (lt Shr 4 <> 1) Then        Return "ERROR - INVALID KEY"    Endif    Dim As Integer pmod, i    lt = Len(ptext)    pmod = lt Mod 16    If pmod <> 0 Or lt < 1 Then        pmod = 16 - pmod         For i = 1 To pmod            ptext += Chr\$(0)        Next i    Endif    lt = Len(ptext)    lt = lt Shr 4    Dim As Ubyte State(1 To 4, 1 To 4), KeyT(1 To 4, 1 To 4), EXPKey(1 To 40, 1 To 4)    RIJNDAEL_TextToState Key, KeyT(), 2    RIJNDAEL_ExpandKey KeyT(), EXPKey()    Select Case ed    Case 1        For i = 1 To lt            mtext = Mid\$(ptext, ((i-1) Shl 4) + 1, 16)            RIJNDAEL_TextToState mtext, State(), 1            RIJNDAEL_BlockEncrypt State(), KeyT(), EXPKey()            ctext += RIJNDAEL_StateToText(State())        Next i    Case 2        RIJNDAEL_Rotate KeyT()        For i = 1 To lt            mtext = Mid\$(ptext, ((i-1) Shl 4) + 1, 16)            RIJNDAEL_TextToState mtext, State(), 1            RIJNDAEL_BlockDecrypt State(), KeyT(), EXPKey()            RIJNDAEL_Rotate State()            ctext += RIJNDAEL_StateToText(State())        Next i    End Select    Return ctextEnd Functionfunction aes_encrypt(Text as string, Key as string) as string    if len(Key) <> 16 then        exit function    end if    dim encrypted as string    encrypted = RIJNDAEL_Encrypt(Text, Key, 1)    return encryptedend functionfunction aes_decrypt(Text as string, Key as string) as string    dim decrypted as string    decrypted = RIJNDAEL_Encrypt(Text, Key, 2)    return decryptedend functionDim As String text, encrypted, decrypteddim key as string * 16Input "Whats to encrypt? ", textdo    Input "Whats the key (length must be 16) ? ", Key    if len(Key) = 16 then        exit do    end ifloop        encrypted = aes_encrypt(Text, Key)print "Encrypted:" + encryptedprint "Decrypted:" + aes_decrypt(encrypted, Key)SleepEnd `

Is this so OK?
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:
yes, its fine. Thanks!
csde_rats
Posts: 114
Joined: Jun 02, 2007 21:13
Contact:
Sorry for the thread-push, but a question was left:
I want to use your code in an closed-source project. In which form should I get you credit (About, Help, ...or whatever?)
redcrab
Posts: 619
Joined: Feb 07, 2006 15:29
Location: France / Luxemburg
Contact:
thanks for tis great contribution !
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:
Lachie Dazdarian
Posts: 2338
Joined: May 31, 2005 9:59
Location: Croatia
Contact:
Oh, hello Zamaster. Remember me?

Shees, you could have find two minutes in the last three weeks to tell me what the hell is going on with you.

Not nice of you as we were in the middle of something. It's like you left me standing on a mine with promises of bringing help in an hour. Well, not really like that, but let's pretend it is.
tslug5
Posts: 1
Joined: Sep 24, 2007 19:33