Last active
June 6, 2022 08:41
-
-
Save joeolaoye/6afc88a474aa086036c65b15abcf0947 to your computer and use it in GitHub Desktop.
A class to encrypt and decrypt using 3DES in java
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
import java.io.UnsupportedEncodingException; | |
import java.security.InvalidKeyException; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Arrays; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.SecretKeySpec; | |
import org.apache.commons.codec.binary.Base64; | |
/** | |
* | |
* @author josepholaoye | |
*/ | |
public class TripleDES { | |
String key; | |
public TripleDES(String myEncryptionKey) { | |
key = myEncryptionKey; | |
} | |
/** | |
* Method To Encrypt The String | |
* | |
* @param unencryptedString | |
* @return encrpted string | |
* @throws java.security.NoSuchAlgorithmException | |
* @throws java.io.UnsupportedEncodingException | |
* @throws javax.crypto.NoSuchPaddingException | |
* @throws java.security.InvalidKeyException | |
* @throws javax.crypto.IllegalBlockSizeException | |
* @throws javax.crypto.BadPaddingException | |
*/ | |
public String harden(String unencryptedString) throws NoSuchAlgorithmException, UnsupportedEncodingException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { | |
MessageDigest md = MessageDigest.getInstance("md5"); | |
byte[] digestOfPassword = md.digest(key.getBytes("utf-8")); | |
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); | |
for (int j = 0, k = 16; j < 8;) { | |
keyBytes[k++] = keyBytes[j++]; | |
} | |
SecretKey secretKey = new SecretKeySpec(keyBytes, "DESede"); | |
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); | |
cipher.init(Cipher.ENCRYPT_MODE, secretKey); | |
byte[] plainTextBytes = unencryptedString.getBytes("utf-8"); | |
byte[] buf = cipher.doFinal(plainTextBytes); | |
byte[] base64Bytes = Base64.encodeBase64(buf); | |
String base64EncryptedString = new String(base64Bytes); | |
return base64EncryptedString; | |
} | |
/** | |
* Method To Decrypt An Ecrypted String | |
* | |
* @param encryptedString | |
* @return | |
* @throws java.io.UnsupportedEncodingException | |
* @throws java.security.NoSuchAlgorithmException | |
* @throws javax.crypto.NoSuchPaddingException | |
* @throws java.security.InvalidKeyException | |
* @throws javax.crypto.IllegalBlockSizeException | |
* @throws javax.crypto.BadPaddingException | |
*/ | |
public String soften(String encryptedString) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { | |
if(encryptedString == null) | |
{ | |
return ""; | |
} | |
byte[] message = Base64.decodeBase64(encryptedString.getBytes("utf-8")); | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
byte[] digestOfPassword = md.digest(key.getBytes("utf-8")); | |
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); | |
for (int j = 0, k = 16; j < 8;) { | |
keyBytes[k++] = keyBytes[j++]; | |
} | |
SecretKey secretKey = new SecretKeySpec(keyBytes, "DESede"); | |
Cipher decipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); | |
decipher.init(Cipher.DECRYPT_MODE, secretKey); | |
byte[] plainText = decipher.doFinal(message); | |
return new String(plainText, "UTF-8"); | |
} | |
} |
What if the the key is in HEX format, will the byte[] digestOfPassword = md.digest(key.getBytes("utf-8")); still work ?
What if the the key is in HEX format, will the byte[] digestOfPassword = md.digest(key.getBytes("utf-8")); still work ?
Hopefully, as long as it is in the utf-8 encoding, it should work.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://commons.apache.org/proper/commons-codec/