Jump to content

Java No Such Algorithm Exception

Geralt
Go to solution Solved by Geralt,

Disregard. Debugged it myself lol

KeyGenerator kg = KeyGenerator.getInstance("HmaccSHA1");

should be

KeyGenerator kg = KeyGenerator.getInstance("HmacSHA1");

Doh! Progamming after dark. >.>

Hey all, hit another snag in one of my cryptography programs. I keep getting the following output rather than the desired one. How do I go about fixing it?

Enter a string to encrypt: HelloWorldjava.security.NoSuchAlgorithmException: HmaccSHA1 KeyGenerator not available	at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:169)	at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223)	at cyapp.practical05.MyMAC.main(MyMAC.java:19)

Here's my code:

import java.security.NoSuchAlgorithmException;import java.util.Scanner;import javax.crypto.KeyGenerator;import javax.crypto.Mac;import javax.crypto.SecretKey;public class MyMAC {	public static void main(String[] args) throws NoSuchAlgorithmException{		try {			//get message			Scanner in = new Scanner(System.in);			System.out.print("Enter a string to encrypt: ");			byte[] message = in.nextLine().getBytes();						//generate key			KeyGenerator kg = KeyGenerator.getInstance("HmaccSHA1");			SecretKey key = kg.generateKey();						//get MAC object			Mac mac = Mac.getInstance("HmacSHA1");			mac.init(key);			mac.update(message);			System.out.println("Provider: " + mac.getProvider().getInfo());						//get HMAC			byte[] hmac = mac.doFinal();						//print results			System.out.println("");			System.out.println("MAC: HmacSHA1");			System.out.println("Message: " + new String(message));			System.out.println("HMAC: \"" + new String(hmac, "UTF8") + "\"");						in.close();		} catch (Exception e) {			e.printStackTrace();		}	}}

The stacktrace doesn't work too. Not sure why it didn't catch the exception and print the stacktrace. Any help is appreciated!

~~~SnapDragon~~~

| CPU: AMD Ryzen 7 3700X @ PBO & -0.06v offset | CPU Cooler: Scythe Ninja 5 |RAM: 32GB G.Skill Flare X 3200MHz @ 3600MHz 1.45V| Mobo: Gigabyte X570 Aorus Elite  | Storage: Crucial MX300 500GB + Western Digital Blue M.2 250GB + Seagate Barracuda 2TB + Western Digital 1TB Blue | Graphics Card: Asus ROG Strix RTX 2070 Super Advanced 8G | Case: Cooler Master HAF X | PSU: Superflower Leadex Silver 650W |

 

Link to comment
Share on other sites

Link to post
Share on other sites

Disregard. Debugged it myself lol

KeyGenerator kg = KeyGenerator.getInstance("HmaccSHA1");

should be

KeyGenerator kg = KeyGenerator.getInstance("HmacSHA1");

Doh! Progamming after dark. >.>

~~~SnapDragon~~~

| CPU: AMD Ryzen 7 3700X @ PBO & -0.06v offset | CPU Cooler: Scythe Ninja 5 |RAM: 32GB G.Skill Flare X 3200MHz @ 3600MHz 1.45V| Mobo: Gigabyte X570 Aorus Elite  | Storage: Crucial MX300 500GB + Western Digital Blue M.2 250GB + Seagate Barracuda 2TB + Western Digital 1TB Blue | Graphics Card: Asus ROG Strix RTX 2070 Super Advanced 8G | Case: Cooler Master HAF X | PSU: Superflower Leadex Silver 650W |

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×