Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

Code-Beispiel

Code-Beispiele » Verschlüsselung

Dateiverschlüsselung mit XTEA

Lizenz:Erster Autor:Letzte Bearbeitung:
k. A.MitgliedNeo7530 07.11.2012

Das ist ein kleines Programm, womit man Dateien jeglicher Art im XTEA-CBC-Mode verschlüsseln kann. Das 128-Bit-Password wird als MD5 gehasht. Codierte Dateien enden auf ".tea", das Programm erkennt auch an dieser Extension, dass die Datei bereits verschlüsselt ist, und geht in den Decoder-Modus, um die Datei zu entschlüsseln.

Viel Spass damit.

'##### last modified: 2011-12-21 ######## added password-check ###########

Declare sub enc (a As UInteger, b As UInteger, round As UInteger)
Declare sub dec (a As UInteger, b As UInteger, round As UInteger)
Declare Sub passwd
Declare Sub getblock
Declare Sub putblock
Declare Sub cbc
Declare Sub iv_gen
Declare Sub iv_get
Declare Sub final_e
Declare Sub final_d
Declare Sub passgen
Declare Sub passcheck


Dim Shared As UInteger text(0 To 1), ki(0 To 3), cbct(0 To 1) ,s, cbct_n(0 To 1), pc
Dim Shared As UByte pb(0 To 7)

#Include "md5.bas"

passwd
?

weiter:
Open Command(1) For Binary Access Read As #1
If Mid(Command(1),Len(Command(1))-3,4) = ".tea" Then
     s = 1
Else
    Open Command(1) + ".tea" For Binary Access write As #2: s = 0
EndIf

If s=0 Then 'encrypt
    iv_gen
    passgen
EndIf

If s=1 Then 'decrypt
    iv_get
    passcheck
    Open Mid(Command(1),1,Len(Command(1))-4) For Binary Access write As #2
EndIf


Do Until Eof(1)

    pc = Lof(1) - Loc(1)
    If pc < 8 And s = 0 Then final_e Else getblock   'if text-block < 64bit => padding

    If s=0 Then 'encrypt
        text(0) Xor= cbct(0) : text(1) Xor= cbct(1) ' Plaintext XOR IV (or last Crypttext-Block)
        enc (text(0),text(1),31)                            'encrypt
        cbct(0) = text(0) : cbct(1) = text(1)           'Crypttext = next IV (CBC)
        putblock                                                    'Write block to file
        If LOC(1) = Lof(1) Then pc = 0 : final_e        'add last Padding-block
    EndIf

    If s=1 Then 'decrypt
        cbct(0) = cbct_n(0): cbct(1) = cbct_n(1)        'get next cbc-block from last-round
        cbct_n(0) = text(0): cbct_n(1) = text(1)        'next round cbc-block = ciphertext
        dec (text(0),text(1),31)                            'decrypt
        text(0) Xor= cbct(0): text(1) Xor= cbct(1)  'text XOR cbc-block
        If pc = 8 Then final_d                              'reverse-padding
        putblock                                                    'write Plaintext to file
    EndIf

Loop
?
? "ready..."

Sub passgen
    text(0) = &h56881ade
    text(1) = &h5b621734
    text(0) Xor= cbct(0) : text(1) Xor= cbct(1)
    enc (text(0),text(1),31)
    cbct(0) = text(0) : cbct(1) = text(1)           'Crypttext = next IV (CBC)
    putblock                                                    'Write block to file
End Sub

Sub passcheck
    getblock
    cbct(0) = cbct_n(0): cbct(1) = cbct_n(1)        'get next cbc-block from last-round
    cbct_n(0) = text(0): cbct_n(1) = text(1)        'next round cbc-block = ciphertext
    dec (text(0),text(1),31)                            'decrypt
    text(0) Xor= cbct(0): text(1) Xor= cbct(1)  'text XOR cbc-block
    If (text(0) Xor text(1)) = &h0dea0dea Then
        Exit Sub

    Else ? "Wrong Password!!!" : Sleep : End
    EndIf
End Sub

Sub final_e
    getblock                                                        'get Plaintext
    text(1) Or= (8-pc) Shl 24                               'add padding-length
'   ? Hex(text(0),8);Hex(text(1),8)
    text(0) Xor= cbct(0) : text(1) Xor= cbct(1)     'CBC
    enc (text(0),text(1),31)                                'crypt
    putblock                                                        'write to file

    Close #1, #2
    ? "ready...."
    End
End Sub

Sub final_d

        For i As Integer = 0 To 1                           'integer to byte-splitting
            pb(i*4) = text(i) And &hFF
            pb(i*4+1) Or= text(i) Shr 8
            pb(i*4+2) Or= text(i) Shr 16
            pb(i*4+3) Or= text(i) Shr 24
        Next

        For i As Integer = 0 To 7-pb(7)
            Put #2, ,pb(i)                                      'put byte-by-byte exept padding bytes
        Next
        Close #1,#2
        ? "ready...": End

End Sub

Sub iv_gen
    Randomize (-1,0)
    For i As Integer = 0 To 1
        cbct(i) = Int(rnd(1)*&hFFFF) Shl 16 + Int(rnd(1)*&hFFFF)
    Next
        Put #2, , cbct(0)
        Put #2, , cbct(1)
End Sub

Sub iv_get
    Get #1, , cbct_n(0)
    Get #1, , cbct_n(1)
End Sub

Sub passwd:
    If Command$(2) ="" Then
    Dim As String test, x
    ? "password: ";
    Do:
        x = InKey$: IF x = CHR$(13) THEN EXIT DO  'Exit with Enter Key
        If x <> "" THEN test = test + x: PRINT "*";
    Loop
        Dim As String result = createHash(test)
    Else
        Dim As String result = createFileHash(Command$(2))
    End If
    For i As Integer = 0 TO 3
        ki(i) = (ki(i) + lpszMD5(i*4)) Shl 24 + lpszMD5(i*4+1) Shl 16 + lpszMD5(i*4+2) Shl 8 + lpszMD5(i*4+3)
    Next
    ?
    ?
End Sub

Sub getblock
    Get #1, , text(0)
    Get #1, , text(1)
End Sub

Sub putblock
    Put #2, , text(0)
    Put #2, , text(1)
End Sub

Sub enc (a As UInteger, b As UInteger, round As UInteger)
    Dim As UInteger i, v0, v1, sum, delta

    v0=a: v1=b: sum=0: delta=&h9E3779B9
    for i = 0 To round
        v0 += ((((v1 Shl 4) xor (v1 Shr 5)) + v1) Xor (sum + ki(sum And 3)))

        sum = sum + delta
        v1 += ((((v0 Shl 4) xor (v0 Shr 5)) + v0) Xor (sum + ki((sum Shr 11) And 3)))

    Next
    text(0) = v0: text(1)=v1
End sub


Sub dec (a As UInteger, b As UInteger, round As Uinteger)
        Dim As UInteger i, v0, v1, delta, sum

    v0=a: v1=b: delta=&h9E3779B9
        sum = (round+1)* delta
    for i = 0 To round

        v1 -= ((((v0 Shl 4) xor (v0 Shr 5)) + v0) Xor (sum + ki((sum Shr 11) And 3)))
        sum -= delta
        v0 -= ((((v1 Shl 4) xor (v1 Shr 5)) + v1) Xor (sum + ki(sum And 3)))
    Next

    text(0) = v0: text(1)=v1
End sub

Zusätzliche Informationen und Funktionen
  • Das Code-Beispiel wurde am 07.11.2012 von MitgliedNeo7530 angelegt.
  • Die aktuellste Version wurde am 07.11.2012 von MitgliedNeo7530 gespeichert.
  Bearbeiten Bearbeiten  

  Versionen Versionen