1
AIM: Write a C program that contains a string (char pointer) with a value \Hello
World’. The program should XOR each character in this string with 0 and display
the result.
PROGRAM:
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
void main()
{
char str[]="Hello World";
char str1[11];
int i,len; len=strlen(str);
for(i=0;i<len;i++)
{
str1[i]=str[i]^0;
printf("%c",str1[i]);
}
printf("\n");
}
output:
Hello World
AIM: Write a C program that contains a string (char pointer) with a value
\Hello World’. The program should AND or and XOR each character in this string
with 127 and displaythe result.
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
void main()
{
char str[]="Hello World";
char str1[11];
char str2[11];
int i,len;
len = strlen(str);
for(i=0;i<len;i++)
{
str1[i] =str[i]&127; printf("%c",str1[i]);
}
printf("\n");
for(i=0;i<len;i++)
{
str2[i] =str[i]^127;
printf("%c",str2[i]);
}
printf("\n");
}
output:
Hello World
7�_(
3
AIM: Write a Javaprogramtoperformencryptionanddecryption usingthe
following algorithms:
a) Ceaser Cipher
b) Substitution Cipher
c) Hill Cipher
Ceaser Cipher
import [Link];
import [Link];
import [Link];
import [Link];
public class CeaserCipher {
static Scanner sc=new Scanner([Link]);
static BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
public static void main(String[] args) throws IOException {
// TODO code application logic here
[Link]("Enter any String: ");
String str = [Link]();
[Link]("\nEnter the Key: ");
int key= [Link]();
String encrypted = encrypt(str, key);
[Link]("\nEncrypted String is: " +encrypted);
String decrypted = decrypt(encrypted, key);
[Link]("\nDecrypted String is: "+decrypted);
[Link]("\n");
}
public static String encrypt(String str, int key)
{
String encrypted ="";
for(int i = 0; i < [Link](); i++)
{
int c= [Link](i);
if ([Link](c)) {
c = c + (key % 26);
if (c > 'Z')
c = c - 26;
}
else if ([Link](c)) {
c = c + (key % 26);
if (c > 'z')
c = c - 26;
}
encrypted += (char) c;
}
return encrypted;
}
public static String decrypt(String str, int key)
{
String decrypted = "";
for(int i= 0; i < [Link](); i++)
{
int c = [Link](i);
if ([Link](c)) {
c = c - (key % 26);
if (c < 'A')
c = c + 26;
}
else if ([Link](c)) {
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return decrypted;
}
}
output:
Enter any String: hellow
Enter the Key: 5
Encrypted String is: mjqqtb
Decrypted String is: hellow
Substitution Cipher:
import [Link].*;
import [Link].*;
public class SubstitutionCipher{
static Scanner sc = new Scanner([Link]);
static BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
public static void main(String[] args) throws IOException {
// TODO code application logic here
String a= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String b = "zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA";
[Link]("Enter any string: ");
String str = [Link]();
String decrypt = "";
char c;
for(int i=0;i<[Link]();i++)
{
c = [Link](i);
int j = [Link](c);
decrypt = decrypt+[Link](j);
}
[Link]("The encrypted data is: " +decrypt);
}
}
output:
Enter any string: SwarnanDhra
The encrypted data is: HdzimzmWsiz
Hill Cipher:
import [Link].*;
import [Link].*;
import [Link].*;
public class HillCipher {
static float[][] decrypt = new float[3][1];
static float[][] a = new float[3][3];
static float[][] b =new float[3][3];
static float[][] mes = new float[3][1];
static float[][] res = new float[3][1];
static BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
static Scanner sc = new Scanner([Link]);
public static void main(String[] args) throws IOException {
// TODOcode applicationlogic here
getkeymes();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int k=0;k<3;k++)
{
res[i][j]=res[i][j]+a[i][k]*mes[k][j];
}
[Link]("\nEncrypted string is : ");
for(int i=0;i<3;i++)
{
[Link]((char)(res[i][0]%26+97));
res[i][0]=res[i][0];
}
inverse();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int k=0;k<3;k++) {
decrypt[i][j] = decrypt[i][j]+b[i][k]*res[k][j];
}
[Link]("\nDecrypted string is : ");
for(int i=0;i<3;i++){
[Link]((char)(decrypt[i][0]% 26+97));
}
[Link]("\n");
}
public static void getkeymes() throws IOException {
[Link]("Enter 3x3 matrix for key (It should be inversible): ");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]= [Link]();
[Link]("\nEnter a 3 letter string: ");
String msg = [Link]();
for(int i=0;i<3;i++)
mes[i][0]= [Link](i)-97;
}
public static void inverse()
{
float p,q;
float[][] c = a;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
//a[i][j]=[Link]();
if(i==j)
b[i][j]=1;
else b[i][j]=0;
}
for(int k=0;k<3;k++)
{ for(int i=0;i<3;i++) {
p = c[i][k];
q = c[k][k];
for(int j=0;j<3;j++)
{
if(i!=k)
{
c[i][j] = c[i][j]*q-p*c[k][j];
b[i][j] =b[i][j]*q-p*b[k][j];
}
}
}
}
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
b[i][j] = b[i][j]/c[i][i]; }
[Link]("");
[Link]("\nInverse Matrix is : ");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) [Link](b[i][j] + " ");
[Link]("\n"); }
}}
output:
4
DES:
AIM: Write a Java program to implement the DES algorithm logic.
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
class DES{
public static void main(String[] args) throws IOException,
NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException,
NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
//String we want to encrypt
String message="This is a confidential message.";
byte[] myMessage =[Link](); //string to byte array as DES works on
bytes
//If you want to use your own key
// SecretKeyFactory MyKeyFactory = [Link]("DES");
// String Password = "My Password";
// byte[] mybyte =[Link]();
// DESKeySpec myMaterial = new DESKeySpec(mybyte);
// SecretKey myDESKey = [Link](myMaterial);
//Generating Key
KeyGenerator Mygenerator = [Link]("DES");
SecretKey myDesKey = [Link]();
//initializing crypto algorithm
Cipher myCipher = [Link]("DES");
//setting encryption mode
[Link](Cipher.ENCRYPT_MODE, myDesKey);
byte[] myEncryptedBytes=[Link](myMessage);
//setting decryption mode
[Link](Cipher.DECRYPT_MODE, myDesKey);
byte[] myDecryptedBytes=[Link](myEncryptedBytes);
//print message in byte format
//[Link]([Link](myEncryptedBytes));
//[Link]([Link](myDecryptedBytes));
String encrypteddata=new String(myEncryptedBytes);
String decrypteddata=new String(myDecryptedBytes);
[Link]("Message : "+ message);
[Link]("Encrypted - "+ encrypteddata);
[Link]("Decrypted Message - "+ decrypteddata);
}
}
output:
Message : This is a confidential message.
Encrypted - /??C?aT??8 ??????????P?\??
Decrypted Message - This is a confidential message.