Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

pls tracing output for this programs and comment how get output ------------------------------------------------------------------ Program1: package javaapplication4; /*package com.mkyong.test; import java.security.MessageDigest; /** * * @author Dr.Hossam Diab

pls tracing output for this programs and comment how get output

------------------------------------------------------------------

Program1:

package javaapplication4;

/*package com.mkyong.test;

import java.security.MessageDigest;

/**

*

* @author Dr.Hossam Diab

*/

import java.security.MessageDigest;

public class JavaApplication4 {

public static void main(String[] args)throws Exception

{

String password = "1234*5689";

MessageDigest md = MessageDigest.getInstance("SHA-256");

md.update(password.getBytes());

byte byteData[] = md.digest();

//convert the byte to hex format method 1

StringBuffer sb = new StringBuffer();

for (int i = 0; i

sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));

}

System.out.println("Hex format : " + sb.toString());

//convert the byte to hex format method 2

StringBuffer hexString = new StringBuffer();

for (int i=0;i

String hex=Integer.toHexString(0xff & byteData[i]);

if(hex.length()==1) hexString.append('0');

hexString.append(hex);

}

System.out.println("Hex format : " + hexString.toString());

}

}

image text in transcribed

Program2:

package javaapplication5;

import java.io.FileInputStream;

import java.security.MessageDigest;

/**

*

* @author Dr.Hossam Diab

*/

public class JavaApplication5 {

public static void main(String[] args)throws Exception

{

MessageDigest md = MessageDigest.getInstance("SHA-256");

FileInputStream fis = new FileInputStream("d:\\loging.txt");

byte[] dataBytes = new byte[1024];

int nread = 0;

while ((nread = fis.read(dataBytes)) != -1) {

md.update(dataBytes, 0, nread);

};

byte[] mdbytes = md.digest();

//convert the byte to hex format method 1

StringBuffer sb = new StringBuffer();

for (int i = 0; i

sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));

}

System.out.println("Hex format : " + sb.toString());

//convert the byte to hex format method 2

StringBuffer hexString = new StringBuffer();

for (int i=0;i

hexString.append(Integer.toHexString(0xFF & mdbytes[i]));

}

System.out.println("Hex format : " + hexString.toString());

}

}

image text in transcribed

Program3:

package javaapplication6;

import java.io.FileInputStream;

import java.security.MessageDigest;

/**

*

* @author Dr.Hossam Diab

*/

public class JavaApplication6 {

public static void main(String[] args)throws Exception

{

MessageDigest md = MessageDigest.getInstance("MD5");

FileInputStream fis = new FileInputStream("d:\\loging.txt");

byte[] dataBytes = new byte[1024];

int nread = 0;

while ((nread = fis.read(dataBytes)) != -1) {

md.update(dataBytes, 0, nread);

};

byte[] mdbytes = md.digest();

//convert the byte to hex format method 1

StringBuffer sb = new StringBuffer();

for (int i = 0; i

sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));

}

System.out.println("Hex format : " + sb.toString());

//convert the byte to hex format method 2

StringBuffer hexString = new StringBuffer();

for (int i=0;i

hexString.append(Integer.toHexString(0xFF & mdbytes[i]));

}

System.out.println("Hex format : " + hexString.toString());

}

}

image text in transcribed

Program4:

package javaapplication7;

/*package com.mkyong.test;

import java.security.MessageDigest;

/**

*

* @author Dr.Hossam Diab

*/

import java.security.MessageDigest;

public class JavaApplication7 {

public static void main(String[] args)throws Exception

{

String password = "1234*5689asdr000";

MessageDigest md = MessageDigest.getInstance("MD5");

md.update(password.getBytes());

byte byteData[] = md.digest();

//convert the byte to hex format method 1

StringBuffer sb = new StringBuffer();

for (int i = 0; i

sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));

}

System.out.println("Hex format : " + sb.toString());

//convert the byte to hex format method 2

StringBuffer hexString = new StringBuffer();

for (int i=0;i

String hex=Integer.toHexString(0xff & byteData[i]);

if(hex.length()==1) hexString.append('0');

hexString.append(hex);

}

System.out.println("Hex format : " + hexString.toString());

}

}

image text in transcribed

Program5:

package javaapplication8;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

public class JavaApplication8 {

public static void main(String[] args) throws NoSuchAlgorithmException {

String passwordToHash = "password";

byte[] salt = getSalt();

String securePassword = get_SHA_1_SecurePassword(passwordToHash, salt);

System.out.println(securePassword);

}

private static String get_SHA_1_SecurePassword(String passwordToHash, byte[] salt)

{

String generatedPassword = null;

try {

MessageDigest md = MessageDigest.getInstance("SHA-1");

md.update(salt);

byte[] bytes = md.digest(passwordToHash.getBytes());

StringBuilder sb = new StringBuilder();

for(int i=0; i

{

sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));

}

generatedPassword = sb.toString();

}

catch (NoSuchAlgorithmException e)

{

e.printStackTrace();

}

return generatedPassword;

}

//Add salt

private static byte[] getSalt() throws NoSuchAlgorithmException

{

SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

byte[] salt = new byte[16];

sr.nextBytes(salt);

return salt;

}

}

image text in transcribed

Output JavaApplication2 (run) Hex format : 0ff05 Hex format: Off05a903ebecae619bab5ac3Ofe53941680882e735332b21e5765565b26a65c BUILD SUCCESSFUL (total time: 0 seconds) a903ebecae619bab5ac30fe53941680882e73 5332b21e5765565b26a65c Output JavaApplication1 (run) Hex format 2cf24dba5fb0a30e26e83b2ac5b9e29e1bl6 Hex format 2cf24dba5fb0a3e26e83b2ac5b9e29elb16 BUILD SUCCESSFUL (total time: 0 seconds) 1e5c c1fa7425e73043362938b9824 1e5c1fa7425e7343362938b9824 Output JavaApplication1 (run) Hex format 5d41402abc4b2a76b9719d911017c592 Hex Eormat BUILD SUCCESSFUL (total time: 0 seconds) 5d41402abc4b2a76b9719d911017c592 Output-JavaApplication1 (run) Hex format 768f151f011adac0213beac29cd34509 Hex format 768f151f011adac0213beac29cd34509 BUILD SUCCESSFUL (total time: 0 seconds) Output JavaApplication1 (run) 5c6da77025128416dE67b BUILD SUCCESSFUL (total time: 0 seconds) Output JavaApplication2 (run) Hex format : 0ff05 Hex format: Off05a903ebecae619bab5ac3Ofe53941680882e735332b21e5765565b26a65c BUILD SUCCESSFUL (total time: 0 seconds) a903ebecae619bab5ac30fe53941680882e73 5332b21e5765565b26a65c Output JavaApplication1 (run) Hex format 2cf24dba5fb0a30e26e83b2ac5b9e29e1bl6 Hex format 2cf24dba5fb0a3e26e83b2ac5b9e29elb16 BUILD SUCCESSFUL (total time: 0 seconds) 1e5c c1fa7425e73043362938b9824 1e5c1fa7425e7343362938b9824 Output JavaApplication1 (run) Hex format 5d41402abc4b2a76b9719d911017c592 Hex Eormat BUILD SUCCESSFUL (total time: 0 seconds) 5d41402abc4b2a76b9719d911017c592 Output-JavaApplication1 (run) Hex format 768f151f011adac0213beac29cd34509 Hex format 768f151f011adac0213beac29cd34509 BUILD SUCCESSFUL (total time: 0 seconds) Output JavaApplication1 (run) 5c6da77025128416dE67b BUILD SUCCESSFUL (total time: 0 seconds)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions