DATAENCYPTION STANDARD ALGORITHM
AIM
To write a program to implement the Data Encryption Standard algorithm.
ALGORITHM
Start the program.
Encryption and Decryption process is carried out.
ENCRYPTION
The plain text length is 64 bit and key length is 56 bit.
The encryption process is made of two permutations(P-boxes),which we
call initial and final permutations, and sixteen Feistel rounds.
Each round in DES divides the 64 bit plain text into two 32 bit.
Sinceplaintextisnow32-bitinputandkeyisa48-
bitkey,firstexpansionshouldbedone.
Afterexpansion,XORoperationisdonewiththe48bitkeyandsenttothes-boxto
obtain the32-bit again.
The S-boxes d other real mixing(confusion).DESuses8S-
boxes,eachwitha6-bitinput and a 4-bit output.
Then it is sent to the straight p-box to obtain the32-bit output.
This process is carried out for the 16 rounds.
The cipher text can be obtained after the final permutation.
DECRYPTION
The process that are carried out in the encryption process are carried out
in reverse order.
After16 rounds of decryption the plain text can be obtained.
CODING
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
public classDES {
private Ciphercipher = null;
privateDESKeySpec keySpec=null;
privateSecretKeyFactorykeyFactory=null;
publicStringencrypt(StringinputString,StringcommonKey)
throws Exception {
StringencryptedValue= "";
SecretKeykey=getSecretKey(commonKey);
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[]inputBytes= inputString.getBytes();
byte[]outputBytes= cipher.doFinal(inputBytes);
encryptedValue =newHexBinaryAdapter().marshal(outputBytes);
return encryptedValue;
}
Public String decrypt(Stringencrypted String,Stringcommon Key)
throws Exception {
Stringdecrypted Value= "";
encryptedString=encryptedString.replace(' ','+');
SecretKeykey=getSecretKey(commonKey);
cipher.init(Cipher.DECRYPT_MODE,key);
byte[]recoveredBytes=null;
try{recoveredBytes =cipher.doFinal
(newHexBinaryAdapter().unmarshal(encryptedString));
} catch(Exceptione)
{e.printStackTrace();
return null;
}
decryptedValue=newString(recoveredByte);
returndecryptedValue;
}
privateSecretKeygetSecretKey(Stringsecret Passrd)
{
SecretKeykey=null;
try{
cipher= Cipher.getInstance("DES");
keySpec =new DESKeySpec(secretPassword.getBytes("UTF8"));
keyFactory= SecretKeyFactory.getInstance("DES");
key= keyFactory.generateSecret(keySpec);
} catch(Exceptione) {e.printStackTrace();
System.out.println("Error ingeneratingthe secretKey");
}
returnkey;
}
publicstatic void main(String[]args) {BufferedReaderreader;
reader =newBufferedReader
(newInputStreamReader(System.in));
DESdes = new DES();
try{
System.out.println("ENCRYPTION--------------------------------");
System.out.print("EnterPlain Message:");
Stringinput = reader.readLine();
System.out.print("EnterKey:");
Stringkey=reader.readLine();
System.out.println();
System.out.print("EncryptedMessage: ");
Stringencrypted =des.encrypt(input,key);
System.out.println(encrypted);
System.out.println(“DECRYPTION”);
System.out.print("EnterEncryptedMessage:");
encrypted=reader.readLine();
System.out.print("EnterKey:");
key= reader.readLine();
System.out.println();
System.out.print("DecryptedMessage: ");
Stringdecrypted =des.decrypt(encrypted, key);
System.out.println(decrypted);
System.out.println();
} catch(Exceptione) {
e.printStackTrace();
}}}
OUTPUT
RESULT
Thus the DES algorithm is implemented using JAVA language and the output is
verified successfully.