All Cns Code
All Cns Code
OUTPUT:-
Text :ATTACKATONCE
Shift : 4
Cipher : EXXEGOEXSRGI
Vigenere Cipher:-
//encryption code
for(i = 0; i < msgLen; ++i)
encryptedMsg[i] = (char) (((msg[i] + key[i]) % 26) + 'A');
//decryption code
for(i = 0; i < msgLen; ++i)
decryptedMsg[i] = (char)((((encryptedMsg[i] - key[i]) + 26) % 26) + 'A');
RailFenceBasic:-
import java.util.*;
class RailFenceBasic{
int depth;
String Encryption(String plainText,int depth)throws Exception
{
int r=depth,len=plainText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;
String cipherText="";
String plainText="";
return plainText;
}
}
class RailFence{
public static void main(String args[])throws Exception
{
RailFenceBasic rf=new RailFenceBasic();
Scanner scn=new Scanner(System.in);
int depth;
String plainText,cipherText,decryptedText;
cipherText=rf.Encryption(plainText,depth);
System.out.println("Encrypted text is:\n"+cipherText);
decryptedText=rf.Decryption(cipherText, depth);
}
}
OUTPUT:-
Enter plain text:
KARRTHIK
Enter depth for Encryption:
4
Encrypted text is:
KTAHRIRK
Decrypted text is:
KARRTHIK
RSA:-
import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;
private BigInteger P;
private BigInteger Q;
private BigInteger N;
private BigInteger e;
private BigInteger d;
private Random R;
public RSA()
R = new Random();
P = BigInteger.probablePrime(maxLength, R);
Q = BigInteger.probablePrime(maxLength, R);
N = P.multiply(Q);
e = BigInteger.probablePrime(maxLength / 2, R);
e.add(BigInteger.ONE);
d = e.modInverse(PHI);
this.e = e;
this.d = d;
this.N = N;
String inputString;
inputString = input.readLine();
+ bToS(inputString.getBytes()));
// encryption
// decryption
temp += Byte.toString(b);
return temp;
OUTPUT:
Enter message you wish to send.
KARRTHIK
Encrypting the message: KARRTHIK
The message in bytes is:: 7565828284727375
Decrypting Bytes: 7565828284727375
Plain message is: KARRTHIK
SHA-1
import java.security.*;
class JceSha1Test {
public static void main(String[] a) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
System.out.println("Message digest object info: ");
System.out.println(" Algorithm = "+md.getAlgorithm());
System.out.println(" Provider = "+md.getProvider());
System.out.println(" toString = "+md.toString());
String input = "";
md.update(input.getBytes());
byte[] output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") =");
System.out.println(" "+bytesToHex(output));
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") =");
System.out.println(" "+bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") =");
System.out.println(" "+bytesToHex(output));
} catch (Exception e) {
System.out.println("Exception: "+e);
}
}
public 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 (int j=0; j<b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]);
}
return buf.toString();
}
}
OUTPUT:-
SHA1("") =
DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
SHA1("abc") =
A9993E364706816ABA3E25717850C26C9CD0D89D
SHA1("abcdefghijklmnopqrstuvwxyz") =
32D10C7B8CF96570CA04CE37F2A19D84240D3A89