Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Translate this code to Java programming language (The code is in C#, implementin

ID: 3670423 • Letter: T

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 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();
}
}
}

Explanation / Answer

Solution:

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.Cipher;
// class to perfrom DES encryption and decrption
public class DESProgram
{
private static byte[] iv ={ 0x0a, 0x01, 0x02, 0x03, 0x04, 0x0b, 0x0c, 0x0d };

// method for encryption
private static byte[] EncryptMethod(byte[] message, SecretKey key, String password) throws Exception
{
    Cipher cpr = Cipher.getInstance(password);
    IvParameterSpec inparspc = new IvParameterSpec(iv);
    cpr.init(Cipher.ENCRYPT_MODE, key, inparspc);
    return cpr.doFinal(message);
}

// method for decryption
private static byte[] Decrypt(byte[] encryptedMessage, SecretKey key, String password) throws Exception
{
    Cipher cipher = Cipher.getInstance(password);
    IvParameterSpec inparspc = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, inparspc);
    return cipher.doFinal(encryptedMessage);
}

// main method
public static void main(String[] unused) throws Exception {
    String pass = "password";
    // Generate a secret key
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56);
    SecretKey key = kg.generateKey();

    String unencryptString ="AABBCCDD11223344";

    byte[] encryptString ;
    byte[] decryptString ;

   System.out.println("Entered String is: " + unencryptString);
   System.out.println("Password is: " + pass);

   encryptString = EncryptMethod(unencryptString.getBytes(), key, pass);
   System.out.println("Encrypted String is: " + encryptString);

   decryptString = Decrypt(encryptString, key, pass);
   System.out.println("Decrypted String is: " + decryptString);

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote