Dataencyption Standard Algorithm
Dataencyption Standard Algorithm
AIM
ALGORITHM
ENCRYPTION
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.