Question: Translate this code to Java programming language (The code is in C#, implementing the DES algorithm) using System; using System.Collections.Generic; using System.Linq; using System.Text; using

Translate this code to Java programming language

(The code is in C#, implementing the DES algorithm)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO;

namespace DES { public class DESProgram {

public static string EncryptMethod(string message, string password) { // Encode message and password byte[] msgBytes = ASCIIEncoding.ASCII.GetBytes(message); byte[] pswdBytes = ASCIIEncoding.ASCII.GetBytes(password);

DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); ICryptoTransform transform = descsp.CreateEncryptor(pswdBytes, pswdBytes); CryptoStreamMode mode = CryptoStreamMode.Write;

MemoryStream mStream = new MemoryStream(); CryptoStream crypStream = new CryptoStream(mStream, transform, mode); crypStream.Write(msgBytes, 0, msgBytes.Length); crypStream.FlushFinalBlock();

byte[] encryptedMsgBytes = new byte[mStream.Length]; mStream.Position = 0; mStream.Read(encryptedMsgBytes, 0, encryptedMsgBytes.Length);

string encryptedMsg = Convert.ToBase64String(encryptedMsgBytes);

return encryptedMsg; }

public static string Decrypt(string encryptedMessage, string password) { // Convert encrypted message and password to bytes byte[] encryptedMsgBytes = Convert.FromBase64String(encryptedMessage); byte[] pswdBytes = ASCIIEncoding.ASCII.GetBytes(password);

DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); ICryptoTransform trans = descsp.CreateDecryptor(pswdBytes, pswdBytes); CryptoStreamMode mode = CryptoStreamMode.Write;

MemoryStream mStream = new MemoryStream(); CryptoStream crypStream = new CryptoStream(mStream, trans, mode); crypStream.Write(encryptedMsgBytes, 0, encryptedMsgBytes.Length); crypStream.FlushFinalBlock();

byte[] decryptedMsgBytes = new byte[mStream.Length]; mStream.Position = 0; mStream.Read(decryptedMsgBytes, 0, decryptedMsgBytes.Length);

string msg = ASCIIEncoding.ASCII.GetString(decryptedMsgBytes);

return msg; }

static void Main(string[] args) { //Enter string to be encrypted string unencryptString = "AABBCCDD11223344"; string encryptString; string decryptString;

//Enter a 8 byte password string pass = "password";

System.Console.WriteLine("Entered String is: " + unencryptString); System.Console.WriteLine("Password is: " + pass);

encryptString = DESProgram.EncryptMethod(unencryptString, pass); System.Console.WriteLine("Encrypted String is: " + encryptString);

decryptString = DESProgram.Decrypt(encryptString, pass); System.Console.WriteLine("Decrypted String is: " + decryptString);

System.Console.ReadLine(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!