3DES
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
public class ThreeDES {
private static String str = "中国";
public static void main(String[] args) {
jdk3DES();
}
public static void jdk3DES() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
keyGenerator.init(new SecureRandom());
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
System.out.println("key : " + Base64.encodeBase64String(bytesKey));
DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(Base64.encodeBase64String(bytesKey).getBytes());
SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] result = cipher.doFinal(str.getBytes());
System.out.println("encrpt : " + Base64.encodeBase64String(result));
cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
result = cipher.doFinal(Base64.decodeBase64(Base64.encodeBase64String(result).getBytes()));
System.out.println("decrypt : " + new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Encrypt3DESUtil
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Base64;
public class Encrypt3DESUtil {
public static String generageKey() {
String key = "";
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
keyGenerator.init(new SecureRandom());
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
key = Base64.encodeBase64String(bytesKey);
System.out.println("key : " + key);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return key;
}
public static String encrypt(String str, String key) {
String returnStr = "";
try {
DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(Base64.decodeBase64(key));
SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] result = cipher.doFinal(str.getBytes());
System.out.println("encrpt : " + Base64.encodeBase64String(result));
returnStr = Base64.encodeBase64String(result);
} catch (Exception e) {
}
return returnStr;
}
public static String decrypt(String str, String key) {
String returnStr = "";
try {
DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(Base64.decodeBase64(key));
SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
byte[] result = cipher.doFinal(Base64.decodeBase64(str));
System.out.println("decrypt : " + new String(result));
} catch (Exception e) {
e.printStackTrace();
}
return returnStr;
}
public static void main(String[] args) {
String str = "hello 1111111111";
String key = generageKey();
String encryptStr = encrypt(str, key);
decrypt(encryptStr, key);
}
}