Am attempting to encrypt a file using this program in QB64.
It does not actually encrypt the file and always returns successful. Why?
DECLARE LIBRARY
FUNCTION EncryptFile (f$)
FUNCTION DecryptFile (f$, BYVAL f&)
END DECLARE
PRINT "Enter filename";
INPUT f$
IF f$ <> "" THEN
f$ = f$ + CHR$(0)
x = EncryptFile(f$)
IF x = 0 THEN
PRINT "Error encrypting file."
ELSE
PRINT "File encrypted."
END IF
END IF
END
The solution was to detect the encryption status of a filename such as this:
REM checks encryption status of a filename
DECLARE DYNAMIC LIBRARY "advapi32"
FUNCTION FileEncryptionStatusA% (f$, f&)
END DECLARE
DO
PRINT "Filename";
INPUT f$
IF f$ = "" THEN END
x = FileEncryptionStatusA(f$, f&)
IF x = 0 THEN
PRINT "Error accessing file."
END IF
IF x THEN
SELECT CASE f&
CASE 0
PRINT "File can be encrypted."
CASE 1
PRINT "File is encrypted."
CASE 2
PRINT "File is system."
CASE 3
PRINT "File is root."
CASE 4
PRINT "File is system directory."
CASE 5
PRINT "Encryption status unknown."
CASE 6
PRINT "File system does not support encryption."
CASE 7 ' reserved
CASE 8
PRINT "File is read-only."
END SELECT
END IF
LOOP
END
Related
In my program I am outputting a .csv file which exceeds 1000000 lines. Currently after the file is exported, I am splitting the file from linux using the below commands. However, I would like to know if we can split the files using a progress code. If so, could someone plese let me know on how to do it.
Below is the linux command I use manually.
ls -l xa*
split -1000000 filename.csv
mv xaa filename-01.csv
mv xab filename-02.csv
Without any code to work with I invented some code outputting to different files. You will have to work with OUTPUT TO and set new filenames.
This example will output 1050 lines split in files of 100 lines each.
DEFINE VARIABLE iLinesToOutput AS INTEGER NO-UNDO INIT 1050.
DEFINE VARIABLE iSplitAt AS INTEGER NO-UNDO INIT 100.
DEFINE VARIABLE iLine AS INTEGER NO-UNDO.
DEFINE VARIABLE cFile AS CHARACTER NO-UNDO.
DEFINE VARIABLE iFile AS INTEGER NO-UNDO.
DEFINE VARIABLE iOpen AS INTEGER NO-UNDO.
DEFINE STREAM str.
DO iLine = 1 TO iLinesToOutput:
// Open a new stream/file
IF (iLine - 1 ) MOD iSplitAt = 0 THEN DO:
iFile = iFile + 1.
cFile = "c:\temp\file-" + STRING(iFile, "999") + ".txt".
OUTPUT STREAM str TO VALUE(cFile).
EXPORT STREAM str DELIMITER "," "Customer ID" "Order Number" "Contact" "Count"
END.
// Output some data
PUT STREAM str UNFORMATTED "Line " iLine SKIP.
// Close the stream/file
IF iLine MOD iSplitAt = 0 THEN DO:
OUTPUT STREAM str CLOSE.
END.
END.
/* Close last file if not exactly right number of lines */
/* This could also be checked but close twice doesn't really matter */
OUTPUT STREAM str CLOSE.
Im working on a script that work with a Binary file Inside of loop and read its bytes Consecutive , (also be able to Go back and forth inside the file (savepos) (setpos))
But I dont know I need to use what method so its not bad for performance
My first attempt was to use FileRead with FileHandle, But sadly This made the speed of the program slower as the program progressed and read further from file (Program finish after like 30 min for a 10MB file)
$File = FileOpen($Path, 16)
$tTimer = TimerInit()
$ndx = 4
for $i=0 to 100000
$test = FileRead($File, $ndx)
$ndx += 4
ConsoleWrite($test & #CRLF)
Next
ConsoleWrite(TimerDiff($ttimer) &" Sekunden"& #CRLF)
So I tried to first read whole File into a variable and then read the binary from it with BinaryMid
But this method was even more slower...
$File = FileOpen($Path, 16)
$Readfilee = FileRead($File)
$tTimer = TimerInit()
$ndx = 4
for $i=0 to 100000
$test = _BinaryRead(4)
ConsoleWrite($test & #CRLF)
Next
ConsoleWrite(round(TimerDiff($ttimer) /1000,2) &" Sekunden"& #CRLF)
Func _BinaryRead($iCount)
$ndx += $iCount
Return BinaryMid($Readfilee, $GNOW - $iCount, $iCount)
EndFunc
so I want to know what can I do for reading Bin file as fast as possible?
sorry if its not a good question, Im new in autoit
You don't need to read the file byte-by-byte, the function FileRead() do everything for you in only one step, you can use something like this:
$hFile = FileOpen($Path, $FO_BINARY)
$tTimer = TimerInit()
$bFileContent = FileRead($hFile)
FileClose($hFile)
; Now you can use $bFileContent as you want.
ConsoleWrite(TimerDiff($ttimer) &" Sekunden"& #CRLF)
I just want to run a loop at autoit where if i take any kind of number then the code wont execute The code is below,
If $Number($read, "")Then
;We have it, display the message.
MsgBox($MB_SYSTEMMODAL, "", "The following values were converted to a numeric value:" & #CRLF & _
$Number)
Else
;Get Existing Data of edit
$read2 = GUICtrlRead($hEdit)
$text = $read2 & #CRLF & $read ;
If I understand correctly, you want to check if a value is a number, and execute the code if it's NOT. If so, use IsNumber(). For example:
$testVar = 1
If Not (IsNumber($testVar)) Then
MsgBox(0, "Title", "This code will not execute as the variable's a number.")
Else
MsgBox(0, "Title", "This code WILL execute since the variable is a number.")
EndIf
$testVar2 = "String"
If Not (IsNumber($testVar2)) Then
MsgBox(0, "Title", "This code WILL execute since the variable is NOT a number.")
Else
MsgBox(0, "Title", "This code will not execute as the variable's NOT a number.")
EndIf
If $testVar IS a number, but that number is in quotes, it will be recognized as a string instead and execute (as it's not a number).
Following is my RSpec code snippet:
describe UsersController do
def mock_authentication_token(user, token_string)
...
...
end
def create_data
#date_format = '%Y-%m-%d %H:%M:%S'
today = Time.now
#today_str = today.strftime(#date_format)
...
..
..
end
before do
#current_user = Factory(:client)
authtoken_str = "client auth token string"
mock_authentication_token(#current_user, authtoken_str)
end
context "action: index" do
before do
create_data
#params = #params.merge(limit: 5)
end
it "should return the more link with date set to 1 second ahead of #{#today_str}" do
get :index, #params
body = JSON.parse response.body
...
...
...
end
end
This example it "should return the more link with date set to 1 second ahead of #{#today_str}" do when fails it doesn't print the value of instance variable #today_str set by helper method create_data, in the failed example description.
It just prints: should return the more link with date set to 1 second ahead of
It seems that it method doesn't allow string interpolation.Is this really the case? If yes how do I achieve the desired behavior.
Thanks,
Jignesh
Rspec resets the class instance # variables after each it block.
For instance:
describe 'it blocks' do
before :all
#reset = 0
##global = 'will break tests'
end
it 'should increment' do
#reset += 1
end
it "shouldn't forget it, either" do
# but it does
#reset.should eql 0
end
it 'does remember class-level variables, though' do
##global += ' for sure'
end
it 'can be demonstrated via' do
##global.split(' ').should > 3
end
# this is not the same #reset as what's in `before :all`.
this_is_blank = #reset
it "won't interpolate #{this_is_blank} because it's an instance level variable" do
true.should be true
end
local = 'might as well hard code them into your descriptions at this point'
it "Doesn't matter anymore because you #{local}" do
true.should eql true
end
it "won't get here because class-level variables #{##global}" do
(2 + 2).should eql 5
end
end
Looks like you'll have to name your spec tests more generically. I have, anyway.
i'm trying to do a sample app, for testing purposes vs other people development, and would like to print to the screen the encrypted string, and put it back to a decrypt mechanism....I just don't seem to be finding the way to do this...I've tried base64 and unpack, and feel this is the way, but am not getting there.
require 'openssl'
require 'base64'
def ask(prompt)
loop do
print prompt, ' '
$stdout.flush
s = gets
return s.chomp
end
end
def aes(m,k,t)
(aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(m)).key = Digest::SHA256.digest(k)
aes.update(t) << aes.final
end
def encrypt(key, text)
aes(:encrypt, key, text)
end
def decrypt(key, text)
aes(:decrypt, key, text)
end
def my_decrypt
#crypted = ask("Crypted data: ")
decrypted = decrypt("12345678911131511192123252729412",#crypted)
print decrypted
end
def my_encrypt
#decrypted = ask("Data to encrypt: ")
crypted = encrypt("12345678911131511192123252729412",#decrypted)
print crypted
end
option=ask("Option 1 - Encrypt, 2 decrypt")
case option
when "1" then my_encrypt
when "2" then my_decrypt
else print "Option not valid"
end
Anyone to the rescue?
Thank you
After some fight, i finally got it...I just need to convert the binary to Hex, and then back to binary...
Two notes : to convert a binary to hex, you can use String.unpack, which will return an array.
To convert an hex to binary, you first need to build it as an array ["anystringhere"] , and then pack it back to binary using Array.pack
Here is the resulting code
require 'openssl'
require 'base64'
def ask(prompt)
loop do
print prompt, ' '
$stdout.flush
s = gets
return s.chomp
end
end
def aes(m,k,t)
(aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(m)).key = Digest::SHA256.digest(k)
aes.update(t) << aes.final
end
def encrypt(key, text)
aes(:encrypt, key, text)
end
def decrypt(key, text)
aes(:decrypt, key, text)
end
def my_decrypt
#crypted = ask("Crypted data: ")
decrypted = decrypt("12345678911131517192123252729313",[#crypted].pack('H*'))
print decrypted
end
def my_encrypt
#decrypted = ask("Data to encrypt: ")
crypted = encrypt("12345678911131517192123252729313",#decrypted)
print u=crypted.unpack('H*')
end
option=ask("Option 1 - Encrypt, 2 decrypt")
case option
when "1" then my_encrypt
when "2" then my_decrypt
else print "Option not valid"
end