Jump to content

Java No Such Algorithm Exception

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 9 9950X3D | CPU Cooler: Gigabyte Aorus Waterforce X II 360mm |RAM: 2x32GB G.Skill Trident Z5 Neo RGB 6000MHz | Mobo: Gigabyte X870E Aorus Xtreme X3D AI Top  | Storage: Samsung 9100 Pro 4TB + Samsung 990 Pro 4TB + Samsung 870 Evo 4TB + Samsung 870 Evo 2TB | Graphics Card: Gigabyte RTX 5090 Aorus Master 32G | Case: Lian Li Lancool 216 | PSU: Seasonic Vertex GX-1200 |

 

Link to comment
https://linustechtips.com/topic/507190-java-no-such-algorithm-exception/
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 9 9950X3D | CPU Cooler: Gigabyte Aorus Waterforce X II 360mm |RAM: 2x32GB G.Skill Trident Z5 Neo RGB 6000MHz | Mobo: Gigabyte X870E Aorus Xtreme X3D AI Top  | Storage: Samsung 9100 Pro 4TB + Samsung 990 Pro 4TB + Samsung 870 Evo 4TB + Samsung 870 Evo 2TB | Graphics Card: Gigabyte RTX 5090 Aorus Master 32G | Case: Lian Li Lancool 216 | PSU: Seasonic Vertex GX-1200 |

 

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

×