Что нового

[Криптография] Как зашифровать и расшифровать смешанный (Ru/En) такст из файла в файл ?

urrya

Новичок
Сообщения
159
Репутация
1
Доброго времени уважаемые дамы и гос-да программисты.
Снова есть задачка.
Как зашифровать и расшифровать смешанный (Ru/En) текст из файла в файл ?
Нашёл хороший пример кода на этом форуме от madmasles,
но никак не могу сделать его рабочим для смешанного текста (Рус.+ Англ.)

Код:
$sText = 'Привет! Это бессмысленный текст для _EnCryptDeCrypt()'
$sPass = 'AutoIt Version: 3.3.8.1'
MsgBox(64, 'Info', 'Исходный текст: ' & $sText & @LF & 'Ключ: ' & $sPass)
$sEnCryptText = _EnCryptDeCrypt($sText, $sPass);
MsgBox(64, 'Info', 'Зашифрованный текст: ' & $sEnCryptText);
$sDeCryptText = _EnCryptDeCrypt($sEnCryptText, $sPass)
MsgBox(64, 'Info', 'Расшифрованный текст: ' & $sDeCryptText);

Func _EnCryptDeCrypt($s_Data, $s_Key)
    If (Not $s_Data) Or (Not $s_Key) Then Return SetError(-1, 0, '')
    Local $a_Data, $a_Key, $s_Ret, $i_ChrW, $i_UbData, $i_UbKey

    $a_Data = StringToASCIIArray($s_Data)
    $i_UbData = UBound($a_Data)
    If Not $i_UbData Then Return SetError(1, 0, '')
    $a_Key = StringToASCIIArray($s_Key)
    $i_UbKey = UBound($a_Key)
    If Not $i_UbKey Then Return SetError(2, 0, '')
    For $i = 0 To $i_UbData - 1
        For $j = 0 To $i_UbKey - 1
            $i_ChrW = BitXOR($a_Data[$i], $a_Key[$j], ($i_UbKey - $j) * ($i_UbData - $i))
        Next
        $s_Ret &= ChrW($i_ChrW)
    Next
    Return $s_Ret
EndFunc   ;==>_EnCryptDeCrypt


нет, без записи в файл .txt код прекрасно работает, а вот через текстовый файл нет
:-\
 
Автор
urrya

urrya

Новичок
Сообщения
159
Репутация
1
мнда, если текстовый файл написан в блокноте с "клавы", код работает.
Но вот после моего кода, который создаёт текстовые файлы нет.
Сейчас посмотрю что не так :stars:
 

madmasles

Модератор
Глобальный модератор
Сообщения
7,790
Репутация
2,322
urrya,
Код:
$sText = 'Привет! Это бессмысленный текст для _EnCryptDeCrypt()'
$sPass = 'AutoIt Version: 3.3.8.1'
;~ MsgBox(64, 'Info', 'Исходный текст: ' & $sText & @LF & 'Ключ: ' & $sPass)
$sEnCryptText = _EnCryptDeCrypt($sText, $sPass);
MsgBox(64, 'Info', 'Зашифрованный текст: ' & $sEnCryptText);

$hFile = FileOpen(@ScriptDir & '\EnCrypt.txt', 34)
FileWrite($hFile, $sEnCryptText)
FileClose($hFile)
$sEnCryptText = ''
$hFile = FileOpen(@ScriptDir & '\EnCrypt.txt', 32)
$sEnCryptText = FileRead($hFile)
FileClose($hFile)

$sDeCryptText = _EnCryptDeCrypt($sEnCryptText, $sPass)
MsgBox(64, 'Info', 'Расшифрованный текст: ' & $sDeCryptText);

Func _EnCryptDeCrypt($s_Data, $s_Key)
    If (Not $s_Data) Or (Not $s_Key) Then Return SetError(-1, 0, '')
    Local $a_Data, $a_Key, $s_Ret, $i_ChrW, $i_UbData, $i_UbKey

    $a_Data = StringToASCIIArray($s_Data)
    $i_UbData = UBound($a_Data)
    If Not $i_UbData Then Return SetError(1, 0, '')
    $a_Key = StringToASCIIArray($s_Key)
    $i_UbKey = UBound($a_Key)
    If Not $i_UbKey Then Return SetError(2, 0, '')
    For $i = 0 To $i_UbData - 1
        For $j = 0 To $i_UbKey - 1
            $i_ChrW = BitXOR($a_Data[$i], $a_Key[$j], ($i_UbKey - $j) * ($i_UbData - $i))
        Next
        $s_Ret &= ChrW($i_ChrW)
    Next
    Return $s_Ret
EndFunc   ;==>_EnCryptDeCrypt
 
Автор
urrya

urrya

Новичок
Сообщения
159
Репутация
1
madmasles сказал(а):
urrya,
Код:
$sText = 'Привет! Это бессмысленный текст для _EnCryptDeCrypt()'
$sPass = 'AutoIt Version: 3.3.8.1'
;~ MsgBox(64, 'Info', 'Исходный текст: ' & $sText & @LF & 'Ключ: ' & $sPass)
$sEnCryptText = _EnCryptDeCrypt($sText, $sPass);
MsgBox(64, 'Info', 'Зашифрованный текст: ' & $sEnCryptText);

$hFile = FileOpen(@ScriptDir & '\EnCrypt.txt', 34)
FileWrite($hFile, $sEnCryptText)
FileClose($hFile)
$sEnCryptText = ''
$hFile = FileOpen(@ScriptDir & '\EnCrypt.txt', 32)
$sEnCryptText = FileRead($hFile)
FileClose($hFile)

$sDeCryptText = _EnCryptDeCrypt($sEnCryptText, $sPass)
MsgBox(64, 'Info', 'Расшифрованный текст: ' & $sDeCryptText);

Func _EnCryptDeCrypt($s_Data, $s_Key)
    If (Not $s_Data) Or (Not $s_Key) Then Return SetError(-1, 0, '')
    Local $a_Data, $a_Key, $s_Ret, $i_ChrW, $i_UbData, $i_UbKey

    $a_Data = StringToASCIIArray($s_Data)
    $i_UbData = UBound($a_Data)
    If Not $i_UbData Then Return SetError(1, 0, '')
    $a_Key = StringToASCIIArray($s_Key)
    $i_UbKey = UBound($a_Key)
    If Not $i_UbKey Then Return SetError(2, 0, '')
    For $i = 0 To $i_UbData - 1
        For $j = 0 To $i_UbKey - 1
            $i_ChrW = BitXOR($a_Data[$i], $a_Key[$j], ($i_UbKey - $j) * ($i_UbData - $i))
        Next
        $s_Ret &= ChrW($i_ChrW)
    Next
    Return $s_Ret
EndFunc   ;==>_EnCryptDeCrypt

:beer:
 
Верх