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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started