Java编程实现2种现有的加密方式不包括古典密码转轮密码等简单方式基于密钥的加密方式。符合要求密码有AES、RSA、ECC、DES、3DES、Rabin、ELGamal、Diffie-Hellman等包括但不限于列出的算法。代码实现要求写出算法加密实现过程禁止调用现成模块接口只进行传参。
以下是Java编程实现AES和RSA加密方式的示例代码:
- AES加密算法实现:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
public class AESExample {
public static void main(String[] args) throws Exception {
String plainText = "Hello, World!";
String key = "1234567890123456";
byte[] encryptedText = encrypt(plainText, key);
String decryptedText = decrypt(encryptedText, key);
System.out.println("Encrypted Text: " + new String(encryptedText));
System.out.println("Decrypted Text: " + decryptedText);
}
public static byte[] encrypt(String plainText, String key) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(plainText.getBytes());
}
public static String decrypt(byte[] encryptedText, String key) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(encryptedText);
return new String(decryptedBytes);
}
}
- RSA加密算法实现:
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
public class RSAExample {
public static void main(String[] args) throws Exception {
String plainText = "Hello, World!";
KeyPair keyPair = generateKeyPair();
byte[] encryptedText = encrypt(plainText, keyPair.getPublic());
String decryptedText = decrypt(encryptedText, keyPair.getPrivate());
System.out.println("Encrypted Text: " + new String(encryptedText));
System.out.println("Decrypted Text: " + decryptedText);
}
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048, new SecureRandom());
return keyPairGenerator.generateKeyPair();
}
public static byte[] encrypt(String plainText, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(plainText.getBytes());
}
public static String decrypt(byte[] encryptedText, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedText);
return new String(decryptedBytes);
}
}
请注意,在实际应用中,AES和RSA等加密算法的密钥生成和管理应该遵循更严格的安全实践。以上示例仅供参考。
原文地址: https://www.cveoy.top/t/topic/jgGS 著作权归作者所有。请勿转载和采集!