Program:
class caesarCipher {
public static String encode(String enc, int offset) {
offset = offset % 26 + 26;
StringBuilder encoded = new StringBuilder();
for (char i : [Link]()) {
if ([Link](i)) {
if ([Link](i)) {
[Link]((char) ('A' + (i - 'A' + offset) % 26));
} else {
[Link]((char) ('a' + (i - 'a' + offset) % 26));
}
} else {
[Link](i);
}
}
return [Link]();
}
public static String decode(String enc, int offset) {
return encode(enc, 26 - offset);
}
public static void main(String[] args) throws [Link] {
String msg = "Anna University";
[Link]("Simulating Caesar Cipher\n------------------------");
[Link]("Input : " + msg);
[Link]("Encrypted Message : ");
[Link]([Link](msg, 3));
[Link]("Decrypted Message : ");
[Link]([Link]([Link](msg, 3), 3));
}
}
Output:
Program:
import [Link];
class playfairCipher {
private static char[][] charTable;
private static Point[] positions;
private static String prepareText(String s, boolean chgJtoI) {
s = [Link]().replaceAll("[^A-Z]", "");
return chgJtoI ? [Link]("J", "I") : [Link]("Q", "");
}
private static void createTbl(String key, boolean chgJtoI) {
charTable = new char[5][5];
positions = new Point[26];
String s = prepareText(key + "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
chgJtoI);
int len = [Link]();
for (int i = 0, k = 0; i < len; i++) {
char c = [Link](i);
if (positions[c - 'A'] == null) {
charTable[k / 5][k % 5] = c;
positions[c - 'A'] = new Point(k % 5, k / 5);
k++;
}
}
}
private static String codec(StringBuilder txt, int dir) {
int len = [Link]();
for (int i = 0; i < len; i += 2) {
char a = [Link](i);
char b = [Link](i + 1);
int row1 = positions[a - 'A'].y;
int row2 = positions[b - 'A'].y;
int col1 = positions[a - 'A'].x;
int col2 = positions[b - 'A'].x;
if (row1 == row2) {
col1 = (col1 + dir) % 5;
col2 = (col2 + dir) % 5;
} else if (col1 == col2) {
row1 = (row1 + dir) % 5;
row2 = (row2 + dir) % 5;
} else {
int tmp = col1;
col1 = col2;
col2 = tmp;
}
[Link](i, charTable[row1][col1]);
[Link](i + 1, charTable[row2][col2]);
}
return [Link]();
}
private static String encode(String s) {
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < [Link](); i += 2) {
if (i == [Link]() - 1) {
[Link]([Link]() % 2 == 1 ? 'X' : "");
} else if ([Link](i) == [Link](i + 1)) {
[Link](i + 1, 'X');
}
}
return codec(sb, 1);
}
private static String decode(String s) {
return codec(new StringBuilder(s), 4);
}
public static void main(String[] args) throws [Link] {
String key = "CSE";
String txt = "Security Lab"; /* make sure string length is even */ /* change
J to I */
boolean chgJtoI = true;
createTbl(key, chgJtoI);
String enc = encode(prepareText(txt, chgJtoI));
[Link]("Simulating Playfair Cipher\n----------------------");
[Link]("Input Message : " + txt);
[Link]("Encrypted Message : " + enc);
[Link]("Decrypted Message : " + decode(enc));
}
}
Output:
Program:
class hillCipher {
/* 3x3 key matrix for 3 characters at once */
public static int[][] keymat = new int[][] { { 1, 2, 1 }, { 2, 3, 2 },
{ 2, 2, 1 } }; /* key inverse matrix */
public static int[][] invkeymat = new int[][] { { -1, 0, 1 }, { 2, -1, 0 }, { -2, 2,
-1 } };
public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static String encode(char a, char b, char c) {
String ret = "";
int x, y, z;
int posa = (int) a - 65;
int posb = (int) b - 65;
int posc = (int) c - 65;
x = posa * keymat[0][0] + posb * keymat[1][0] + posc * keymat[2][0];
y = posa * keymat[0][1] + posb * keymat[1][1] + posc * keymat[2][1];
z = posa * keymat[0][2] + posb * keymat[1][2] + posc * keymat[2][2];
a = [Link](x % 26);
b = [Link](y % 26);
c = [Link](z % 26);
ret = "" + a + b + c;
return ret;
}
private static String decode(char a, char b, char c) {
String ret = "";
int x, y, z;
int posa = (int) a - 65;
int posb = (int) b - 65;
int posc = (int) c - 65;
x = posa * invkeymat[0][0] + posb * invkeymat[1][0] + posc *
invkeymat[2][0];
y = posa * invkeymat[0][1] + posb * invkeymat[1][1] + posc *
invkeymat[2][1];
z = posa * invkeymat[0][2] + posb * invkeymat[1][2] + posc *
invkeymat[2][2];
a = [Link]((x % 26 < 0) ? (26 + x % 26) : (x % 26));
b = [Link]((y % 26 < 0) ? (26 + y % 26) : (y % 26));
c = [Link]((z % 26 < 0) ? (26 + z % 26) : (z % 26));
ret = "" + a + b + c;
return ret;
}
public static void main(String[] args) throws [Link] {
String msg;
String enc = "";
String dec = "";
int n;
msg = ("SecurityLaboratory");
[Link]("simulation of Hill Cipher\n-------------------------");
[Link]("Input message : " + msg);
msg = [Link]();
msg = [Link]("\\s", "");
/* remove spaces */ n = [Link]() % 3;
/* append padding text X */ if (n != 0) {
for (int i = 1; i <= (3 - n); i++) {
msg += 'X';
}
}
[Link]("padded message : " + msg);
char[] pdchars = [Link]();
for (int i = 0; i < [Link](); i += 3) {
enc += encode(pdchars[i], pdchars[i + 1], pdchars[i + 2]);
}
[Link]("encoded message : " + enc);
char[] dechars = [Link]();
for (int i = 0; i < [Link](); i += 3) {
dec += decode(dechars[i], dechars[i + 1], dechars[i + 2]);
}
[Link]("decoded message : " + dec);
}
}
Output:
Program:
public class vigenereCipher {
static String encode(String text, final String key) {
String res = "";
text = [Link]();
for (int i = 0, j = 0; i < [Link](); i++) {
char c = [Link](i);
if (c < 'A' || c > 'Z') {
continue;
}
res += (char) ((c + [Link](j) - 2 * 'A') % 26 + 'A');
j = ++j % [Link]();
}
return res;
}
static String decode(String text, final String key) {
String res = "";
text = [Link]();
for (int i = 0, j = 0; i < [Link](); i++) {
char c = [Link](i);
if (c < 'A' || c > 'Z') {
continue;
}
res += (char) ((c - [Link](j) + 26) % 26 + 'A');
j = ++j % [Link]();
}
return res;
}
public static void main(String[] args) throws [Link] {
String key = "VIGENERECIPHER";
String msg = "SecurityLaboratory";
[Link]("Simulating Vigenere Cipher\n------------------------");
[Link]("Input Message : " + msg);
String enc = encode(msg, key);
[Link]("Encrypted Message : " + enc);
[Link]("Decrypted Message : " + decode(enc, key));
}
}
Output:
Program:
class railfenceCipherHelper {
int depth;
String encode(String msg, int depth) throws Exception {
int r = depth;
int l = [Link]();
int c = l / depth;
int k = 0;
char mat[][] = new char[r][c];
String enc = "";
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++) {
if (k != l) {
mat[j][i] = [Link](k++);
} else {
mat[j][i] = 'X';
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
enc += mat[i][j];
}
}
return enc;
}
String decode(String encmsg, int depth) throws Exception {
int r = depth;
int l = [Link]();
int c = l / depth;
int k = 0;
char mat[][] = new char[r][c];
String dec = "";
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
mat[i][j] = [Link](k++);
}
}
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++) {
dec += mat[j][i];
}
}
return dec;
}
}
class railFenceCipher {
public static void main(String[] args) throws [Link] {
railfenceCipherHelper rf = new railfenceCipherHelper();
String msg, enc, dec;
msg = "Anna University, Chennai";
int depth = 2;
enc = [Link](msg, depth);
dec = [Link](enc, depth);
[Link]("Simulating Railfence Cipher\n-------------------------");
[Link]("Input Message : " + msg);
[Link]("Encrypted Message : " + enc);
[Link]("Decrypted Message : " + dec);
}
}
Output:
Program:
import [Link].*;
class TransCipher {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the plain text");
String pl = [Link]();
[Link]();
String s = "";
int start = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == ' ') {
s = s + [Link](start, i);
start = i + 1;
}
}
s = s + [Link](start);
[Link](s);
[Link]();
// end of space deletion
int k = [Link]();
int l = 0;
int col = 4;
int row = [Link]() / col;
char ch[][] = new char[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (l < k) {
ch[i][j] = [Link](l);
l++;
} else {
ch[i][j] = '#';
}
}
}
// arranged in matrix
char trans[][] = new char[col][row];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
trans[j][i] = ch[i][j];
}
}
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
[Link](trans[i][j]);
}
}
// display
[Link]();
}
}
Output:
Program:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class DES
{
public static void main(String[] argv) {
try{
[Link]("Message Encryption Using DES Algorithm\
n-------");
KeyGenerator keygenerator =
[Link]("DES");
SecretKey myDesKey = [Link]();
Cipher desCipher;
desCipher = [Link]("DES/ECB/PKCS5Padding");
[Link](Cipher.ENCRYPT_MODE, myDesKey);
byte[] text = "Secret Information ".getBytes();
[Link]("Message [Byte Format] : " + text);
[Link]("Message : " + new String(text));
byte[] textEncrypted = [Link](text);
[Link]("Encrypted Message: " + textEncrypted);
[Link](Cipher.DECRYPT_MODE, myDesKey);
byte[] textDecrypted = [Link](textEncrypted);
[Link]("Decrypted Message: " + new
String(textDecrypted));
}catch(NoSuchAlgorithmException e){
[Link]();
}catch(NoSuchPaddingException e){
[Link]();
}catch(InvalidKeyException e){
[Link]();
}catch(IllegalBlockSizeException e){
[Link]();
}catch(BadPaddingException e){
[Link]();}
}
}
Output:
Program:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].Base64;
import [Link];
import [Link];
public class AES {
private static SecretKeySpec secretKey;
private static byte[] key;
public static void setKey(String myKey) {
MessageDigest sha = null;
try {
key = [Link]("UTF-8");
sha = [Link]("SHA-1");
key = [Link](key);
key = [Link](key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
[Link]();
} catch (UnsupportedEncodingException e) {
[Link]();
}
}
public static String encrypt(String strToEncrypt, String secret) {
try {
setKey(secret);
Cipher cipher = [Link]("AES/ECB/PKCS5Padding");
[Link](Cipher.ENCRYPT_MODE, secretKey);
return
[Link]().encodeToString([Link]([Link]("U
TF-8")));
} catch (Exception e) {
[Link]("Error while encrypting: " + [Link]());
}
return null;
}
public static String decrypt(String strToDecrypt, String secret) {
try {
setKey(secret);
Cipher cipher = [Link]("AES/ECB/PKCS5PADDING");
[Link](Cipher.DECRYPT_MODE, secretKey);
return new
String([Link]([Link]().decode(strToDecrypt)));
} catch (Exception e) {
[Link]("Error while decrypting: " + [Link]());
}
return null;
}
public static void main(String[] args) {
final String secretKey = "annaUniversity";
String originalString = "[Link]";
String encryptedString = [Link](originalString, secretKey);
String decryptedString = [Link](encryptedString, secretKey);
[Link]("URL Encryption Using AES Algorithm\n------------");
[Link]("Original URL : " + originalString);
[Link]("Encrypted URL : " + encryptedString);
[Link]("Decrypted URL : " + decryptedString);
}
}
Output:
Program:
<html>
<head>
<title>RSA Encryption</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>RSA Algorithm</h1>
<h2>Implemented Using HTML & Javascript</h2>
<hr>
<table>
<tr>
<td>Enter First Prime Number:</td>
<td><input type="number" value="53" id="p"></td>
</tr>
<tr>
<td>Enter Second Prime Number:</td>
<td><input type="number" value="59" id="q"></p>
</td>
</tr>
<tr>
<td>Enter the Message(cipher text):<br>[A=1, B=2,...]</td>
<td><input type="number" value="89" id="msg"></p>
</td>
</tr>
<tr>
<td>Public Key:</td>
<td>
<p id="publickey"></p>
</td>
</tr>
<tr>
<td>Exponent:</td>
<td>
<p id="exponent"></p>
</td>
</tr>
<tr>
<td>Private Key:</td>
<td>
<p id="privatekey"></p>
</td>
</tr>
<tr>
<td>Cipher Text:</td>
<td>
<p id="ciphertext"></p>
</td>
</tr>
<tr>
<td><button onclick="RSA();">Apply RSA</button></td>
</tr>
</table>
</center>
</body>
<script type="text/javascript">
function RSA() {
var gcd, p, q, no, n, t, e, i, x;
gcd = function (a, b) { return (!b) ? a : gcd(b, a % b); };
p = [Link]('p').value;
q = [Link]('q').value;
no = [Link]('msg').value;
n = p * q;
t = (p - 1) * (q - 1);
for (e = 2; e < t; e++) {
if (gcd(e, t) == 1) {
break;
}
}
for (i = 0; i < 10; i++) {
x=1+i*t
if (x % e == 0) {
d = x / e;
break;
}
}
ctt = [Link](no, e).toFixed(0);
ct = ctt % n;
dtt = [Link](ct, d).toFixed(0);
dt = dtt % n;
[Link]('publickey').innerHTML = n;
[Link]('exponent').innerHTML = e;
[Link]('privatekey').innerHTML = d;
[Link]('ciphertext').innerHTML = ct;
}
</script>
</html>
Output:
Program:
class DiffieHellman {
public static void main(String args[]) {
int p = 23; /* publicly known (prime number) */
int g = 5; /* publicly known (primitive root) */
int x = 4; /* only Alice knows this secret */
int y = 3; /* only Bob knows this secret */
double aliceSends = ([Link](g, x)) % p;
double bobComputes = ([Link](aliceSends, y)) % p;
double bobSends = ([Link](g, y)) % p;
double aliceComputes = ([Link](bobSends, x)) % p;
double sharedSecret = ([Link](g, (x * y))) % p;
[Link]("simulation of Diffie-Hellman key exchange algorithm\
n---------------------------------------------");
[Link]("Alice Sends : " + aliceSends);
[Link]("Bob Computes : " + bobComputes);
[Link]("Bob Sends : " + bobSends);
[Link]("Alice Computes : " + aliceComputes);
[Link]("Shared Secret : " + sharedSecret);
/* shared secrets should match and equality is transitive */
if ((aliceComputes == sharedSecret) && (aliceComputes ==
bobComputes))
[Link]("Success: Shared Secrets Matches! " + sharedSecret);
else
[Link]("Error: Shared Secrets does not Match");
}
}
Output:
Program:
import [Link].*;
public class sha1 {
public static void main(String[] a) {
try {
MessageDigest md = [Link]("SHA1");
[Link]("Message digest object info:\n-----------------");
[Link]("Algorithm=" + [Link]());
[Link]("Provider=" + [Link]());
[Link]("ToString=" + [Link]());
String input = "";
[Link]([Link]());
byte[] output = [Link]();
[Link]();
[Link]("SHA1(\"" + input + "\")=" + bytesToHex(output));
input = "abc";
[Link]([Link]());
output = [Link]();
[Link]();
[Link]("SHA1(\"" + input + "\")=" + bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
[Link]([Link]());
output = [Link]();
[Link]();
[Link]("SHA1(\"" + input + "\")=" + bytesToHex(output));
[Link]();
} catch (Exception e) {
[Link]("Exception:" + e);
}
}
private static String bytesToHex(byte[] b) {
char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
StringBuffer buf = new StringBuffer();
for (byte aB : b) {
[Link](hexDigit[(aB >> 4) & 0x0f]);
[Link](hexDigit[aB & 0x0f]);
}
return [Link](); }
}
Output:
Program:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class CreatingDigitalSignature {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner([Link]);
[Link]("Enter some text");
String msg = [Link]();
KeyPairGenerator keyPairGen = [Link]("DSA");
[Link](2048);
KeyPair pair = [Link]();
PrivateKey privKey = [Link]();
Signature sign = [Link]("SHA256withDSA");
[Link](privKey);
byte[] bytes = "msg".getBytes();
[Link](bytes);
byte[] signature = [Link]();
[Link]("Digital signature for given text: "+new String(signature,
"UTF8"));
}
}
Output: