Created
December 30, 2011 12:45
-
-
Save netoht/1539691 to your computer and use it in GitHub Desktop.
TripleDES com chaves curtas
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.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
/** | |
* http://tripoverit.blogspot.com/2009/06/tripledes-encryption-compatibility.html | |
* http://nicholasbarger.com/2008/10/21/triple-des-encryption-class/ | |
* @author Waldinar Oliveira Neto | |
* | |
*/ | |
public class TripleDES { | |
private String key; | |
private String vector; | |
public TripleDES(String key, String vector) { | |
this.key = key(key); | |
this.vector = vector(vector); | |
} | |
public String encryptText(String plainText) throws Exception { | |
// ---- Use specified 3DES key and IV from other source -------------- | |
byte[] plaintext = plainText.getBytes(); | |
byte[] tdesKeyData = key.getBytes(); | |
// byte[] myIV = initializationVector.getBytes(); | |
Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding"); | |
SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DESede"); | |
IvParameterSpec ivspec = new IvParameterSpec(vector.getBytes()); | |
c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec); | |
byte[] cipherText = c3des.doFinal(plaintext); | |
return new sun.misc.BASE64Encoder().encode(cipherText); | |
} | |
public String decryptText(String cipherText) throws Exception { | |
byte[] encData = new sun.misc.BASE64Decoder().decodeBuffer(cipherText); | |
Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); | |
byte[] tdesKeyData = key.getBytes(); | |
SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DESede"); | |
IvParameterSpec ivspec = new IvParameterSpec(vector.getBytes()); | |
decipher.init(Cipher.DECRYPT_MODE, myKey, ivspec); | |
byte[] plainText = decipher.doFinal(encData); | |
return new String(plainText); | |
} | |
public static void main(String[] args) throws Exception { | |
String key = "A2SGcb2%dfg00145"; | |
String vector = "@x1=001A2v2"; | |
TripleDES tripleDES = new TripleDES(key, vector); | |
String encrypt = tripleDES.encryptText("NETO"); | |
System.out.println(encrypt); | |
String decrypt = tripleDES.decryptText(encrypt); | |
System.out.println(decrypt); | |
} | |
public static String key(String keyStr) { | |
if (keyStr == null) { | |
return null; | |
} | |
byte[] key = keyStr.getBytes(); | |
byte[] keyValue = new byte[24]; | |
if (key.length == 16) { | |
// Create the third key from the first 8 bytes | |
System.arraycopy(key, 0, keyValue, 0, 16); | |
System.arraycopy(key, 0, keyValue, 16, 8); | |
} else if (key.length != 24) { | |
throw new IllegalArgumentException("A TripleDES key should be 24 bytes long"); | |
} else { | |
keyValue = key; | |
} | |
return new String(keyValue); | |
} | |
public static String vector(String vectorStr) { | |
if (vectorStr == null) { | |
return null; | |
} | |
byte[] vector = vectorStr.getBytes(); | |
byte[] vectorValue = new byte[8]; | |
if (vector.length > 8) { | |
System.arraycopy(vector, 0, vectorValue, 0, 8); | |
} else if (vector.length != 8) { | |
throw new IllegalArgumentException("A TripleDES vector should be 8 bytes long"); | |
} else { | |
vectorValue = vector; | |
} | |
return new String(vectorValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment