Java實現RSA加密演算法
生成金鑰對
首先建立 KeyPairGenerator
類的物件,用於生成公鑰和私鑰對
// 生成公鑰和私鑰對,基於RSA演算法生成物件 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
然後初始化金鑰對的長度,最低長度512位,並且長度不能低於明文的長度
// 初始化金鑰大小為1024位 keyPairGen.initialize(1024);
利用 keyPairGen
物件生成金鑰對,儲存在 KeyPair
類中
KeyPair keyPair = keyPairGen.generateKeyPair();
從 keyPair
類中獲取公鑰和私鑰
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
加密(encrypt)
protected static byte[] encrypt(RSAPublicKey publicKey, byte[] srcBytes) { if (publicKey != null) { try { // Cipher負責完成加密或解密工作,基於RSA Cipher cipher = Cipher.getInstance(ALGORITHM); // 根據公鑰,對Cipher物件進行初始化 cipher.init(Cipher.ENCRYPT_MODE, publicKey); // 加密,結果儲存進resultBytes,並返回 byte[] resultBytes = cipher.doFinal(srcBytes); return resultBytes; } catch (Exception e) { e.printStackTrace(); } } return null; }
解密(decrypt)
protected static byte[] encrypt(RSAPublicKey publicKey, byte[] srcBytes) { if (publicKey != null) { try { // Cipher負責完成加密或解密工作,基於RSA Cipher cipher = Cipher.getInstance("RSA"); // 根據公鑰,對Cipher物件進行初始化 cipher.init(Cipher.ENCRYPT_MODE, publicKey); // 加密,結果儲存進resultBytes,並返回 byte[] resultBytes = cipher.doFinal(srcBytes); return resultBytes; } catch (Exception e) { e.printStackTrace(); } } return null; }
完整程式碼
import java.security.*; import java.security.interfaces.*; import javax.crypto.*; public class RSA { private RSAPublicKey publicKey; private RSAPrivateKey privateKey; private byte[] resultBytes; private final static String ALGORITHM = "RSA"; public RSA() { try { String message = "你好,我很喜歡加密演算法"; System.out.println("明文是:" + message); // 生成公鑰和私鑰對,基於RSA演算法生成物件 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM); // 初始化金鑰對生成器,金鑰大小為1024位 keyPairGen.initialize(1024); // 生成一個金鑰對,儲存在keyPair中 KeyPair keyPair = keyPairGen.generateKeyPair(); // 得到公鑰和私鑰 publicKey = (RSAPublicKey) keyPair.getPublic(); privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 用公鑰加密 byte[] srcBytes = message.getBytes(); resultBytes = RSA.encrypt(publicKey, srcBytes); String result = new String(resultBytes); System.out.println("用公鑰加密後密文是:" + result); } catch (Exception e) { e.printStackTrace(); } } protected static byte[] encrypt(RSAPublicKey publicKey, byte[] srcBytes) { if (publicKey != null) { try { // Cipher負責完成加密或解密工作,基於RSA Cipher cipher = Cipher.getInstance(ALGORITHM); // 根據公鑰,對Cipher物件進行初始化 cipher.init(Cipher.ENCRYPT_MODE, publicKey); // 加密,結果儲存進resultBytes,並返回 byte[] resultBytes = cipher.doFinal(srcBytes); return resultBytes; } catch (Exception e) { e.printStackTrace(); } } return null; } protected byte[] decrypt(RSAPrivateKey privateKey, byte[] encBytes) { if (privateKey != null) { try { Cipher cipher = Cipher.getInstance(ALGORITHM); // 根據私鑰對Cipher物件進行初始化 cipher.init(Cipher.DECRYPT_MODE, privateKey); // 解密並將結果儲存進resultBytes byte[] decBytes = cipher.doFinal(encBytes); return decBytes; } catch (Exception e) { e.printStackTrace(); } } return null; } public static void main(String[] args) { long rsaStart = System.currentTimeMillis(); RSA rsa = new RSA(); byte[] decBytes = rsa.decrypt(rsa.privateKey, rsa.resultBytes); String dec = new String(decBytes); long rsaEnd = System.currentTimeMillis(); System.out.println("用私鑰解密後的結果是:" + dec); System.out.println("共計用時:" + (rsaEnd - rsaStart)); } }
執行結果見下面的GIF:
