0% found this document useful (0 votes)
5 views14 pages

cns output

The document contains multiple Python and Java scripts for cryptographic algorithms, including RSA key generation, encryption, decryption, signing, and verification, as well as Caesar, Substitution, and Hill ciphers. Each script demonstrates how to encrypt and decrypt messages using the respective algorithms and includes user input for messages and keys. Outputs from the scripts show the encrypted and decrypted results, along with verification of signatures where applicable.

Uploaded by

adityadhavale22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

cns output

The document contains multiple Python and Java scripts for cryptographic algorithms, including RSA key generation, encryption, decryption, signing, and verification, as well as Caesar, Substitution, and Hill ciphers. Each script demonstrates how to encrypt and decrypt messages using the respective algorithms and includes user input for messages and keys. Outputs from the scripts show the encrypted and decrypted results, along with verification of signatures where applicable.

Uploaded by

adityadhavale22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Rsa.

py
import rsa

def generate_keys():
pubKey =
rsa.pubKey()
with open('keys/pubkey.pem','wb') as
f:
f.write(pubKey.save_pkcs1('PEM')
)

with open('keys/prikey.pem','wb') as
f:
f.write(priKey.save_pkcs1('PEM')
)

def load_keys():
with open('keys/pubkey.pem','rb') as f:
pubKey = rsa.PublicKey.load_pkcs1(f.read())

with open('keys/prikey.pem','rb') as f:
priKey = rsa.PrivateKey.load_pkcs1(f.read())

return pubKey, priKey


try:
return rsa.decrypt(ciphertext,key).decode('ascii')
except:
return False

def sign_sha1(msg, key):


return rsa.sign(msg.encode('ascii'), key, 'SHA-1')

def verify_sha1(msg, signature,


key): try:
return rsa.verify(msg.encode('ascii'), signature, key) == 'SHA-
1' except:
return False

generate_keys()
pubKey, priKey = load_keys()

message = input('Enter a Message:')


ciphertext = encrypt(message,
pubKey)

signature = sign_sha1(message, priKey)

plaintext = decrypt(ciphertext, priKey)

print(f'Cipher text: {ciphertext}')


print(f'Signature: {signature}')

if plaintext:
print(f'Plain Text: {plaintext}')
else:
print('Could not decrypt the message')

if verify_sha1(plaintext, signature, pubKey):


print('Signature is Verified!!!!')
else:
print('Could not verify the message signature..')
Digisign.py
import rsa

def generate_keys():
pubKey = rsa.pubKey()
with open('keys/pubkey.pem','wb') as f:
f.write(pubKey.save_pkcs1('PEM'))

with open('keys/prikey.pem','wb') as f:
f.write(priKey.save_pkcs1('PEM'))

def load_keys():
with open('keys/pubkey.pem','rb') as f:
pubKey = rsa.PublicKey.load_pkcs1(f.read())

with open('keys/prikey.pem','rb') as f:
priKey = rsa.PrivateKey.load_pkcs1(f.read())

return pubKey, priKey

def encrypt(msg, key):


return rsa.encrypt(msg.encode('ascii'), key)

def decrypt(ciphertext, key):


try:
return rsa.decrypt(ciphertext,key).decode('ascii')
except:
return False

def sign_sha1(msg, key):


return rsa.sign(msg.encode('ascii'), key, 'SHA-1')

def verify_sha1(msg, signature, key):


try:
return rsa.verify(msg.encode('ascii'), signature, key) == 'SHA-1'
except:
return False

generate_keys()
pubKey, priKey = load_keys()

message = input('Enter a Message:')


ciphertext = encrypt(message, pubKey)

signature = sign_sha1(message, priKey)


plaintext = decrypt(ciphertext, priKey)

print(f'Cipher text: {ciphertext}')


print(f'Signature: {signature}')

if plaintext:
print(f'Plain Text: {plaintext}')
else:
print('Could not decrypt the message')

if verify_sha1(plaintext, signature, pubKey):


print('Signature is Verified!!!!')
else:
print('Could not verify the message signature..')

DES.py
Outputs
PROGRAM:
import
java.io.Buffe
redReader;
import
java.io.IOEx
ception;
import
java.io.Input
StreamRead
er; import
java.util.Sca
nner;
public class CeaserCipher {
static Scanner sc=new Scanner(System.in);
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); public static void main(String[]
args) throws IOException {
// TODO code
application logic
here
System.out.print("
Enter any String:
"); String str =
br.readLine();
System.out.print("
\nEnter the Key:
"); int key =
sc.nextInt();
String encrypted = encrypt(str, key);
System.out.println("\nEncrypted String
is: " +encrypted); String decrypted =
decrypt(encrypted, key);
System.out.println("\nDecrypted String
is: "
+decrypted); System.out.println("\n");
}
public static String encrypt(String str, int key)

{ String encrypted = "";


for(int i = 0; i
< str.length();
i++) { int c =
str.charAt(i);
if
(Characte
r.isUpper
Case(c)) {
c=c+
(key 26);
public class CaesarCipher {
public static String encrypt(String str, int key) {
String encrypted = "";
for (int i = 0; i < str.length(); i++) {
int c = str.charAt(i);

if (Character.isUpperCase(c)) {
c = c + (key % 26);
if (c > 'Z') c = c - 26;
}
else if (Character.isLowerCase(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 < str.length(); i++) {
int c = str.charAt(i);

if (Character.isUpperCase(c)) {
c = c - (key % 26);
if (c < 'A') c = c + 26;
}
else if (Character.isLowerCase(c)) {
c = c - (key % 26);
if (c < 'a') c = c + 26;
}

decrypted += (char) c;
}
return decrypted;
}

public static void main(String[] args) {


String text = "HelloWorld";
int shift = 3;

String encryptedText = encrypt(text, shift);


System.out.println("Encrypted: " + encryptedText);

String decryptedText = decrypt(encryptedText, shift);


System.out.println("Decrypted: " + decryptedText);
}
}
Output:
Enter any String: hello World
Enter the Key: 5
Encrypted String is: MjqqtBtwqi
Decrypted String is: Hello World
import java.io.*;
import java.util.*;

public class SubstitutionCipher {


static Scanner sc = new Scanner(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException {


// Define the original and substitution alphabets
String a = "abcdefghijklmnopqrstuvwxyz";
String b = "zyxwvutsrqponmlkjihgfedcba";

// Get user input


System.out.print("Enter any string: ");
String str = br.readLine();

String decrypt = "";


char c;

// Encrypt the string using the substitution cipher


for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
int j = a.indexOf(c);
if (j != -1) {
decrypt += b.charAt(j);
} else {
decrypt += c; // Keep non-alphabetic characters unchanged
}
}

// Display the encrypted output


System.out.println("The encrypted data is: " + decrypt);
}
}
Output:
Enter any string: aceho
The encrypted data is: zxvsl
import java.io.*;
import java.util.*;

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(System.in));


static Scanner sc = new Scanner(System.in);

public static void main(String[] args) throws IOException {


getKeyMessage();

// Encryption Process
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];

System.out.print("\nEncrypted string is: ");


for (int i = 0; i < 3; i++) {
System.out.print((char) (res[i][0] % 26 + 97));
}

// Decryption Process
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];

System.out.print("\nDecrypted string is: ");


for (int i = 0; i < 3; i++) {
System.out.print((char) (decrypt[i][0] % 26 + 97));
}
System.out.print("\n");
}

public static void getKeyMessage() throws IOException {


System.out.println("Enter 3x3 matrix for key (It should be invertible): ");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
a[i][j] = sc.nextFloat();

System.out.print("\nEnter a 3-letter string: ");


String msg = br.readLine();
for (int i = 0; i < 3; i++)
mes[i][0] = msg.charAt(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++)
b[i][j] = (i == j) ? 1 : 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];

System.out.println("\nInverse Matrix is: ");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
System.out.print(b[i][j] + " ");
System.out.print("\n");
}
}
}
Hill Cipher Output

Enter a 3-letter string: hai

Encrypted string is: fdx

Inverse Matrix is:

0.083333336 0.41666666 -0.33333334


-0.41666666 -0.083333336 0.6666667
0.5833333 -0.083333336 -0.33333334

Decrypted string is: hai

You might also like