java.lang.NoClassDefFoundError: org/bouncycastle/crypto/generators/SCrypt


While encoding your password/data with SCryptPasswordEncoder, you might be getting the below exception:

java.lang.NoClassDefFoundError: org/bouncycastle/crypto/generators/SCrypt
	at org.springframework.security.crypto.scrypt.SCryptPasswordEncoder.digest(SCryptPasswordEncoder.java:195) ~[spring-security-crypto-6.0.3.jar:6.0.3]
	at org.springframework.security.crypto.scrypt.SCryptPasswordEncoder.encode(SCryptPasswordEncoder.java:150) ~[spring-security-crypto-6.0.3.jar:6.0.3]
	at org.websparrow.SpringSecurityMavenApplication.run(SpringSecurityMavenApplication.java:27) ~[classes/:na]
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:769) ~[spring-boot-3.0.6.jar:3.0.6]
	at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:753) ~[spring-boot-3.0.6.jar:3.0.6]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) ~[spring-boot-3.0.6.jar:3.0.6]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.0.6.jar:3.0.6]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.0.6.jar:3.0.6]
	at org.websparrow.SpringSecurityMavenApplication.main(SpringSecurityMavenApplication.java:13) ~[classes/:na]
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.crypto.generators.SCrypt
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
	... 9 common frames omitted

This is all happening because encoding technique used by SCryptPasswordEncoder is based on the scrypt key derivation function.

Solution

By adding the bellow dependency in your project build file will solve this issue.

Maven:

<dependencies>
	...
	<dependency>
		<groupId>org.bouncycastle</groupId>
		<artifactId>bcprov-jdk15on</artifactId>
		<version>1.70</version>
	</dependency>
	...
</dependencies>

Gradle:

implementation 'org.bouncycastle:bcprov-jdk15on:1.70'

References

  1. Types of Password Encoders in Spring Security
  2. Securing Passwords with Spring Security Password Encoder
  3. Bouncy Castle Provider- MVN Repository

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.