Professional Documents
Culture Documents
import java.util.*;
public class LeakyBucket {
public final static int max=25;
static int min(int a,int b)
{
if(a<b)
return a;
return b;
}
}
2 Bellmanford Algorithm
import java.util.*;
import java.math.*;
public class BellanFord {
static Scanner scan=new Scanner(System.in);
static int cost[][],prevd[],dist[],n;
static void bf() {
for(int k=0;k<n-1;k++) {
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(cost[j][i]!=999 && prevd[j]!=999 &&
dist[i]>prevd[j]+cost[j][i])
dist[i]=prevd[j]+cost[j][i];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(cost[j][i]!=999 && prevd[j]!=999 &&
dist[i]>prevd[j]+cost[j][i])
System.out.println("neagtive cycles");
}
}
public static void main(String[] args) {
System.out.println("Enter the of vertices");
n=scan.nextInt();
cost=new int[n][n];
dist=new int[n];
prevd=new int[n];
System.out.println("Enter cost matrix");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++) {
cost[i][j]= scan.nextInt();
if(cost[i][j]==0 && i!=j)
cost[i][j]=999;
}
System.out.println("Enter the source");
int s = scan.nextInt();
for(int l=0;l<n;l++) {
dist[l]=prevd[l]=cost[s][l];
}
bf();
System.out.println("the distances are");
for(int k=0;k<n;k++) {
System.out.println(s+" to "+ k +":"+dist[k]);
}
}
3 Dijkstra’s Program
import java.util.*;
public class Dijkstra
{
public int distance[] = new int[10];
public int cost[][] = new int[10][10];
public void calc(int n,int s)
{
int flag[] = new int[n+1];
int i,minpos=1,k,c,minimum;
for(i=1;i<=n;i++)
{
flag[i]=0;
this.distance[i]=this.cost[s][i];
}
c=2;
while(c<=n)
{
minimum=99;
for(k=1;k<=n;k++)
{
if(this.distance[k]<minimum && flag[k]!=1)
{
minimum=this.distance[i];
minpos=k;
}
}
flag[minpos]=1;
c++;for(k=1;k<=n;k++)
{
if(this.distance[minpos]+this.cost[minpos][k] < this.distance[k] && flag[k]!=1 )
this.distance[k]=this.distance[minpos]+this.cost[minpos][k];
}
}
}
public static void main(String args[])
{
int nodes,source,i,j;
Scanner in = new Scanner(System.in);
System.out.println("Enter the Number of Nodes \n");
nodes = in.nextInt();
Dijkstra d = new Dijkstra();
System.out.println("Enter the Cost Matrix Weights: \n");
for(i=1;i<=nodes;i++)
for(j=1;j<=nodes;j++)
{
d.cost[i][j]=in.nextInt();
if(d.cost[i][j]==0)
d.cost[i][j]=999;
}
System.out.println("Enter the Source Vertex :\n");
source=in.nextInt();
d.calc(nodes,source);
System.out.println("The Shortest Path from Source \t"+source+"\t to all other vertices
are : \n");
for(i=1;i<=nodes;i++)
if(i!=source)
System.out.println("source :"+source+"\t destination :"+i+"\t MinCost is :"+d.distance[i]
+"\t");
}
}
4 RSA
import java.util.*;
import java.math.*;
public class Rsa {
static Scanner scan = new Scanner(System.in);
static int p,q,n,z,e,d,ed[],dd[],num[];
static BigInteger temp;
static String msg;
static int gcd(int a, int b) {
if(a==0)
return b;
return gcd(b%a,a);
}
static void rsaAlg(String msg) {
p=7;
q=11;
n=p*q;
z=(p-1)*(q-1);
for(int i=2;i<z;i++)
if(gcd(i,z)==1) {
e=i;
break;
}
for(int i=2;i<z;i++)
if((e*i)%z==1) {
d=i;
break;
}
System.out.println("p="+p+ "q="+q+ "\nn="+n+ "z="+z+
"\ne=" +e+"d=" +d);
num= new int[msg.length()];
ed= new int[msg.length()];
dd= new int[msg.length()];
for(int i=0;i<msg.length();i++)
num[i]=msg.charAt(i)-'a'+1;
for(int i=0;i<msg.length();i++) {
temp= new BigInteger(Integer.toString(num[i]));
temp= temp.pow(e);
temp= temp.mod(new BigInteger(Integer.toString(n)));
ed[i]=temp.intValue();
}
System.out.println("encrypted data");
for(int i=0;i<msg.length();i++)
System.out.println(ed[i]);
System.out.println();
for(int i=0;i<msg.length();i++) {
temp= new BigInteger(Integer.toString(ed[i]));
temp= temp.pow(d);
temp= temp.mod(new BigInteger(Integer.toString(n)));
dd[i]=temp.intValue();
}
System.out.println("Dencrypted data");
for(int i=0;i<msg.length();i++)
System.out.println((char)(dd[i]+'a'-1));
System.out.println();
}
public static void main(String[] args) {
System.out.println("Enter the msg");
msg= scan.next();
rsaAlg(msg);
}
}
5 DES
package des;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.spec.KeySpec;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.util.Scanner;
7 Deffiee Hellmon
package program;
import java.math.BigInteger;
import java.util.Scanner;
importjava.math.BigInteger;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
importjava.util.Scanner;
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
returnhashtext;
}
catch (NoSuchAlgorithmException e)
{
throw new RuntimeException(e);
}
}
}
}
8(b) SHA-1
importjava.math.BigInteger;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
importjava.util.Scanner;
MessageDigest md = MessageDigest.getInstance("SHA");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
returnhashtext;
}
catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);
}
}
public static void main(String args[]) throws NoSuchAlgorithmException
{
System.out.println("HashCode Generated by SHA-1 for: ");
String s1 = "SHA-1 is commonly used in cryptographic applications and environments
where the need for data integrity is high.";
System.out.println("s1:" + encryptThisString(s1));
System.out.println("enter the second string:");
Scanner sc = new Scanner(System.in);
String s2=sc.nextLine();
System.out.println("s2:" + encryptThisString(s2));
sc.close();
}
}
AWK FILE
BEGIN {
c=0;
}
{
If ($1= ="d")
{
c++;
printf("%s\t%s\n",$5,$11);
}
}
/*immediately after END should open braces ‘{‘
END{
printf("The number of packets dropped =%d\n",c);
}
AWK FILE
BEGIN{
udp=0;
tcp=0;
}
{
if($1= = “r” && $5 = = “cbr”)
{
udp++;
}
else if($1 = = “r” && $5 = = “tcp”)
{
tcp++;
}
}
END{
printf(“Number of packets sent by TCP = %d\n”, tcp);
printf(“Number of packets sent by UDP=%d\n”,udp);
}
IOT Programs
exp 1 2 3
import serial
#For Serial Communication
import time
#For Delay Function
ser= serial.Serial('/dev/ttyS0', baudrate=9600, timeout=3.0)
while True:
ser.write('Hello User A \r\n')
exp 4
import bme280
import I2C_LCD_driver
from time import *
mylcd = I2C_LCD_driver.lcd()
temperature,pressure,humidity = bme280.readBME280All()
mylcd.lcd_display_string("BME280", 1)
sleep(1)
mylcd.lcd_clear()
mylcd.lcd_display_string('C=' + str(temperature ) , 1)
sleep(2)
mylcd.lcd_clear()
mylcd.lcd_display_string('hPa=' + str(pressure ) , 1)
sleep(2)
mylcd.lcd_clear()
mylcd.lcd_display_string('%=' + str(humidity ) , 1)
sleep(2)
Commands
minicom -b 9600 -o -D /dev/ttyS0
i2cdetect -y 1
END