Need XORd briefly explained for Cryptopals Crypto Challenge 3 set 1 - python-3.x

i just have a quick question on this bit of code i have here on the Cryptopals Challenges, using Python3, For XORing a string with one single character.The program takes in a hex string string, decodes it, XORs it with a single character, does this for every possible character, then finds the "most english" line of XORd data. heres my code snippet (which i admittdly used from a solutions page ) :
def singlechar_xor(input_bytes, key_value):
"""XORs every byte of the input with the given key_value and returns the result."""
output = b''
for char in input_bytes:
output += bytes([char ^ key_value])
return output
i know what is happening and i understand what is supposed to happen, im just not sure how bytes behave and what types are and arent supposed to be XORd. Why do i need the brackets around char^key_value? If i remove the brackets my output becomes a bunch of 0's. What is the result of the XOR or the character and the key_value? If someone could kindly explain so i could have a better understanding going forward in these challenges id GREATLY appreciate it <3

Related

Recursively extract contents of (nested) parentheses in string, replace selected content(s) down to a single (char+int), read again and repeat

This is my first post so please comment down if you need further clarification, Say we take in a string such as:
((((a).(5)).((a)*)).((b)*))*
and through the process, we perhaps count++ the amount of '(' read and count-- the amount of ')' read until we come across our first char or variable (we can consider other operators such as '.' or '|' or '*') that is the left and innermost content such that a is replaced so that our string now reads:
(((R1.(5)).((a)*)).((b)*))*
We must consider when a is selected, we also include its parenthesis as well but only consider the string that is read (perhaps store it in or as a vector, pointer, object, etc.), the same applies for when 5 which results in:
(((R1.R2).((a)*)).((b)*))*
at this moment we perhaps find now that the innermost content found is string (R1.R2) and
this results the string to be converted into R3 and having the string as:
((R3.((a)*)).((b)*))*
We continue the iteration for a to be read too.
((R3.(R4*)).((b)*))*
If a star * is read, we can consider extracting that content with R4 to be replaced as R5
((R3.R5).((b)*))*
Our next iteration follows:
((R3.R5).(R6*))*
((R3.R5).R7)*
(R8.R7)*
R9*
and finally as our final result,
R10
And we end.
I've already tried a variety of algorithms from several sources for hours yet I'm still stuck and puzzled at the same spot and I may not be thinking this through very properly as I had hoped.
The only closest that was modified was this:
https://www.geeksforgeeks.org/extract-substrings-between-any-pair-of-delimiters/
but I'm still puzzled about what must be properly implemented.
How would you make this possible? Any solutions or a straight post of your code could surely help me understand this process.

Converting character to real fails if character consists of an integer (i.e. without dot)

I am reading a text file with my fortran code. I parse the text file (which contain a bunch of stuff such as names and numbers) and I end up with strings containing real number (they are real time measuraments) such as:
string = 1.34
I simply write this string in a real number by doing
read(levelCHAR,'(f)') level
And everything worked great for a month until today, when the number in the input file was exactly 1 and I had:
string = 1
and the read statement above gave me
level=0
Therefore to fix this I added before the read statement:
if (index(string ,'.')<=0) then
string = trim(string )//'.'
endif
And this seems to have fixed the issue. However, I wanted to know if I am missing something and there is a more elegant way to do this in one line for example by replacing the format '(f)' in the read statement with a more suitable expression.
Your program is not valid Fortran:
read(levelCHAR,'(f)') level
1
Error: Nonnegative width required in format string at (1)
form.f90:5.5:
You must indicate the input field with such as f5.0. Or you can use the list-directed input read(levelChar,*) level.
Also, be sure to use the .0 and not any other number in the fw.d descriptor for input. Otherwise strange results are to be expected for integer inputs as they will be multiplied by 10**(-d).

Decoding Specification '(AA$)' For Write Statement

I'm confused as the what this write specification is trying to specify. N is an array of single characters. Could someone help me and explain the write format specification below. I saw someone post the exact same question a few days ago but the page is not there anymore.
WRITE(*,'(AA$)') N(I),","
The dollar sign in a format specifier suppresses a new line.
Therefore, the array N is written element-wise as a string (A) separated by a comma (second string A) one a single line.
Note that this syntax is not standard conforming, in modern Fortran you would write the format as
WRITE(*,'(2A)', advance='no') N(I),","

How do I TRIM a character array in standard F77?

I'm reading from ASCII data files with text headers. (The headers contain info about the data run.) I want to add some of the columns of each data file, then write the result to another data file, but keep the headers for each of the files. The problem is, I don't know beforehand what the lengths of the header lines are. If I use a long character variable (character*400, for example) to make sure I get the entire header lines, then my new data files have lots of white space I don't want. Basically, I want to do TRIM(HeaderVariable), but TRIM is not available to me. Any suggestions? Is there a way to WRITE only to a CrLF? I thought of using an array of character*1, and testing each character as I read it and write it, but...wow, that's sooooo complicated. Is there a simpler way to do this in standard F77?
[edit: self-answer moved to answer. could not do it at first because rep was too low.]
I got the answer. Posting here to help others. The LENGTH function below is taken from http://www.star.le.ac.uk/~cgp/prof77.html#tth_sEc7 Once you've got this LENGTH function, it's trivial to implement your own TRIM function. Functionally, this isn't much different from my initial horrid idea, but it's prettier.
LEN The LEN function takes a character argument and returns its length as an integer. The argument may be a local character variable or array element but this will just return a constant. LEN is more useful in procedures where character dummy arguments (and character function names) may have their length passed over from the calling unit, so that the length may be different on each procedure call. The length returned by LEN is that declared for the item. Sometimes it is more useful to find the length excluding trailing blanks. The next function does just that, using LEN in the process.
INTEGER FUNCTION LENGTH(STRING) !Returns length of string ignoring trailing blanks
CHARACTER*(*) STRING
DO 15, I = LEN(STRING), 1, -1
IF(STRING(I:I) .NE. ' ') GO TO 20
15 CONTINUE
20 LENGTH = I
END

MIPS: Check how many alphabetic characters are in a string

Right now I'm trying to load a null-terminated string and return the number of alphabetic characters that are in that string. Currently I have three functions: the Main, countAlpha (which is intended to count the number of characters), and isAlpha, which determines whether the character is alphabetic or not. I would like some help with my algorithm.
So, for my Main I load the string, jump and link to countAlpha, and then load the syscall commands to print int and exit the program.
For my countAlpha, which I am having trouble with, I want to create a for loop that goes through each character in the string and if isAlpha returns a 1 (indicating that the character is alphabetic), then increase the count.
isAlpha is straightforward-just determine if the character is between specific numbers in ASCII.
So I guess my question is how do I attack countAlpha. Thanks for your help. (Note: I don't want actual code, just tips and hints)
Sounds like you want to test a return value / register for the call to isAlpha and add/increment a seperate register/count var everytime the test is true. Or you could just add the return value directly to your count register every time through the loop if the value is returned as a 0 or 1. Then, just setup your loop to keep going until the current character is a '\0' char. Then your count register holds your final value.
Is this in-depth enough to help you out?

Resources