Created
September 6, 2023 14:42
-
-
Save ensargunesdogdu/8b95bb2849df51d4a69a3c214e659441 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
public class SampleEncryption { | |
private final String securityKey = "YOUR_PRIVATE_SECURITY_KEY_HERE"; | |
public String Decrypt(String cipherString) { | |
try { | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
final byte[] digestOfPassword = md.digest(securityKey.getBytes("utf-8")); | |
final byte[] keyArray = Arrays.copyOf(digestOfPassword, 24); | |
for (int j = 0, k = 16; j < 8; ) { | |
keyArray[k++] = keyArray[j++]; | |
} | |
SecretKeySpec key = new SecretKeySpec(keyArray, "DESede"); | |
Cipher c = Cipher.getInstance("DESede/ECB/PKCS5Padding"); | |
c.init(Cipher.DECRYPT_MODE, key); | |
byte[] toDecryptArray = Base64.getDecoder().decode(cipherString); | |
byte[] decryptedValue = c.doFinal(toDecryptArray); | |
return new String(decryptedValue); | |
} catch (Exception e) { | |
log.error("Decryption failed due to ", e); | |
return null; | |
} | |
} | |
public String Encrypt(String cipherString) { | |
try { | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
final byte[] digestOfPassword = md.digest(securityKey.getBytes("utf-8")); | |
final byte[] keyArray = Arrays.copyOf(digestOfPassword, 24); | |
for (int j = 0, k = 16; j < 8; ) { | |
keyArray[k++] = keyArray[j++]; | |
} | |
SecretKeySpec key = new SecretKeySpec(keyArray, "DESede"); | |
Cipher c = Cipher.getInstance("DESede/ECB/PKCS5Padding"); | |
c.init(Cipher.ENCRYPT_MODE, key); | |
byte[] toEncryptArray = cipherString.getBytes("utf-8"); | |
byte[] encryptedText = c.doFinal(toEncryptArray); | |
return new String(Base64.getEncoder().encode(encryptedText)); | |
} catch (Exception e) { | |
log.error("Encryption failed due to ", e); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment