AIM:
Towrite a program
toimplement
DES encryption and decryption in java.
ALGORITHM:
Step 1:Enter the plain text and key
Step 2: Initial permutation (IP) will be done for the plain text and key.
Step 3: 16 rounds of a complex key dependent calculation f
Function f is
L(i) = R(i-1)
R(i) = L(i-1) Å P(S( E(R(i-1)) Å K(i) ))
Step 4: A final permutation, being the inverse of IP will be done to get cipher
text.
Step 5: The above steps 4, 3, 2 are repeated in reverse order to get
decrypted text.
PROGRAM:
import
java.util.*;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.security.spec.KeySpec;
importjavax.crypto.Cipher;
importjavax.crypto.SecretKey;
importjavax.crypto.SecretKeyFactory;
importjavax.crypto.spec.DESedeKeySpec;
import
sun.misc.BASE64Decoder;
import
sun.misc.BASE64Encoder;
public
class DES {
private
static final String UNICODE_FORMAT = "UTF8";
public
static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
private
KeySpecmyKeySpec;
private
SecretKeyFactorymySecretKeyFactory;
private
Cipher cipher;
byte[]
keyAsBytes;
private
String myEncryptionKey;
private
String myEncryptionScheme;
SecretKey
key;
staticBufferedReaderbr
= new BufferedReader(new InputStreamReader(System.in));
public
DES() throws Exception {
myEncryptionKey
= "ThisIsSecretEncryptionKey";
myEncryptionScheme
= DESEDE_ENCRYPTION_SCHEME;
keyAsBytes
= myEncryptionKey.getBytes(UNICODE_FORMAT);
myKeySpec
= new DESedeKeySpec(keyAsBytes);
mySecretKeyFactory
= SecretKeyFactory.getInstance(myEncryptionScheme);
cipher
= Cipher.getInstance(myEncryptionScheme);
key
= mySecretKeyFactory.generateSecret(myKeySpec);
}
public
String encrypt(String unencryptedString) {
String encryptedString = null;
try
{
cipher.init(Cipher.ENCRYPT_MODE,
key);
byte[]
plainText = unencryptedString.getBytes(UNICODE_FORMAT);
byte[]
encryptedText = cipher.doFinal(plainText);
BASE64Encoder base64encoder = new
BASE64Encoder();
encryptedString
= base64encoder.encode(encryptedText); }
catch
(Exception e) {
e.printStackTrace();
}
returnencryptedString;
}
public
String decrypt(String encryptedString) {
String decryptedText=null;
try
{
cipher.init(Cipher.DECRYPT_MODE,
key);
BASE64Decoder base64decoder = new
BASE64Decoder();
byte[]
encryptedText = base64decoder.decodeBuffer(encryptedString);
byte[]
plainText = cipher.doFinal(encryptedText);
decryptedText=
bytes2String(plainText); }
catch
(Exception e) {
e.printStackTrace();
}
returndecryptedText;
}
private
static String bytes2String(byte[] bytes) {
StringBufferstringBuffer
= new StringBuffer();
for
(int i = 0; i <bytes.length; i++) {
stringBuffer.append((char)
bytes[i]); }
returnstringBuffer.toString();
}
public
static void main(String args []) throws Exception {
System.out.print("Enter
the string: ");
DES myEncryptor= new DES();
String
stringToEncrypt = br.readLine();
String encrypted =
myEncryptor.encrypt(stringToEncrypt);
String
decrypted = myEncryptor.decrypt(encrypted);
System.out.println("\nString
To Encrypt: " +stringToEncrypt);
System.out.println("\nEncrypted
Value : " +encrypted);
System.out.println("\nDecrypted
Value : " +decrypted);
System.out.println("");
}
}
Comments
Post a Comment