Created
August 10, 2021 19:19
-
-
Save Anass-ABEA/e3bba80fdaf197f0ede8885985fc6716 to your computer and use it in GitHub Desktop.
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
import javax.crypto.Cipher; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.GCMParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.util.Base64; | |
/** | |
* Possible KEY_SIZE values are 128, 192 and 256 | |
* Possible T_LEN values are 128, 120, 112, 104 and 96 | |
*/ | |
public class AES { | |
private SecretKey key; | |
private int KEY_SIZE = 128; | |
private int T_LEN = 128; | |
private byte[] IV; | |
public void init() throws Exception { | |
KeyGenerator generator = KeyGenerator.getInstance("AES"); | |
generator.init(KEY_SIZE); | |
key = generator.generateKey(); | |
} | |
public void initFromStrings(String secretKey, String IV){ | |
key = new SecretKeySpec(decode(secretKey),"AES"); | |
this.IV = decode(IV); | |
} | |
public String encryptOld(String message) throws Exception { | |
byte[] messageInBytes = message.getBytes(); | |
Cipher encryptionCipher = Cipher.getInstance("AES/GCM/NoPadding"); | |
encryptionCipher.init(Cipher.ENCRYPT_MODE, key); | |
IV = encryptionCipher.getIV(); | |
byte[] encryptedBytes = encryptionCipher.doFinal(messageInBytes); | |
return encode(encryptedBytes); | |
} | |
public String encrypt(String message) throws Exception { | |
byte[] messageInBytes = message.getBytes(); | |
Cipher encryptionCipher = Cipher.getInstance("AES/GCM/NoPadding"); | |
GCMParameterSpec spec = new GCMParameterSpec(T_LEN,IV); | |
encryptionCipher.init(Cipher.ENCRYPT_MODE, key,spec); | |
byte[] encryptedBytes = encryptionCipher.doFinal(messageInBytes); | |
return encode(encryptedBytes); | |
} | |
public String decrypt(String encryptedMessage) throws Exception { | |
byte[] messageInBytes = decode(encryptedMessage); | |
Cipher decryptionCipher = Cipher.getInstance("AES/GCM/NoPadding"); | |
GCMParameterSpec spec = new GCMParameterSpec(T_LEN, IV); | |
decryptionCipher.init(Cipher.DECRYPT_MODE, key, spec); | |
byte[] decryptedBytes = decryptionCipher.doFinal(messageInBytes); | |
return new String(decryptedBytes); | |
} | |
private String encode(byte[] data) { | |
return Base64.getEncoder().encodeToString(data); | |
} | |
private byte[] decode(String data) { | |
return Base64.getDecoder().decode(data); | |
} | |
private void exportKeys(){ | |
System.err.println("SecretKey : "+encode(key.getEncoded())); | |
System.err.println("IV : "+encode(IV)); | |
} | |
public static void main(String[] args) { | |
try { | |
AES aes = new AES(); | |
aes.initFromStrings("CHuO1Fjd8YgJqTyapibFBQ==","e3IYYJC2hxe24/EO"); | |
String encryptedMessage = aes.encrypt("TheXCoders_2"); | |
//String decryptedMessage = aes.decrypt(encryptedMessage); | |
System.err.println("Encrypted Message : " + encryptedMessage); | |
//System.err.println("Decrypted Message : " + decryptedMessage); | |
//aes.exportKeys(); | |
} catch (Exception ignored) { | |
} | |
} | |
} |
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
import javax.crypto.Cipher; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.GCMParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.util.Base64; | |
public class Client { | |
private SecretKey key; | |
private int KEY_SIZE = 128; | |
private int T_LEN = 128; | |
private byte[] IV; | |
public void initFromStrings(String secretKey, String IV){ | |
key = new SecretKeySpec(decode(secretKey),"AES"); | |
this.IV = decode(IV); | |
} | |
public String decrypt(String encryptedMessage) throws Exception { | |
byte[] messageInBytes = decode(encryptedMessage); | |
Cipher decryptionCipher = Cipher.getInstance("AES/GCM/NoPadding"); | |
GCMParameterSpec spec = new GCMParameterSpec(T_LEN, IV); | |
decryptionCipher.init(Cipher.DECRYPT_MODE, key, spec); | |
byte[] decryptedBytes = decryptionCipher.doFinal(messageInBytes); | |
return new String(decryptedBytes); | |
} | |
private String encode(byte[] data) { | |
return Base64.getEncoder().encodeToString(data); | |
} | |
private byte[] decode(String data) { | |
return Base64.getDecoder().decode(data); | |
} | |
public static void main(String[] args) { | |
try{ | |
Client client = new Client(); | |
client.initFromStrings("CHuO1Fjd8YgJqTyapibFBQ==","e3IYYJC2hxe24/EO"); | |
String decryptedMessage = client.decrypt("mqQQF6K2GEaR0JKTd1yN58Mbs7qeYamM0xgung=="); | |
System.err.println(decryptedMessage); | |
}catch (Exception ignored){} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment