1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| import org.apache.tomcat.util.codec.binary.Base64; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;
import javax.crypto.Cipher; import java.security.*; import java.security.spec.EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;
public class CipherRsaUtils {
private static final String CIPHER_TRANSFORMS = "RSA/ECB/PKCS1Padding"; private static final int KEY_PAIR_LENGTH = 1024;
public static KeyPair generateRsaKeyPair(String secureRandomSeed) { KeyPair keyPair = null; try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(secureRandomSeed.getBytes());
keyPairGenerator.initialize(KEY_PAIR_LENGTH, secureRandom); keyPair = keyPairGenerator.genKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return keyPair; }
public static String cipherByPrivateKey(String content, int model, String secureRandomSeed) { String result = ""; try { KeyPair keyPair = generateRsaKeyPair(secureRandomSeed);
byte[] privateEncoded = keyPair.getPrivate().getEncoded();
EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(privateEncoded); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey keyPrivate = keyFactory.generatePrivate(encodedKeySpec); result = encryptionDecryption(content, model, keyPrivate); } catch (Exception e) { e.printStackTrace(); } return result; }
public static String cipherByPublicKey(String content, int model, String secureRandomSeed) { String result = ""; try { KeyPair keyPair = generateRsaKeyPair(secureRandomSeed); EncodedKeySpec keySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey keyPublic = keyFactory.generatePublic(keySpec); result = encryptionDecryption(content, model, keyPublic); } catch (Exception e) { e.printStackTrace(); } return result; }
private static String encryptionDecryption(String content, int model, Key key) { String result = ""; try { Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMS); cipher.init(model, key); byte[] contentBytes = content.getBytes(); if (model == Cipher.DECRYPT_MODE) { contentBytes = new BASE64Decoder().decodeBuffer(content); }
byte[] finalBytes = cipher.doFinal(contentBytes); if (model == Cipher.ENCRYPT_MODE) { result = new BASE64Encoder().encode(finalBytes); } else if (model == Cipher.DECRYPT_MODE) { result = new String(finalBytes); } } catch (Exception e) { e.printStackTrace(); } return result; }
public static void main(String[] args) { String content = "panda"; String secureRandomSeed = "1646894592726";
System.out.println("被加密数据字节大小:" + content.getBytes().length + " 字节," + content.length() + " 个字符"); System.out.println("源内容:\n" + content);
KeyPair keyPair = generateRsaKeyPair(secureRandomSeed);
String publicKey = new String(keyPair.getPublic().getEncoded()); System.out.println("publicKey:\n" + publicKey);
String publicKeyBase64 = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded())); System.out.println("publicKeyBase64:\n" + publicKeyBase64);
String privateKeyBase64 = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded())); System.out.println("privateKeyBase64:\n" + privateKeyBase64);
try { String encrypted = CipherRsaUtils.cipherByPublicKey(content, 1, secureRandomSeed); String decrypted = CipherRsaUtils.cipherByPrivateKey(encrypted, 2, secureRandomSeed); System.out.println("加密后:\n" + encrypted); System.out.println("解密后:\n" + decrypted);
String decrypted2 = CipherRsaUtils.cipherByPrivateKey("j1Jg10GQDqD2G5JQQ/0iuITz60XbIZarXT8SckLwqjy6vwOVzM7U0kDBcSP8cpnJIQnz3zQt/6Igi2GejhCfhuFOB3zD9eJb8qCRwG7xRSadnGyi9gD6RJFQAOHd8hde4ra7XuPL7v7JylM2QzYq2GdZDhRkTiNVEeyPn/+aOJE=", 2, secureRandomSeed); System.out.println("over:\n" + decrypted2); } catch (Exception e) { e.printStackTrace(); }
} }
|