欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

java aes加密和rsa

林國瑞1年前8瀏覽0評論

Java中的AES加密算法是一種高級(jí)加密標(biāo)準(zhǔn),可以對數(shù)據(jù)進(jìn)行對稱加密。它使用相同的密鑰加密和解密數(shù)據(jù),因此需要確保密鑰的安全性。

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class AESUtil {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
public static byte[] encrypt(byte[] key, byte[] data) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] key, byte[] encryptedData) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
return cipher.doFinal(encryptedData);
}
}

RSA加密算法是一種非對稱加密算法,它需要一對公鑰和私鑰,公鑰可以用于加密數(shù)據(jù),私鑰可以用于解密數(shù)據(jù)。RSA算法不僅可以用于加密傳輸?shù)臄?shù)據(jù),還可以用于數(shù)字簽名。

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class RSAUtil {
private static final String ALGORITHM = "RSA";
private static final int KEY_SIZE = 2048;
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(KEY_SIZE);
return keyPairGenerator.generateKeyPair();
}
public static byte[] encrypt(PublicKey publicKey, byte[] data) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
public static byte[] decrypt(PrivateKey privateKey, byte[] encryptedData) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(encryptedData);
}
}