Files
appliedcrypto/unit01_cipher_fundamentals/lab/sample_ans.md
billbuchanan 44789e8d3a Update
2021-01-18 20:47:18 +00:00

4.8 KiB

A.6

print 0x43 | 0x21
print 0x43 & 0x21
print 0x43 ^ 0x21

A.7

val1=93
print "Dec:\t",val1
print "Bin:\t",bin(val1)
print "Hex:\t",hex(val1)
print "Oct:\t",oct(val1)
print "Char:\t",chr(val1)

A.8

val=93
console.log(val.toString(2))
console.log(val.toString(16))
console.log(val.toString(8))
console.log(String.fromCharCode(val))

A.9

import base64
str=crypto
print base64.b64encode(val)

A.10

import sys

val1="00110101"

if (len(sys.argv)>1):
        val1=sys.argv[1]

print "Binary form:  \t\t",val1
dec=int(val1,2)

print "Decimal form:  \t\t",dec,"\t",bin(dec)[2:10].rjust(8,'0')

res=(dec << 1) & 0xff
print "Shift left  (1):\t",res,"\t",bin(res)[2:10].rjust(8,'0')

res=(dec << 2) & 0xff

print "Shift left  (2):\t",res,"\t",bin(res)[2:].rjust(8,'0')

res=(dec >> 1) & 0xff
print "Shift right (1):\t",res,"\t",bin(res)[2:10].rjust(8,'0')

res=(dec >> 2) & 0xff
print "Shift right (2):\t",res,"\t",bin(res)[2:10].rjust(8,'0')

B.1

def gcd(a, b):
    
	while( b != 0 ):
		Remainder = a % b;
		a = b;
		b = Remainder;
	return a;

g = gcd(54,8)
print g

C.3

message = raw_input('Enter message: ')
e =  raw_input('Enter exponent: ') 
p = raw_input('Enter prime ')

cipher = (int(message) ** int(e)) % int(p)
print cipher

D.1

import math

def get_if_prime(val):

	max = math.sqrt(val);

	if (val % 2 == 0):
		return (False); 
            
	if (val % 3 == 0):
		return (False); 

	for k in range(0, 10000):
		testval = 6 * k + 1;
                if (testval>max):
			break
                if (val % testval == 0):
			return (False)
                testval = 6 * k - 1;
                if (testval>max):
			break
 
                if (val % testval == 0):
			return (False)
	return (true)

val=93

res = get_if_prime(val)
if (res==True):
	print str(val)+" is prime"
else:	
	print str(val)+" is not prime"

D.2

import sys

test=1000

if (len(sys.argv)>1):
	test=int(sys.argv[1])

def sieve_for_primes_to(n):
    size = n//2
    sieve = [1]*size
    limit = int(n**0.5)
    for i in range(1,limit):
        if sieve[i]:
            val = 2*i+1
            tmp = ((size-1) - i)//val 
            sieve[i+val::val] = [0]*tmp
    return [2] + [i*2+1 for i, v in enumerate(sieve) if v and i>0]
 
print sieve_for_primes_to(test)

This works because we start with all the odd numbers up to the square root of the limit of the numbers we are looking for. If we have 100, then the size will be 50. We start off with odd numbers (as 2 is the only even prime):

3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 .. 99

In the first time round we have i equal to 1, and we will jump 3 each time and mark them as not prime:

3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 .. 97 99

In the next time round, we will jump 5, starting at 5:

3 5 7 X 11 13 X 17 19 X 23 25 X 29 31 X 35 .. 97, X

In the next time round, we will jump 7, starting at 7:

3 5 7 X 11 13 X 17 19 X 23 X X 29 31 X X .. 97 99

In the next time round, we will jump 9, starting at 9:

3 5 7 X 11 13 X 17 19 X 23 X X 29 31 X X .. 97 99

In the end we stop at 19, and with a jump of 19, and add the value of 2 to the discovered prime numbers:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97

The marking of the factors follows this sequence:

[1, 3, 5, 7, 9, 11,13,15,17,19,21,23,25,27,29,31,33,35 .. ]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1,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