Created
July 10, 2021 13:39
-
-
Save kriss-u/ab1705dc3ccc55c89f6ff6f209f0bc7d to your computer and use it in GitHub Desktop.
Adroit decrypt code
This file contains hidden or 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
// Online Java Compiler | |
// Use this editor to write, compile and run your Java code online | |
import java.io.UnsupportedEncodingException; | |
import java.security.InvalidKeyException; | |
import java.security.Key; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Base64; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.spec.SecretKeySpec; | |
public class Main { | |
private String secret; | |
public String getSecret() { return this.secret; } | |
public void setSecret(String secret) { this.secret = secret; } | |
public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { | |
System.out.println(Main.decrypt("Sup3rS3cur3Dr0it", "l4A+n+p+xSxDcYCl0mgxKrO15+OEC3aOfdrWafSqwpY=")); | |
} | |
public static String encrypt(String key, String text) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { | |
Key aesKey = new SecretKeySpec(key.getBytes(), "AES"); | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(1, aesKey); | |
byte[] encrypted = cipher.doFinal(text.getBytes()); | |
return Base64.getEncoder().encodeToString(encrypted); | |
} | |
public static String decrypt(String key, String text) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { | |
try { | |
Key aesKey = new SecretKeySpec(key.getBytes(), "AES"); | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(2, aesKey); | |
return new String(cipher.doFinal(Base64.getDecoder().decode(text))); | |
} catch (InvalidKeyException i) { | |
System.out.println("[x] Invalid key length {16 required}"); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment