AES
package encrypt;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.Charset;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class AesUtil {
private static final String ALGORITHM_MODE = "AES";
private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
private static final int SECRET_KEY_SIZE = 128;
private static final int IV_KEY_SIZE = 16;
private AesUtil() {
}
public static String encrypt(String plaintext, String strKey) {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, getKey(strKey), getIv(strKey));
byte[] bytes = plaintext.getBytes(Charset.defaultCharset());
return Base64.encodeBase64String(cipher.doFinal(bytes));
} catch (Exception e) {
throw new RuntimeException("Encryption failed. Cause: ", e);
}
}
public static String decrypt(String cipherText, String strKey) {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, getKey(strKey), getIv(strKey));
byte[] bytes = Base64.decodeBase64(cipherText);
return StringUtils.newStringUtf8(cipher.doFinal(bytes));
} catch (Exception e) {
throw new RuntimeException("Decryption failed. Cause: ", e);
}
}
private static IvParameterSpec getIv(String ivKey) {
if (ivKey == null) {
ivKey = "";
}
StringBuffer stringBuffer = new StringBuffer(IV_KEY_SIZE);
stringBuffer.append(ivKey);
while (stringBuffer.length() < IV_KEY_SIZE) {
stringBuffer.append("0");
}
if (stringBuffer.length() > IV_KEY_SIZE) {
stringBuffer.setLength(IV_KEY_SIZE);
}
return new IvParameterSpec(StringUtils.getBytesUtf8(stringBuffer.toString()));
}
private static Key getKey(String strKey) {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_MODE);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(StringUtils.getBytesUtf8(strKey));
keyGenerator.init(SECRET_KEY_SIZE, secureRandom);
return keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Failed to generate key. Cause: " + e);
}
}
public static void main(String[] args) throws Exception {
String plaintext = "这是一条待加密的字符串";
String strKey = "qazc===+++,";
String encrypt = encrypt(plaintext, strKey);
String decrypt = decrypt(encrypt, strKey);
System.out.println("原文:" + plaintext + "\n" + "加密后:" + encrypt + "\n" + "解密后:" + decrypt);
}
}
DES
package encrypt;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.Charset;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class DesUtil {
private static final String ALGORITHM_MODE = "DES";
private static final String TRANSFORMATION = "DES/CBC/PKCS5Padding";
private static final int SECRET_KEY_SIZE = 56;
private static final int IV_KEY_SIZE = 8;
private DesUtil() {
}
public static String encrypt(String plaintext, String strKey) {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, getKey(strKey), getIv(strKey));
byte[] bytes = plaintext.getBytes(Charset.defaultCharset());
return Base64.encodeBase64String(cipher.doFinal(bytes));
} catch (Exception e) {
throw new RuntimeException("Encryption failed. Cause: ", e);
}
}
public static String decrypt(String cipherText, String strKey) {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, getKey(strKey), getIv(strKey));
byte[] bytes = Base64.decodeBase64(cipherText.getBytes(Charset.defaultCharset()));
return StringUtils.newStringUtf8(cipher.doFinal(bytes));
} catch (Exception e) {
throw new RuntimeException("Decryption failed. Cause: ", e);
}
}
private static IvParameterSpec getIv(String ivKey) {
if (ivKey == null) {
ivKey = "";
}
StringBuffer stringBuffer = new StringBuffer(IV_KEY_SIZE);
stringBuffer.append(ivKey);
while (stringBuffer.length() < IV_KEY_SIZE) {
stringBuffer.append("0");
}
if (stringBuffer.length() > IV_KEY_SIZE) {
stringBuffer.setLength(IV_KEY_SIZE);
}
return new IvParameterSpec(StringUtils.getBytesUtf8(stringBuffer.toString()));
}
private static Key getKey(String strKey) {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_MODE);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(StringUtils.getBytesUtf8(strKey));
keyGenerator.init(SECRET_KEY_SIZE, secureRandom);
return keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Failed to generate key. Cause: " + e);
}
}
public static void main(String[] args) throws Exception {
String plaintext = "qwert1234";
String strKey = "okmpl,";
String encrypt = encrypt(plaintext, strKey);
String decrypt = decrypt(encrypt, strKey);
System.out.println("原文:" + plaintext + "\n" + "加密后:" + encrypt + "\n" + "解密后:" + decrypt);
}
}
RSA
package encrypt;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.binary.StringUtils;
import javax.crypto.Cipher;
import java.io.*;
import java.security.*;
import java.security.interfaces.RSAKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.UUID;
public class RsaUtil {
private static final String ALGORITHM = "RSA";
private static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
private static final int KEY_SIZE = 1024;
private static final String FILENAME_PUBLIC = "id_rsa.pub";
private static final String FILENAME_PRIVATE = "id_rsa";
private RsaUtil() {
}
public static void generateKeyPair(String filePath) {
KeyPair keyPair = initKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
String publicKeyString = encodeBase64String(publicKey.getEncoded());
String privateKeyString = encodeBase64String(privateKey.getEncoded());
storeKeyByFile(publicKeyString, filePath + FILENAME_PUBLIC);
storeKeyByFile(privateKeyString, filePath + FILENAME_PRIVATE);
}
private static KeyPair initKeyPair() {
try {
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", "SUN");
secureRandom.setSeed(StringUtils.getBytesUtf8(UUID.randomUUID().toString().replaceAll("-", "")));
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(KEY_SIZE, secureRandom);
return keyPairGenerator.generateKeyPair();
} catch (Exception e) {
throw new RuntimeException("Failed to initialize KeyPair. Cause: ", e);
}
}
private static void storeKeyByFile(String keyString, String pathName) {
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File(pathName)));
bufferedWriter.write(keyString);
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException("File build failed. Cause: ", e);
}
}
public static String readKeyFromFile(String pathName) {
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(pathName)));
String readLine = null;
StringBuffer result = new StringBuffer();
while ((readLine = bufferedReader.readLine()) != null) {
result.append(readLine);
}
return result.toString();
} catch (IOException e) {
throw new RuntimeException("File read failed. Cause: ", e);
}
}
public static RSAPublicKey getPublicKeyFromString(String publicKeyString) {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(decodeBase64(publicKeyString));
try {
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
return (RSAPublicKey) keyFactory.generatePublic(x509EncodedKeySpec);
} catch (Exception e) {
throw new RuntimeException("Failed to get RSAPublicKey. Cause: ", e);
}
}
public static RSAPrivateKey getPrivateKeyFromString(String privateString) {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(decodeBase64(privateString));
try {
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
return (RSAPrivateKey) keyFactory.generatePrivate(pkcs8EncodedKeySpec);
} catch (Exception e) {
throw new RuntimeException("Failed to get RSAPrivateKey. Cause: ", e);
}
}
public static String encryptByKey(String plaintext, Key key) {
if (key == null) {
throw new RuntimeException("Key is empty");
}
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bytes = cipher.doFinal(StringUtils.getBytesUtf8(plaintext));
return encodeBase64String(bytes);
} catch (Exception e) {
throw new RuntimeException("Failed to encrypt. Cause: ", e);
}
}
public static String decryptByKey(String cipherText, Key key) {
if (key == null) {
throw new RuntimeException("Key is empty");
}
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] bytes = cipher.doFinal(decodeBase64(cipherText));
return StringUtils.newStringUtf8(bytes);
} catch (Exception e) {
throw new RuntimeException("Failed to decryption. Cause: ", e);
}
}
public static String encryptByKey2(String plaintext, Key key) {
if (key == null) {
throw new RuntimeException("Key is empty");
}
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
int splitLength = ((RSAKey) key).getModulus().bitLength() / 8 - 11;
byte[][] arrays = splitBytes(StringUtils.getBytesUtf8(plaintext), splitLength);
StringBuffer result = new StringBuffer();
for (byte[] array : arrays) {
result.append(bytesToHexString(cipher.doFinal(array)));
}
return result.toString();
} catch (Exception e) {
throw new RuntimeException("Failed to encrypt. Cause: ", e);
}
}
public static String decryptByKey2(String cipherText, Key key) {
if (key == null) {
throw new RuntimeException("Key is empty");
}
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
int splitLength = ((RSAKey) key).getModulus().bitLength() / 8;
byte[][] arrays = splitBytes(hexStringToBytes(cipherText), splitLength);
StringBuffer result = new StringBuffer();
for (byte[] array : arrays) {
result.append(StringUtils.newStringUtf8(cipher.doFinal(array)));
}
return result.toString();
} catch (Exception e) {
throw new RuntimeException("Failed to decryption. Cause: ", e);
}
}
private static byte[][] splitBytes(byte[] bytes, int splitLength) {
int bytesLength = bytes.length;
int remainder = bytesLength % splitLength;
int quotient = remainder != 0 ? bytesLength / splitLength + 1 : bytesLength / splitLength;
byte[][] result = new byte[quotient][];
byte[] array = null;
for (int i = 0; i < quotient; i++) {
if (i == quotient - 1 && remainder != 0) {
array = new byte[remainder];
System.arraycopy(bytes, i * splitLength, array, 0, remainder);
} else {
array = new byte[splitLength];
System.arraycopy(bytes, i * splitLength, array, 0, splitLength);
}
result[i] = array;
}
return result;
}
private static String encodeBase64String(byte[] bytes) {
return Base64.encodeBase64String(bytes);
}
private static byte[] decodeBase64(String data) {
return Base64.decodeBase64(data);
}
private static String bytesToHexString(byte[] bytes) {
return Hex.encodeHexString(bytes);
}
private static byte[] hexStringToBytes(String data) {
try {
return Hex.decodeHex(data);
} catch (DecoderException e) {
throw new RuntimeException("Conversion failure. Cause: ", e);
}
}
public static String signByPrivateKey(String content, String privateKey) {
RSAPrivateKey rsaPrivateKey = getPrivateKeyFromString(privateKey);
try {
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(rsaPrivateKey);
signature.update(StringUtils.getBytesUtf8(content));
return encodeBase64String(signature.sign());
} catch (Exception e) {
throw new RuntimeException("Failed to add signature. Cause: ", e);
}
}
public static boolean verifySignByPublicKey(String content, String publicKey, String sign) {
RSAPublicKey rsaPublicKey = getPublicKeyFromString(publicKey);
try {
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(rsaPublicKey);
signature.update(StringUtils.getBytesUtf8(content));
return signature.verify(decodeBase64(sign));
} catch (Exception e) {
throw new RuntimeException("Signature verification failed. Cause: ", e);
}
}
public static void main(String[] args) {
String filepath = "E:/";
String publicKeyString = readKeyFromFile(filepath + FILENAME_PUBLIC);
String privateKeyString = readKeyFromFile(filepath + FILENAME_PRIVATE);
RSAPublicKey rsaPublicKey = getPublicKeyFromString(publicKeyString);
RSAPrivateKey rsaPrivateKey = getPrivateKeyFromString(privateKeyString);
String plainText = "qwert12345";
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < 1; i++) {
stringBuffer.append("123456qwertyu");
}
plainText = stringBuffer.toString();
String cipherText = encryptByKey(plainText, rsaPrivateKey);
String decryptText = decryptByKey(cipherText, rsaPublicKey);
System.out.println("------- 私钥加密,公钥解密 --------");
System.out.println("plainText: " + plainText + "\n" + "cipherText: " + cipherText + "\n" + "decryptText: " + decryptText);
cipherText = encryptByKey2(plainText, rsaPublicKey);
decryptText = decryptByKey2(cipherText, rsaPrivateKey);
System.out.println("------- 公钥加密,私钥解密 -------");
System.out.println("plainText: " + plainText + "\n" + "cipherText: " + cipherText + "\n" + "decryptText: " + decryptText);
System.out.println("------- 私钥加密,公钥解密 -------");
String signStr = signByPrivateKey(plainText, privateKeyString);
boolean success = verifySignByPublicKey(plainText, publicKeyString, signStr);
System.out.println("签名串: " + signStr + "\n" + "验签: " + success);
}
}