Skip to content

Instantly share code, notes, and snippets.

@joeolaoye
Last active June 6, 2022 08:41
Show Gist options
  • Save joeolaoye/6afc88a474aa086036c65b15abcf0947 to your computer and use it in GitHub Desktop.
Save joeolaoye/6afc88a474aa086036c65b15abcf0947 to your computer and use it in GitHub Desktop.
A class to encrypt and decrypt using 3DES in java
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");
}
}
@only2dhir
Copy link

Thanks for the code. It works like a charm. Used this code to create a triple DES online tool.

@NabilAbrhim
Copy link

hi there,

Is it possible to add Main function to this to be used in cmd like (java TripleDES.java) which will take a key and the text from the user to encrypt it and decrypt it?

I was trying build this in python but I could not

Thank you

@joeolaoye
Copy link
Author

hi there,

Is it possible to add Main function to this to be used in cmd like (java TripleDES.java) which will take a key and the text from the user to encrypt it and decrypt it?

I was trying build this in python but I could not

Thank you

Yeah, you can add a main() function.

@oyorjuelar
Copy link

Thank you, it is working very fine.

@tsukiazuma
Copy link

I can't import org.apache.commons.codec.binary.Base64;
package org.apache.commons.codec.binary does not exist

@joeolaoye
Copy link
Author

I can't import org.apache.commons.codec.binary.Base64;
package org.apache.commons.codec.binary does not exist

https://commons.apache.org/proper/commons-codec/

@ochonma
Copy link

ochonma commented Jun 5, 2022

What if the the key is in HEX format, will the byte[] digestOfPassword = md.digest(key.getBytes("utf-8")); still work ?

@joeolaoye
Copy link
Author

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