-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Greg Zoller
committed
Nov 8, 2023
1 parent
62ddc82
commit e48b358
Showing
6 changed files
with
363 additions
and
119 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/main/java/co/blocke/scalajack/AESEncryptionDecryption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package co.blocke.scalajack; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.InvalidKeyException; | ||
import java.io.UnsupportedEncodingException; | ||
import javax.crypto.IllegalBlockSizeException; | ||
import java.util.Arrays; | ||
import java.util.Base64; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import javax.crypto.NoSuchPaddingException; | ||
import javax.crypto.BadPaddingException; | ||
|
||
/** | ||
* Java String Encryption Decryption Example | ||
* @author Ramesh Fadatare | ||
* | ||
*/ | ||
public class AESEncryptionDecryption { | ||
private static SecretKeySpec secretKey; | ||
private static byte[] key; | ||
private static final String ALGORITHM = "AES"; | ||
|
||
public void prepareSecreteKey(String myKey) throws NoSuchAlgorithmException { | ||
MessageDigest sha = null; | ||
key = myKey.getBytes(StandardCharsets.UTF_8); | ||
sha = MessageDigest.getInstance("SHA-1"); | ||
key = sha.digest(key); | ||
key = Arrays.copyOf(key, 16); | ||
secretKey = new SecretKeySpec(key, ALGORITHM); | ||
} | ||
|
||
public String encrypt(String strToEncrypt, String secret) | ||
throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException { | ||
prepareSecreteKey(secret); | ||
Cipher cipher = Cipher.getInstance(ALGORITHM); | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey); | ||
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))); | ||
} | ||
|
||
public String decrypt(String strToDecrypt, String secret) throws NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, InvalidKeyException, IllegalBlockSizeException { | ||
prepareSecreteKey(secret); | ||
Cipher cipher = Cipher.getInstance(ALGORITHM); | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey); | ||
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package co.blocke.scalajack; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface TypeHint{} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.