-
-
Save sywalters/623ed7e9b1316f53c912 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 java.security.MessageDigest; | |
import java.util.Arrays; | |
import javax.crypto.Cipher; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.SecretKeySpec; | |
import org.apache.commons.codec.binary.Base64; | |
/** | |
* Simple TripleDES Encrypt/Decrypt Test | |
* sha1, utf-8, no padding | |
* | |
* uses commons-codec-1.6 | |
* javac -cp :commons-codec-1.6.jar TripleDESTest.java | |
* java -cp :commons-codec-1.6.jar TripleDESTest | |
*/ | |
public class TripleDESTest { | |
public static void main(String[] args) throws Exception { | |
String text = "textToEncrypt"; | |
String codedtext = new TripleDESTest()._encrypt(text,"SecretKey"); | |
String decodedtext = new TripleDESTest()._decrypt(codedtext,"SecretKey"); | |
System.out.println(codedtext + " ---> " + decodedtext); | |
} | |
private String _encrypt(String message, String secretKey) throws Exception { | |
MessageDigest md = MessageDigest.getInstance("SHA-1"); | |
byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); | |
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); | |
SecretKey key = new SecretKeySpec(keyBytes, "DESede"); | |
Cipher cipher = Cipher.getInstance("DESede"); | |
cipher.init(Cipher.ENCRYPT_MODE, key); | |
byte[] plainTextBytes = message.getBytes("utf-8"); | |
byte[] buf = cipher.doFinal(plainTextBytes); | |
byte [] base64Bytes = Base64.encodeBase64(buf); | |
String base64EncryptedString = new String(base64Bytes); | |
return base64EncryptedString; | |
} | |
private String _decrypt(String encryptedText, String secretKey) throws Exception { | |
byte[] message = Base64.decodeBase64(encryptedText.getBytes("utf-8")); | |
MessageDigest md = MessageDigest.getInstance("SHA-1"); | |
byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); | |
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); | |
SecretKey key = new SecretKeySpec(keyBytes, "DESede"); | |
Cipher decipher = Cipher.getInstance("DESede"); | |
decipher.init(Cipher.DECRYPT_MODE, key); | |
byte[] plainText = decipher.doFinal(message); | |
return new String(plainText, "UTF-8"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment