内容
- 对称AES加密Demo
- 对称AES解密Demo
- 非对称RSA加密与解密Demo
- 整理成工具类
1.对称加密AES简单Demo
String key = "Q!W#R%G&Q!W#R%G&";
String content = "哈喽";
String algorithm = "AES";
String transformation = "AES";
Cipher cipher = Cipher.getInstance(transformation);
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), algorithm);
cipher.init(CipherMode.encrypt.getValue(), secretKeySpec);
byte[] bytes = cipher.doFinal(content.getBytes());
System.out.println(Base64.encode(bytes ));
2.对称AES解密Demo
String algorithm = "AES";
String transformation = "AES";
Cipher cipher = Cipher.getInstance(transformation);
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), algorithm);
cipher.init(CipherMode.decrypt.getValue(), secretKeySpec);
String content = new String(cipher.doFinal(bytes));
System.out.println(content);
3.非对称RSA加密与解密Demo
String algorithm = "RSA";
String content ="content";
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
byte[] bytes = cipher.doFinal(content.getBytes());
System.out.println(Base64.encode(bytes));
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes1 = cipher.doFinal(bytes);
System.out.println(new String(bytes1));
4.整理成工具类
public class CipherSecurityUtil {
public static final int RSA_KEY_PAIR_LENGTH = 2048;
public static final String AES_KEY_ALGORITHM = "AES";
public static final String RSA_CIPHER_TYPE = "RSA";
public static final String AES_DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
public static final String SHA256_KEY_ALGORITHM = "SHA256withRSA";
public static Cipher getAESCipher(CipherMode cipherMode, String pwdKey) throws Exception {
byte[] key = pwdKey.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(key, AES_KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(AES_DEFAULT_CIPHER_ALGORITHM);
cipher.init(cipherMode.getValue(),secretKeySpec);
return cipher;
}
public static String generateAESSecret() {
String result = UUID.randomUUID().toString().replaceAll("-", "");
result = result.substring(0,16);
return result;
}
public static byte[] encryptAES(String content,String pwdKey) throws Exception {
Cipher cipher = getAESCipher(CipherMode.encrypt, pwdKey);
return cipher.doFinal(content.getBytes());
}
public static byte[] decryptAES(byte[] content,String pwdKey) throws Exception {
Cipher cipher = getAESCipher(CipherMode.decrypt, pwdKey);
return cipher.doFinal(content);
}
public static String encodeAESBase64(String content,String pwdKey) throws Exception {
return Base64.encode(encryptAES(content,pwdKey));
}
public static String decodeAESBase64(String content,String pwdKey) throws Exception {
return new String(decryptAES(Base64.decode(content),pwdKey));
}
public static Cipher getRSACipher(CipherMode cipherMode, Key key) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_CIPHER_TYPE);
cipher.init(cipherMode.getValue(),key);
return cipher;
}
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_CIPHER_TYPE);
keyPairGenerator.initialize(RSA_KEY_PAIR_LENGTH);
return keyPairGenerator.generateKeyPair();
}
public static PublicKey getPublicKey(KeyPair keyPair) throws Exception {
byte[] encoded = keyPair.getPublic().getEncoded();
KeyFactory keyFactory = KeyFactory.getInstance(RSA_CIPHER_TYPE);
X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(encoded);
return keyFactory.generatePublic(encodedKeySpec);
}
public static PrivateKey getPrivateKey(KeyPair keyPair) throws Exception {
byte[] encoded = keyPair.getPrivate().getEncoded();
KeyFactory keyFactory = KeyFactory.getInstance(RSA_CIPHER_TYPE);
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(encoded);
return keyFactory.generatePrivate(encodedKeySpec);
}
public static byte[] cipherRSAByPublicKeyEncrypt(String content, PublicKey publicKey) throws Exception {
Cipher rsaCipher = getRSACipher(CipherMode.encrypt, publicKey);
return rsaCipher.doFinal(content.getBytes());
}
public static byte[] cipherRSAByPrivateKeyDecrypt(byte[] content, PrivateKey privateKey) throws Exception {
Cipher rsaCipher = getRSACipher(CipherMode.decrypt, privateKey);
return rsaCipher.doFinal(content);
}
public static String encodeREABase64(String content,PublicKey publicKey) throws Exception {
return Base64.encode(cipherRSAByPublicKeyEncrypt(content,publicKey));
}
public static String decodeRSABase64(String content,PrivateKey privateKey) throws Exception {
return new String(cipherRSAByPrivateKeyDecrypt(Base64.decode(content),privateKey));
}
public static String getSignature(String content, String algorithm, PrivateKey privateKey) throws Exception {
algorithm = ObjectUtil.isNotNull(algorithm)?algorithm:"SHA256withRSA";
Signature instance = Signature.getInstance(algorithm);
instance.initSign(privateKey);
instance.update(content.getBytes());
return Base64.encode(instance.sign());
}
public static Boolean verifySignature(String content, String algorithm,PublicKey publicKey,String encryptContent) throws Exception{
algorithm = ObjectUtil.isNotNull(algorithm)?algorithm:"SHA256withRSA";
Signature instance = Signature.getInstance(algorithm);
instance.initVerify(publicKey);
instance.update(content.getBytes());
return instance.verify(Base64.decode(encryptContent));
}
public static void main(String[] args) throws Exception {
KeyPair keyPair = getKeyPair();
PublicKey publicKey = getPublicKey(keyPair);
PrivateKey privateKey = getPrivateKey(keyPair);
String content = "哈喽";
String signature = getSignature(content, null, privateKey);
Boolean aBoolean = verifySignature(content, null, publicKey, signature);
System.out.println(aBoolean);
}