Wednesday, September 9, 2020

Write a Lex program to print out all HTML tags in file.

Print out all HTML tags in file in Lex

EXAMPLE :-

Create first file  tag.l or tag.lex , and Create Second file tags.txt

tag.l


%{ %} %% "<"[^>]*> {printf("%s\n", yytext); } . ; %% int yywrap(){} int main(int argc, char*argv[]) { yyin=fopen("tags.txt","r"); yylex(); return 0; }


INPUT :-

tags.txt


<html> <title> Html Tags </title> <head></head> <body> <h1> A smile is happiness you’ll find right under your nose. </h1> </body> </html>

OUTPUT


sunny@sunny-PC:~/Desktop$ flex h.l sunny@sunny-PC:~/Desktop$ gcc lex.yy.c sunny@sunny-PC:~/Desktop$ ./a.out <html> <title> </title> <head> </head> <body> <h1> </h1> </body> </html>









Write a Lex program to print out all numbers from the given file.

 Print out all numbers from the given file in Lex Program.


Create first file  Num.l or Num.lex , and Create Second file num.txt

Num.l


%{ #include <stdio.h> %} %% [0-9]+ { printf("%s\n", yytext); } .|\n ; %% int yywrap(void){} int main(int argc, char*argv[]) { yyin=fopen("num.txt","r"); yylex(); return 0; }


INPUT :-

num.txt


1 2 3 4 5 6 7 8 9 0


OUTPUT



sunny@sunny-PC:~/Desktop$ flex num.l sunny@sunny-PC:~/Desktop$ gcc lex.yy.c sunny@sunny-PC:~/Desktop$ ./a.out 1 2 3 4 5 6 7 8 9 0



Sunday, September 6, 2020

Activity Lifecycle in Android with Source code.

  • Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. 
  • The android Activity is the subclass of Context Theme Wrapper class.

Method

 

Description

onCreate

called when activity is first created.

 

onStart

 

called when activity is becoming visible to the user.

onResume

called when activity will start interacting with the user.

onPause

called when activity is not visible to the user.

onStop

called when activity is no longer visible to the user.

onRestart

called after your activity is stopped, prior to start.

onDestroy

called before the activity is destroyed.



EXAMPLE :-

activity_mail.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Life Cycle Program!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


MainActivity.java

package com.example.sunny.lifecycle;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toast.makeText(getApplicationContext(),"onCreate Method Call",
Toast.LENGTH_LONG).show();

}

protected void onStart()
{
super.onStart();

Toast.makeText(getApplicationContext(),"onStart Method Call",
Toast.LENGTH_LONG).show();

}

@Override
protected void onRestart()
{
super.onRestart();
Toast.makeText(getApplicationContext(),"onRestart Method Call",
Toast.LENGTH_LONG).show();

}

protected void onPause()
{
super.onPause();
Toast.makeText(getApplicationContext(),"onPause Method Call",
Toast.LENGTH_LONG).show();

}

protected void onResume()
{
super.onResume();
Toast.makeText(getApplicationContext(),"onResume Method Call",
Toast.LENGTH_LONG).show();

}

protected void onStop()
{
super.onStop();

Toast.makeText(getApplicationContext(),"onStop Method Call",
Toast.LENGTH_LONG).show();

}

protected void onDestroy()
{
super.onDestroy();
Toast.makeText(getApplicationContext(),"onDestroy Method Call",
Toast.LENGTH_LONG).show();

}
}



OUTPUT




Wednesday, September 2, 2020

Write a Lex Program to count number of words.

EXAMPLE :-

%{ #include<stdio.h> #include<string.h> int s = 0; %} %% ([a-zA-Z0-9])* {s++;} "\n" {printf("%d\n", s); s = 0;} %% int yywrap(void){} int main() { yylex(); return 0; }


OUTPUT


sunny@sunny-PC:~/Desktop$ flex nw.l sunny@sunny-PC:~/Desktop$ gcc lex.yy.c sunny@sunny-PC:~/Desktop$ ./a.out Computer Science 2




Write a Lex program to count number of vowels and consonants in a given input string.

Step 1 :- lex filename.l or lex filename.lex depending on the extension file is saved with
Step 2 :-  gcc lex.yy.c
Step 3 :- ./a.out

cv.l


%{ int vowel=0; int const =0; %} %% [aeiouAEIOU] {vowel++;} [a-zA-Z] {const++;} %% int yywrap(){} int main() { printf("Enter the string of Vowels and Consonents ::"); yylex(); printf("Number of vowels are : %d\n", vowel); printf("Number of consonants are : %d\n", const); return 0; }



OUTPUT



sunny@sunny-PC:~/Desktop$ flex cv.l sunny@sunny-PC:~/Desktop$ gcc lex.yy.c sunny@sunny-PC:~/Desktop$ ./a.out Enter the string of Vowels and Consonents :: Peace begins with a smile
press ctrl+d
Number of vowels are: 9 Number of consonants are: 12








Create a Lexer to take input from text file and count no of characters, no. of lines & no. of words.

Count no of characters no. of lines & no. of words in Lex Program.

Step 1 :- lex filename.l or lex filename.lex depending on the extension file is saved with
Step 2 :-  gcc lex.yy.c
Step 3 :- ./a.out

file.l 


%{ #include<stdio.h> int lines=0,words=0,char=0; %} %% \n {lines++ ; words;} [\t ' '] words++; [a-zA-Z] char++; %% int main() { yyin=fopen("s.txt" , "r"); yylex(); printf("\n File Contents..\n"); printf("\n\t%d Line",lines); printf("\n\t%d Words",words); printf("\n\t%d Characters",char); return 0; } int yywrap() { return 1; }


INPUT :

s.txt


All our dreams can come true if we have the
courage to pursue them The secret of getting ahead is getting started
Mark Twain


OUTPUT



sunny@sunny-PC:~/Desktop$ flex file.l sunny@sunny-PC:~/Desktop$ gcc lex.yy.c sunny@sunny-PC:~/Desktop$ ./a.out
File Contents.. 1 Line 26 Words 111 Characters










Tuesday, September 1, 2020

Implement RSA Algorithm Encryption and Decryption in C++.

  

  • RSA (Rivest–Shamir–Adleman) is an algorithm used by modern computers to encrypt and decrypt messages. 
  • It is an asymmetric cryptographic algorithm
  • Asymmetric means that there are two different keys. 
  • This is also called public key cryptography, because one of the keys can be given to anyone.

EXAMPLE :-


#include<iostream>
#include<math.h>
using namespace std;
int gcd(int a, int b) {
int s;
while(1) {
s= a%b;
if(s==0)
return b;
a = b;
b= s;
}
}
int main() {



double x = 3;
double y = 5;
double n=x*y;
double track;
double phi= (x-1)*(y-1);
double e=11;

while(e<phi) {
track = gcd(e,phi);
if(track==1)
break;
else
e++;
}
double d1=1/e;
double d=fmod(d1,phi);
double message = 13;
double c = pow(message,e);
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
cout<<"Original Message = "<<message;
cout<<"\n"<<"x = "<<x;
cout<<"\n"<<"y = "<<y;
cout<<"\n"<<"n = xy = "<<n;
cout<<"\n"<<"phi = "<<phi;
cout<<"\n"<<"e = "<<e;
cout<<"\n"<<"d = "<<d;
cout<<"\n"<<"Encrypted message = "<<c;
cout<<"\n"<<"Decrypted message = "<<m;
return 0;
}


OUTPUT


Original Message = 13 x = 3 y = 5 n = xy = 15 phi = 8 e = 11 d = 0.0909091 Encrypted message = 7 Decrypted message = 13

 

Implement RSA Algorithm Encryption and Decryption in C

  • RSA (Rivest–Shamir–Adleman) is an algorithm used by modern computers to encrypt and decrypt messages. 
  • It is an asymmetric cryptographic algorithm
  • Asymmetric means that there are two different keys. 
  • This is also called public key cryptography, because one of the keys can be given to anyone.

EXAMPLE :-


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

typedef struct
{
int d;
int x;
int y;
}EE;

EE extended_euclid(int a, int b) {
EE e1, e2, e3;
if (b == 0)
{
e1.d = a;
e1.x = 1;
e1.y = 0;
return e1;
}
else
{
e2 = extended_euclid(b, a % b);
e3.d = e2.d;
e3.x = e2.y;
e3.y = e2.x - floor(a / b) * e2.y;
return e3;
}
}

int modulo(int x, int N){
return (x % N + N) % N;
}

void decimal_to_binary(int op1, int pq[]){
int result, i = 0;
do
{
result = op1 % 2;
op1 /= 2;
pq[i] = result;
i++;
}
while(op1 > 0);
}

int modular_exponentiation(int a, int b, int n){
int *bb;
int count = 0, c = 0, d = 1, i;

count = (int) (log(b)/log(2)) + 1;

bb = (int *) malloc(sizeof(int*) * count);
decimal_to_binary(b, bb);

for (i = count - 1; i >= 0; i--)
{
c = 2 * c;
d = (d*d) % n;
if (bb[i] == 1)
{
c = c + 1;
d = (d*a) % n;
}
}
return d;
}

int get_d(int e, int phi1){
EE Ee;
Ee = extended_euclid(e, phi1);
return modulo(Ee.x, phi1);
}

int main(int argc, char* argv[])
{
int p1, q1, phi1, n1, e1, d1, m1, c1;
printf("Enter the value of p1: ");
scanf("%d", &p1);

printf("Enter the valeu of q1: ");
scanf("%d", &q1);

n1 = p1*q1;
phi1 = (p1 - 1) * (q1 - 1);

printf("Enter the value of e: ");
scanf("%d", &e1);
d1 = get_d(e1, phi1);

printf("Public Key: (n = %d, e = %d)\n", n1, e1);
printf("Private Key: (n = %d, d = %d)\n", n1, d1);
printf("Enter message to encrypt: ");
scanf("%d", &m1);

c1 = modular_exponentiation(m1, e1, n1);
printf("Encrypted message is: %d\n", c1);
m1 = modular_exponentiation(c1, d1, n1);
printf("Message is decrypted to %d\n", m1);
return 0;
}


OUTPUT





Implement RSA Algorithm Encryption and Decryption in Python.

  • RSA (Rivest–Shamir–Adleman) is an algorithm used by modern computers to encrypt and decrypt messages. 
  • It is an asymmetric cryptographic algorithm
  • Asymmetric means that there are two different keys. 
  • This is also called public key cryptography, because one of the keys can be given to anyone.

EXAMPLE :-


import math

print("RSA ENCRYPTION/DECRYPTION")
print("*****************************************************")

# Input Prime Numbers
print("PLEASE ENTER THE 'x' AND 'y' VALUES BELOW:")
x = int(input("Enter a prime number for x: "))
y = int(input("Enter a prime number for y: "))
print("*****************************************************")

def prime_check(k):
if (k == 2):
return True
elif ((k < 2) or ((k % 2) == 0)):
return False
elif (k > 2):
for i in range(2, k):
if not (k % i):
return false
return True


check_x = prime_check(x)
check_y = prime_check(y)
while (((check_x == False) or (check_y == False))):
x = int(input("Enter a prime number for x: "))
y = int(input("Enter a prime number for y: "))
check_x = prime_check(x)
check_y = prime_check(y)

n = x * y
print("RSA Modulus(n) is:", n)

r = (x - 1) * (y - 1)
print("Eulers Toitent(r) is:", r)
print("*****************************************************")

def egcd(e, r):
while (r != 0):
e, r = r, e % r
return e

def eugcd(e, r):
for i in range(1, r):
while (e != 0):
a, b = r // e, r % e
if (b != 0):
print("%d = %d*(%d) + %d" % (r, a, e, b))
r = e
e = b

def eea(a, b):
if (a % b == 0):
return (b, 0, 1)
else:
gcd, s, t = eea(b, a % b)
s = s - ((a // b) * t)
print("%d = %d*(%d) + (%d)*(%d)" % (gcd, a, t, s, b))
return (gcd, t, s)

def mult_inv(e, r):
gcd, s, _ = eea(e, r)
if (gcd != 1):
return None
else:
if (s < 0):
print("s=%d. Since %d is less than 0, "
"s = s(modr), i.e., s=%d." % (s, s, s % r))
elif (s > 0):
print("s=%d." % (s))
return s % r

for i in range(1, 1000):
if (egcd(i, r) == 1):
e = i
print("The value of e is:", e)
print("*****************************************************")

print("RSA ALGORITHM:")
eugcd(e, r)
print("END OF THE STEPS USED TO ACHIEVE EUCLID'S ALGORITHM.")
print("*****************************************************")
print("RSA EXTENDED ALGORITHM:")
d = mult_inv(e, r)
print("END OF THE STEPS USED TO ACHIEVE THE VALUE OF 'd'.")
print("The value of d is:", d)
print("*****************************************************")
public = (e, n)
private = (d, n)
print("Private Key is:", private)
print("Public Key is:", public)
print("*****************************************************")

def encrypt(pub_key, n_text):
e, n = pub_key
x = []
m = 0
for i in n_text:
if (i.isupper()):
m = ord(i) - 65
c = (m ** e) % n
x.append(c)
elif (i.islower()):
m = ord(i) - 97
c = (m ** e) % n
x.append(c)
elif (i.isspace()):
spc = 500
x.append(500)
return x


def decrypt(priv_key, c_text):
d, n = priv_key
txt = c_text.split(',')
x = ''
m = 0
for i in txt:
if (i == '500'):
x += ' '
else:
m = (int(i) ** d) % n
m += 65
c = chr(m)
x += c
return x

message = input("You like Encrypted or Decrypted?
                (Separate numbers with ',' for Decryption):")
print("Your Message is ::", message)

choose = input("Type '1' for Encryption and '2' for Decryption.")

if (choose == '1'):
enc_msg = encrypt(public, message)
print("Your Encrypted Message is:", enc_msg)
print("Thank you for using the RSA Encryption Algorithm. Goodbye!")

elif (choose == '2'):
print("Your Decrypted Message is:", decrypt(private, message))
print("Thank you for using the RSA Encryption Algorithm. Goodbye!")

else:
print("You entered the wrong option.")
print("Thank you for using the RSA Encryption Algorithm. Goodbye!")

OUTPUT

RSA ENCRYPTION/DECRYPTION
*****************************************************
PLEASE ENTER THE 'x' AND 'y' VALUES BELOW:
Enter a prime number for x: 3
Enter a prime number for y: 5
*****************************************************
RSA Modulus(n) is: 15
Eulers Toitent(r) is: 8
*****************************************************
The value of e is: 999
*****************************************************
RSA ALGORITHM:
8 = 0*(999) + 8
999 = 124*(8) + 7
8 = 1*(7) + 1
END OF THE STEPS USED TO ACHIEVE EUCLID'S ALGORITHM.
*****************************************************
RSA EXTENDED ALGORITHM:
1 = 8*(1) + (-1)*(7)
1 = 999*(-1) + (125)*(8)
s=-1. Since -1 is less than 0, s = s(modr), i.e., s=7.
END OF THE STEPS USED TO ACHIEVE THE VALUE OF 'd'.
The value of d is: 7
*****************************************************
Private Key is: (7, 15)
Public Key is: (999, 15)
*****************************************************
You like Encrypted or Decrypted? (Separate numbers with ',' for Decryption):3,15,20,17,13
Your Message is :: 3,15,20,17,13
Type '1' for Encryption and '2' for Decryption.2
Your Decrypted Message is: MAFIH
Thank you for using the RSA Encryption Algorithm. Goodbye!