Crypto Da
Crypto Da
Crypto Da
PLAYFAIR METHOD
PYTHON
CODE: -
def toLowerCase(text):
return text.lower()
def removeSpaces(text):
newText = ""
for i in text:
if i == " ":
continue
else:
newText = newText + i
return newText
def Diagraph(text):
Diagraph = []
group = 0
for i in range(2, len(text), 2):
Diagraph.append(text[group:i])
group = i
Diagraph.append(text[group:])
return Diagraph
def FillerLetter(text):
k = len(text)
if k % 2 == 0:
for i in range(0, k, 2):
if text[i] == text[i+1]:
new_word = text[0:i+1] + 'x' + text[i+1:]
new_word = FillerLetter(new_word)
break
else:
new_word = text
else:
for i in range(0, k-1, 2):
if text[i] == text[i+1]:
LAB ASSESMENT: - 01
new_word = text[0:i+1] + 'x' + text[i+1:]
new_word = FillerLetter(new_word)
break
else:
new_word = text
return new_word
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z']
compElements = []
for i in key_letters:
if i not in compElements:
compElements.append(i)
for i in list1:
if i not in compElements:
compElements.append(i)
matrix = []
while compElements != []:
matrix.append(compElements[:5])
compElements = compElements[5:]
return matrix
char2 = ''
if e2c == 4:
char2 = matr[e2r][0]
else:
char2 = matr[e2r][e2c+1]
char2 = ''
if e2r == 4:
char2 = matr[0][e2c]
else:
char2 = matr[e2r+1][e2c]
char2 = ''
char2 = matr[e2r][e1c]
LAB ASSESMENT: - 01
return char1, char2
if len(PlainTextList[-1]) != 2:
PlainTextList[-1] = PlainTextList[-1] + 'z'
CipherText = ""
for i in CipherList:
LAB ASSESMENT: - 01
CipherText += i
print("CipherText:", CipherText)
JAVA: -
import java.util.*;
if (i < plainText.length() - 1) {
if (plainText.charAt(i) == plainText.charAt(i + 1))
sb.append("x");
}
}
if (sb.length() % 2 != 0)
sb.append("x");
return sb.toString();
LAB ASSESMENT: - 01
}
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
if (y[0] == y[2]) {
y[1] += 1;
y[3] += 1;
} else if (y[1] == y[3]) {
y[0] += 1;
y[2] += 1;
}
return y;
}
if (indices[0] == indices[2]) {
encryptedText.append(matrix[indices[0]][indices[1]]);
encryptedText.append(matrix[indices[0]][indices[3]]);
} else if (indices[1] == indices[3]) {
encryptedText.append(matrix[indices[0]][indices[1]]);
encryptedText.append(matrix[indices[2]][indices[1]]);
} else {
encryptedText.append(matrix[indices[0]][indices[3]]);
encryptedText.append(matrix[indices[2]][indices[1]]);
LAB ASSESMENT: - 01
}
}
}
return encryptedText.toString();
}
}
CEASER Cipher
Code:-
Python
# This function generates the
# key in a cyclic manner until
# it's length isn't equal to
# the length of original text
def generateKey(string, key):
key = list(key)
if len(string) == len(key):
return key
else:
for i in range(len(string) - len(key)):
key.append(key[i % len(key)])
return ''.join(key)
# Driver code
if __name__ == "__main__":
string = input("Enter the string: ").upper()
keyword = input("Enter the keyword: ").upper()
key = generateKey(string, keyword)
cipher_text = cipherText(string, key)
print("Ciphertext:", cipher_text)
print("Original/Decrypted Text:", originalText(cipher_text, key))
JAVA:-
// Java code to implement Vigenere Cipher
class Main
{
cipher_text+=(char)(x);
}
return cipher_text;
}
// This function will convert the lower case character to Upper case
static String LowerToUpper(String s)
{
StringBuffer str =new StringBuffer(s);
for(int i = 0; i < s.length(); i++)
{
if(Character.isLowerCase(s.charAt(i)))
{
str.setCharAt(i, Character.toUpperCase(s.charAt(i)));
}
}
s = str.toString();
return s;
}
// Driver code
public static void main(String[] args)
{
String Str = "VELLOREINSTITUTE";
String Keyword = "SCOPE";
System.out.println("Ciphertext : "
LAB ASSESMENT: - 01
+ cipher_text + "\n");
HILL CYPHER
CODE : -
import java.util.Scanner;
class Main {
static int __gcd(int a, int b)
{
if (b == 0) {
LAB ASSESMENT: - 01
return a;
}
else {
return __gcd(b, a % b);
}
}
return (y % 2 == 0) ? p : (x * p) % m;
}
else {
// Driver code
LAB ASSESMENT: - 01
public static void main(String[] args)
{
int a = 3, m = 11;
modInverse(a, m);
}
}
scanner.close();
}
}
MULTIPLICATIVE INVERSE
USING EXTENDE EUCLEADIAN
THEOREM
import java.util.Scanner;
while (a > 1) {
// q is quotient
int q = a / m;
int t = m;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0) {
x += m0;
}
LAB ASSESMENT: - 01
return x;
}
if (inverse != -1) {
System.out.println("Multiplicative inverse of " + a + " modulo " + m
+ " is: " + inverse);
}
scanner.close();
}
}
scanner.close();
}
}
scanner.close();
}
}
if (m == 1)
return 0;
// Make x1 positive
if (x1 < 0)
x1 += m0;
return x1;
}
// Initialize result
long result = 0;
LAB ASSESMENT: - 01
// Apply Chinese Remainder Theorem
for (int i = 0; i < k; i++) {
long pp = prod / num[i];
result += rem[i] * modInverse(pp, num[i]) * pp;
}
scanner.close();
}
}
return result;
}
scanner.close();
LAB ASSESMENT: - 01
}
}
return result;
}
scanner.close();
}
}