-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA3.java
More file actions
35 lines (29 loc) · 1.15 KB
/
Copy pathA3.java
File metadata and controls
35 lines (29 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
public class A3{
public static String Algorithm(String K, String RAND) {
try {
String combinedInput = K + RAND;
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(combinedInput.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : hash) {
sb.append(String.format("%02x", b));
}
return sb.substring(0, 8);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter secret key (K): ");
String K = scanner.nextLine();
System.out.print("Enter random challenge (RAND): ");
String RAND = scanner.nextLine();
String SRES = a3Algorithm(K, RAND);
System.out.println("Authentication Response (SRES): " + SRES);
}
}