Archived Forum Post

Index of archived forum posts

Question:

ckEncryptFile ran twice on same file decrypts file?

Feb 24 '17 at 17:48

Why does running ckEncryptFile on a file twice Decrypt that file?

' Drop a command button and a text box onto a form and run

Private Sub Command1_Click()

' Creating a text file filled with 1,000 (1's)
Const ForReading = 1, ForWriting = 2, ForAppenting = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateTextFile("ones.txt", 2, 0)
f.write String(1000, "1")
f.Close

' load Contents of ones.txt file into textbox for verification
Set f = fso.openTextFile("ones.txt", 1, 0)
Do While f.AtEndofStream <> True
    Text1 = Text1 & f.readline
Loop
f.Close

' Setup the Chilkat Methods

Dim crypt As New ChilkatCrypt2
success = crypt.UnlockComponent("Anything-30-days")
If (success <> 1) Then
    MsgBox crypt.LastErrorText
    End
End If
crypt.VerboseLogging = True

crypt.CryptAlgorithm = "chacha20"
crypt.KeyLength = 256
crypt.EncodingMode = "hex"
Dim ivHex As String
ivHex = "000000000000000000000002"
crypt.SetEncodedIV ivHex, "hex"
crypt.InitialCount = 42
Dim keyHex As String
keyHex = "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0"
crypt.SetEncodedKey keyHex, "hex"
crypt.HeartbeatMs = 500

' ENCRYPT
strSrcFile = "ones.txt" ' A text file filled with 1's
strDestFile = "cyphertext.txt" ' Gets encrypted
success = crypt.CkEncryptFile(strSrcFile, strDestFile)
If (success <> 1) Then
    MsgBox crypt.LastErrorText
End If
log1 = crypt.LastErrorText
' load Contents of "cyphertext.txt" file into textbox for verification
Set f = fso.openTextFile("cyphertext.txt", 1, 0)    ' Notice we are loading the encrypted file
Text1 = Text1 & "********************************Encrypted section****************************"
Do While f.AtEndofStream <> True
    Text1 = Text1 & f.readline
Loop
f.Close

' Mine correctly shows an encrypted file!

' Now encrypt the encrypted file again strSrcFile = "cyphertext.txt" ' should be encrypted strDestFile = "cypherX2.txt" ' should be Really encrypted ;) success = crypt.CkEncryptFile(strSrcFile, strDestFile) If (success <> 1) Then MsgBox crypt.LastErrorText End If log2 = crypt.LastErrorText ' load Contents of cypherX2.txt file into textbox for verification Set f = fso.openTextFile("cypherX2.txt", 1, 0) ' Notice we are now loading the Double encrypted file Text1 = Text1 & "****Double Encrypted section****" Do While f.AtEndofStream <> True Text1 = Text1 & f.readline Loop f.Close ' Mine shows cypherX2.txt is clear text again and NOT encrypted! ??? Debug.Print log1 Debug.Print "++++++++++++++++++++++++++" Debug.Print log2 MsgBox "Done!"

End Sub

logs:

ChilkatLog:
  CkEncryptFile:
    DllDate: Dec 27 2016
    ChilkatVersion: 9.5.0.65
    UnlockPrefix: RAGSOFCrypt
    Architecture: Little Endian; 32-bit
    Language: ActiveX
    VerboseLogging: 1
    ckEncDecFile:
      inputFile: [ones.txt]
      outputFile: [cyphertext.txt]
      encryptionMethod: chacha
      inFileSize: 1000
    --ckEncDecFile
    Success.
  --CkEncryptFile
--ChilkatLog

++++++++++++++++++++++++++ ChilkatLog: CkEncryptFile: DllDate: Dec 27 2016 ChilkatVersion: 9.5.0.65 UnlockPrefix: RAGSOFCrypt Architecture: Little Endian; 32-bit Language: ActiveX VerboseLogging: 1 ckEncDecFile: inputFile: [cyphertext.txt] outputFile: [cypherX2.txt] encryptionMethod: chacha inFileSize: 1000 --ckEncDecFile Success. --CkEncryptFile --ChilkatLog


Answer

Yes, that's a property of the encryption algorithm. Effectively, chacha20 is an xor of a keystream, which is effectively a PRNG (pseudo-random number generator).