summaryrefslogtreecommitdiff
path: root/3544/CH4
diff options
context:
space:
mode:
authorprashantsinalkar2017-10-10 12:27:19 +0530
committerprashantsinalkar2017-10-10 12:27:19 +0530
commit7f60ea012dd2524dae921a2a35adbf7ef21f2bb6 (patch)
treedbb9e3ddb5fc829e7c5c7e6be99b2c4ba356132c /3544/CH4
parentb1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b (diff)
downloadScilab-TBC-Uploads-7f60ea012dd2524dae921a2a35adbf7ef21f2bb6.tar.gz
Scilab-TBC-Uploads-7f60ea012dd2524dae921a2a35adbf7ef21f2bb6.tar.bz2
Scilab-TBC-Uploads-7f60ea012dd2524dae921a2a35adbf7ef21f2bb6.zip
initial commit / add all books
Diffstat (limited to '3544/CH4')
-rw-r--r--3544/CH4/EX4.17/Ex4_17.sce25
-rw-r--r--3544/CH4/EX4.18/Ex4_18.sce17
-rw-r--r--3544/CH4/EX4.4/Ex4_4.sce45
-rw-r--r--3544/CH4/EX4.5.1/Ex4_5_1.sce11
-rw-r--r--3544/CH4/EX4.5.2/Ex4_5_2.sce13
-rw-r--r--3544/CH4/EX4.5.3/Ex4_5_3.sce30
-rw-r--r--3544/CH4/EX4.5/Ex4_5.sce36
-rw-r--r--3544/CH4/EX4.6/Ex4_6.sce45
-rw-r--r--3544/CH4/EX4.62/Ex4_62.sce23
-rw-r--r--3544/CH4/EX4.9.1/Ex4_9_1.sce32
-rw-r--r--3544/CH4/EX4.9.2/Ex4_9_2.sce21
11 files changed, 298 insertions, 0 deletions
diff --git a/3544/CH4/EX4.17/Ex4_17.sce b/3544/CH4/EX4.17/Ex4_17.sce
new file mode 100644
index 000000000..d11259888
--- /dev/null
+++ b/3544/CH4/EX4.17/Ex4_17.sce
@@ -0,0 +1,25 @@
+
+//Longitudinal redundancy check
+
+data = [ "11100100","11011101","00111001","00101001" ]
+disp("Original data")
+disp(data)
+data = bin2dec(data)
+
+lrc = 0.
+
+for i=1:length(data)
+ lrc = bitxor(lrc,data(i))
+end
+
+disp("LRC: ")
+
+for i=1:7
+ if lrc<(2^(8-i)) then
+ printf("0")
+ else
+ printf("%s",dec2bin(lrc))
+ break
+ end
+end
+
diff --git a/3544/CH4/EX4.18/Ex4_18.sce b/3544/CH4/EX4.18/Ex4_18.sce
new file mode 100644
index 000000000..378f06094
--- /dev/null
+++ b/3544/CH4/EX4.18/Ex4_18.sce
@@ -0,0 +1,17 @@
+// Simple message digest
+
+n = 7391743 //Message
+printf("Original number is %d\n",n)
+
+n_str = string(n) //Conversion of integer to string for easy access
+l = length(n_str)
+n_v = strsplit(n_str,1:l-1) //String to vector of characters
+
+d = 1
+for i=1:l
+ d = d * ( ascii(n_v(i:i)) - ascii('0')) //
+ d = modulo(d,10)
+ i = i+1
+end
+
+printf("Message digest is %d\n",d)
diff --git a/3544/CH4/EX4.4/Ex4_4.sce b/3544/CH4/EX4.4/Ex4_4.sce
new file mode 100644
index 000000000..d03258c23
--- /dev/null
+++ b/3544/CH4/EX4.4/Ex4_4.sce
@@ -0,0 +1,45 @@
+//RSA algorithm example
+
+p = 47
+q = 71
+
+n = p*q
+z = (p-1)*(q-1)
+
+e = 79 // E<N and E & Z are coprime
+
+i=1
+d = i
+
+//Brute-force approach to find 'd'
+while(1==1)
+ if modulo(i*e,z)==1 then // (E*D)mod Z = 1
+ d=i
+ break
+ end
+ i=i+1
+end
+
+printf("%d",d)
+
+//Public key: (n,e)
+//Privae key: (n,d)
+printf("\nPublic Key: (%d,%d)\nPrivate Key: (%d,%d)\n\n",n,e,n,d)
+
+
+P = 688 //Plaintext
+printf("Plaintext: %d\n",P)
+
+C = 1 //Encrypted Text
+for i = 1:e
+ C = modulo(C*P,n)
+end
+
+printf("Encrypted Text: %d\n",C)
+
+P=1 //Decrypting the encypted text 'C'
+for i = 1:d
+ P = modulo(P*C,n)
+end
+
+printf("Decrypted Text: %d\n",P)
diff --git a/3544/CH4/EX4.5.1/Ex4_5_1.sce b/3544/CH4/EX4.5.1/Ex4_5_1.sce
new file mode 100644
index 000000000..df0f7cd5b
--- /dev/null
+++ b/3544/CH4/EX4.5.1/Ex4_5_1.sce
@@ -0,0 +1,11 @@
+
+//ElGamal Key Generation
+
+p = 11
+e1 = 2
+d = 3
+
+e2 = modulo(e1^d,p)
+
+disp("Public key:")
+printf("(%d,%d,%d)",e1,e2,p)
diff --git a/3544/CH4/EX4.5.2/Ex4_5_2.sce b/3544/CH4/EX4.5.2/Ex4_5_2.sce
new file mode 100644
index 000000000..8642b7a41
--- /dev/null
+++ b/3544/CH4/EX4.5.2/Ex4_5_2.sce
@@ -0,0 +1,13 @@
+
+//ElGamal Key Encryption
+
+r = 4
+pt = 7
+e1 = 2
+e2 = modulo(e1^d,p)
+
+c1 = modulo(e1^r,p)
+c2 = modulo(pt*e2^r,p)
+
+disp("Cipher text")
+printf("(%d,%d)",c1,c2)
diff --git a/3544/CH4/EX4.5.3/Ex4_5_3.sce b/3544/CH4/EX4.5.3/Ex4_5_3.sce
new file mode 100644
index 000000000..35d0259ab
--- /dev/null
+++ b/3544/CH4/EX4.5.3/Ex4_5_3.sce
@@ -0,0 +1,30 @@
+
+//ElGamal Key Decryption
+
+// Move scilab to current file directory
+[u,t,n] = file()
+n = strcat(n)
+file_name = basename(n)+fileext(n)
+file_name = strcat(file_name)
+ind=strindex(n,file_name)
+path = part(n,1:ind-1)
+chdir(path)
+
+exec("Chapter_4.sci",-1)
+
+p = 11
+r = 4
+pt = 7
+d = 3
+e1 = 2
+e2 = modulo(e1^d,p)
+
+c1 = modulo(e1^r,p)
+c2 = modulo(pt*e2^r,p)
+
+x =c1^d
+x_inv = mod_inv(x,p)
+
+pt = modulo(c2*x_inv,p)
+disp("Original plaintext")
+disp(pt)
diff --git a/3544/CH4/EX4.5/Ex4_5.sce b/3544/CH4/EX4.5/Ex4_5.sce
new file mode 100644
index 000000000..c588dc704
--- /dev/null
+++ b/3544/CH4/EX4.5/Ex4_5.sce
@@ -0,0 +1,36 @@
+// RSA Encryption scheme
+
+p = 7 //Large prime numbers
+q = 17 //Small values taken here for convenience of calculation
+ //and explanation
+
+n = p*q
+z = (p-1)*(q-1)
+
+e = 5 // e<n and e & z are coprime
+
+i=1
+d = i
+while(1==1) //Calcualtion of 'd' from e and z
+ if modulo(i*e,z)==1 then // (e*d)mod z = 1
+ d=i
+ break
+ end
+ i=i+1
+end
+
+printf("\nPublic Key: (%d,%d)\nPrivate Key: (%d,%d)\n\n",n,e,n,d)
+
+PT = 10 //Example plaintext
+
+printf("Plaintext: %d\n",PT)
+
+C = modulo(PT^e,n)
+printf("\nEncrypted Text Code: %d\n\n",C)
+
+PT=1
+for i = 1:d
+ PT = modulo(PT*C,n)
+end
+
+printf("Decrypted Text: %d\n",PT) //Conversion to plain text in ASCII
diff --git a/3544/CH4/EX4.6/Ex4_6.sce b/3544/CH4/EX4.6/Ex4_6.sce
new file mode 100644
index 000000000..632ad2b7f
--- /dev/null
+++ b/3544/CH4/EX4.6/Ex4_6.sce
@@ -0,0 +1,45 @@
+// RSA Encryption scheme
+
+p = 7 //Large prime numbers
+q = 17 //Small values taken here for convenience of calculation
+ //and explanation
+
+n = p*q
+z = (p-1)*(q-1)
+
+e = 5 // e<n and e & z are coprime
+
+i=1
+d = i
+while(1==1) //Calcualtion of 'd' from e and z
+ if modulo(i*e,z)==1 then // (e*d)mod z = 1
+ d=i
+ break
+ end
+ i=i+1
+end
+
+printf("\nPublic Key: (%d,%d)\nPrivate Key: (%d,%d)\n\n",n,e,n,d)
+
+PT = 'F' //Example plaintext in ASCII
+P = ascii(PT)-ascii('A')+1 //Conversion of ASCII to integer code
+ //(A=1,B=2,C=3, ... )
+printf("Plaintext: %s\n",PT)
+printf("Plain text Integer code: %d\n\n\",P)
+
+C = 1
+for i = 1:e
+ C = modulo(C*P,n)
+end
+
+C = modulo(C,n)
+printf("Encrypted Text Code: %d\n\n",C)
+
+P=1
+for i = 1:d
+ P = modulo(P*C,n)
+end
+
+PT = ascii(ascii('A')+P-1)
+printf("Decrypted Text Code: %d\n",P) //Decryoted code
+printf("Decrypted Text: %s\n",PT) //Conversion to plain text in ASCII
diff --git a/3544/CH4/EX4.62/Ex4_62.sce b/3544/CH4/EX4.62/Ex4_62.sce
new file mode 100644
index 000000000..787468ced
--- /dev/null
+++ b/3544/CH4/EX4.62/Ex4_62.sce
@@ -0,0 +1,23 @@
+// Knapsack algorithm for Public Key Encryption
+
+PT = [0 1 1 0 1 1; 1 1 1 0 0 0; 0 1 0 1 1 0]
+
+disp("Plain text")
+disp(PT)
+
+K = [1 7 8 12 14 20]
+disp("Knapsack:")
+disp(K)
+
+[row,col] = size(PT)
+C = []
+for i=1:row
+ sum=0
+ for j=1:col
+ sum = sum+PT(i,j)*K(j:j)
+ end
+ C(i:i) = sum
+end
+
+disp("Cipher text:")
+disp(C)
diff --git a/3544/CH4/EX4.9.1/Ex4_9_1.sce b/3544/CH4/EX4.9.1/Ex4_9_1.sce
new file mode 100644
index 000000000..8c934755d
--- /dev/null
+++ b/3544/CH4/EX4.9.1/Ex4_9_1.sce
@@ -0,0 +1,32 @@
+
+//ElGamal Signature
+
+// Move scilab to current file directory
+[u,t,n] = file()
+n = strcat(n)
+file_name = basename(n)+fileext(n)
+file_name = strcat(file_name)
+ind=strindex(n,file_name)
+path = part(n,1:ind-1)
+chdir(path)
+
+exec("Chapter_4.sci",-1)
+
+e1 = 10
+e2 = 4
+p = 19
+m = 14 //original message
+d = 16
+r = 5 //random number selected by sender
+
+r_inv = mod_inv(r,p-1) //inverse of r modulo (p-1)
+
+s1 = modulo(e1^r,p)
+
+temp = (m-d*s1)*r_inv
+while temp<0 //calculate modulus (p-1) for negative values
+ temp = temp+p-1
+end
+s2 = modulo(temp,p-1)
+
+printf("The signature is: (%d,%d)",s1,s2)
diff --git a/3544/CH4/EX4.9.2/Ex4_9_2.sce b/3544/CH4/EX4.9.2/Ex4_9_2.sce
new file mode 100644
index 000000000..3f794948c
--- /dev/null
+++ b/3544/CH4/EX4.9.2/Ex4_9_2.sce
@@ -0,0 +1,21 @@
+
+//ElGamal Signature verification
+
+e1 = 10
+e2 = 4
+m = 14
+p = 19
+s1 = 3
+s2 = 4
+
+v1 = modulo(e1^m,p)
+disp("V1")
+disp(v1)
+
+v2 = modulo(e2^s1 * s1^s2,p)
+disp("V2")
+disp(v2)
+
+disp("Since V1=V2, signature is valid")
+
+