0% found this document useful (0 votes)
109 views5 pages

Cryptography and Network Security Assignment 1) A Java Program For Shift Cipher

The document contains code snippets for three cryptography programs: 1) A Java program that implements a shift cipher encryption. 2) A PHP program that also implements a shift cipher encryption and decryption. 3) A Java program that implements a brute force attack to decrypt ciphertext without knowing the encryption key. The program tries all possible keys from 1-26 to decrypt the message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views5 pages

Cryptography and Network Security Assignment 1) A Java Program For Shift Cipher

The document contains code snippets for three cryptography programs: 1) A Java program that implements a shift cipher encryption. 2) A PHP program that also implements a shift cipher encryption and decryption. 3) A Java program that implements a brute force attack to decrypt ciphertext without knowing the encryption key. The program tries all possible keys from 1-26 to decrypt the message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

University of Rwanda

College of Science and Technology


Department of Computer Science
Level 4
Reg.: 217010849

Cryptography and network security Assignment

1) A java program for shift cipher

package shift_cipheredText;
import java.util.*;
public class cipheredText {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int yourNumber, size;
String letters="abcdefghijklmnopqrstuvwxyz";
String yourText, cipheredText="";
System.out.println("Enter the yourText: ");
yourText=sc.nextLine();
System.out.print("Enter the yourNumber: ");
yourNumber = sc.nextInt();
//int k=yourText.length();
//String[] cipheredText=new String[k];
for(int i=0;i<yourText.length();i++) {
char k = yourText.charAt(i);
letters="abcdefghijklmnopqrstuvwxyz";
if(Character.isUpperCase(k))
{
lettres="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
size = lettres.indexOf(yourText.charAt(i)) + yourNumber;
if(size > 25){
size = size % 26;
}
if(yourText.charAt(i)==' ')
{
cipheredText+=" ";
continue;
}
cipheredText = cipheredText + alphabet.charAt(size);
}
System.out.println(cipheredText);
}
}
2) a php program to imprement shift cipher

<?php

// if encrypt button was clicked


if (isset($_POST['encrypt']))
{
$code = encrypt($pswd, $code);
$error = "Text encrypted successfully!";
$color = "#526F35";
}

// if decrypt button was clicked


if (isset($_POST['decrypt']))
{
$code = decrypt($pswd, $code);
$error = "Code decrypted successfully!";
$color = "#526F35";
}

// function to encrypt the text given


function encrypt($pswd, $text)
{
// change key to lowercase for simplicity
$pswd = strtolower($pswd);

// intialize variables
$code = "";
$ki = 0;
$kl = strlen($pswd);
$length = strlen($text);

// iterate over each line in text


for ($i = 0; $i < $length; $i++)
{
// if the letter is alpha, encrypt it
if (ctype_alpha($text[$i]))
{
// uppercase
if (ctype_upper($text[$i]))
{
$text[$i] = chr(((ord($pswd[$ki]) - ord("a") + ord($text[$i]) -
ord("A")) % 26) + ord("A"));
}

// lowercase
else
{
$text[$i] = chr(((ord($pswd[$ki]) - ord("a") + ord($text[$i]) -
ord("a")) % 26) + ord("a"));
}
// update the index of key
$ki++;
if ($ki >= $kl)
{
$ki = 0;
}
}
}

// return the encrypted code


return $text;
}

// function to decrypt the text given


function decrypt($pswd, $text)
{
// change key to lowercase for simplicity
$pswd = strtolower($pswd);

// intialize variables
$code = "";
$ki = 0;
$kl = strlen($pswd);
$length = strlen($text);

// iterate over each line in text


for ($i = 0; $i < $length; $i++)
{
// if the letter is alpha, decrypt it
if (ctype_alpha($text[$i]))
{
// uppercase
if (ctype_upper($text[$i]))
{
$x = (ord($text[$i]) - ord("A")) - (ord($pswd[$ki]) - ord("a"));

if ($x < 0)
{
$x += 26;
}

$x = $x + ord("A");

$text[$i] = chr($x);
}

// lowercase
else
{
$x = (ord($text[$i]) - ord("a")) - (ord($pswd[$ki]) - ord("a"));

if ($x < 0)
{
$x += 26;
}

$x = $x + ord("a");

$text[$i] = chr($x);
}

// update the index of key


$ki++;
if ($ki >= $kl)
{
$ki = 0;
}
}
}

// return the decrypted text


return $text;
}

?>

3) a java program that implement brute force attack


package bruteforceattack;

import java.util.Scanner;
public class BruteForceAlgo {
public static void main(String[] args) {
Scanner t = new Scanner(System.in);
int m, j, l, k = 97, key = 0, flag = 0, intex = 0, keyVal;
String mytext;
char[] txt = new char[10];
char[] myText1 = new char[10];
char temp;
System.out.println("ENTER PLAIN TEXT");
p = t.next();
System.out.println("ENTER KEY VALUE :");
key = t.nextInt();
for (m = 0; i < mytext.length(); i++) {
for (j = 0; j < 26; j++) {

if (m.charAt(i) == ' ') {


flag = 0;
break;
}
temp = (char) (j + k);
if (m.charAt(i) == temp) {
flag = 1;
index = j;
break;
}
}
if (flag == 1) {

char s = (char) (((index + key) % 26) + 97);


txt[i] = s;
}
}
System.out.println("ENCRYPTED DATA:");
for (i = 0; i < p.length(); i++) {
System.out.print(txt[i]);
}
System.out.println("\n" + "DECRYPTION OF DATA USING BRUTE-FORCE ATTACK :");
key = 1;
while (key <= 26) {
for (i = 0; i < m.length(); i++) {
for (j = 0; j < 26; j++) {
if (txt[i] == ' ') {
flag = 0;
break;
}
temp = (char) (j + k);
if (txt[i] == temp) {
flag = 1;
index = j;
break;
}
}
keyVal = index - key;
if (flag == 1 & keyVal > 0) {
myText1[i] = (char) ((keyVal % 26) + 97);

} else if (flag == 1) {
p1[i] = (char) ((26 + keyVal) + 97);
}
}
System.out.print("\n" + "DECRYPTED DATA:");
for (i = 0; i < m.length(); i++) {
System.out.print(myText1[i]);
}
key++;
}
}
}

You might also like