今天讓大家脫坑的是JAVA實現的RSA算法,代碼網上當的,像我這樣的菜雞,只能搬磚,來個代碼,修修補補,報錯百度找教程。
1、軟件環境: eclipse+jdk8.0
2、網上下載jar包
2.1.1下載鏈接:http://hc.apache.org/downloads.cgi
2.1.2下載文檔的具體截圖:
2.2下載鏈接:http://commons.apache.org/io/download_io.cgi然后進入鏡像的鏈接是這樣子的http://mirrors.hust.edu.cn/apache/commons/io/binaries/
2.2.2下載文檔的具體截圖:1:正兒八經的截圖地址
2.2.3進入鏡像后的截圖
3、將下載好的jar包解壓縮,打開eclipse,導入jar包:菜雞做法,請輕噴!
3.1在你的項目文件上右擊,新建文件夾,取個名兒
3.2把你辛苦下載好的jar包文件復制粘貼在文件夾下
3.3右擊這個取啥名兒的文件夾-》構建路徑-》add to buildpath
4、網上當的代碼,寫的很棒棒,讓我這個菜雞膜拜一下。
在此貼上大神的文檔鏈接:https://blog.csdn.net/cz0217/article/details/78426733
PS.代碼我貼上來了,在這里呢~:包名是com,類名是RSAUtils,應該很詳細了吧23333333333333333333
1 package com; 2 3 import org.apache.commons.codec.binary.Base64; 4 import org.apache.commons.io.IOUtils; 5 6 import javax.crypto.Cipher; 7 import java.io.ByteArrayOutputStream; 8 import java.security.*; 9 import java.security.interfaces.RSAPrivateKey; 10 import java.security.interfaces.RSAPublicKey; 11 import java.security.spec.InvalidKeySpecException; 12 import java.security.spec.PKCS8EncodedKeySpec; 13 import java.security.spec.X509EncodedKeySpec; 14 import java.util.HashMap; 15 import java.util.Map; 16 17 public class RSAUtils { 18 19 public static final String CHARSET = "UTF-8"; 20 public static final String RSA_ALGORITHM = "RSA"; 21 22 23 public static Map<String, String> createKeys(int keySize){ 24 //為RSA算法創建一個KeyPairGenerator對象 25 KeyPairGenerator kpg; 26 try{ 27 kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM); 28 }catch(NoSuchAlgorithmException e){ 29 throw new IllegalArgumentException("No such algorithm-->[" + RSA_ALGORITHM + "]"); 30 } 31 32 //初始化KeyPairGenerator對象,密鑰長度 33 kpg.initialize(keySize); 34 //生成密匙對 35 KeyPair keyPair = kpg.generateKeyPair(); 36 //得到公鑰 37 Key publicKey = keyPair.getPublic(); 38 String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded()); 39 //得到私鑰 40 Key privateKey = keyPair.getPrivate(); 41 String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded()); 42 Map<String, String> keyPairMap = new HashMap<String, String>(); 43 keyPairMap.put("publicKey", publicKeyStr); 44 keyPairMap.put("privateKey", privateKeyStr); 45 46 return keyPairMap; 47 } 48 49 /** 50 * 得到公鑰 51 * @param publicKey 密鑰字符串(經過base64編碼) 52 * @throws Exception 53 */ 54 public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { 55 //通過X509編碼的Key指令獲得公鑰對象 56 KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); 57 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey)); 58 RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec); 59 return key; 60 } 61 62 /** 63 * 得到私鑰 64 * @param privateKey 密鑰字符串(經過base64編碼) 65 * @throws Exception 66 */ 67 public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException { 68 //通過PKCS#8編碼的Key指令獲得私鑰對象 69 KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); 70 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)); 71 RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec); 72 return key; 73 } 74 75 /** 76 * 公鑰加密 77 * @param data 78 * @param publicKey 79 * @return 80 */ 81 public static String publicEncrypt(String data, RSAPublicKey publicKey){ 82 try{ 83 Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); 84 cipher.init(Cipher.ENCRYPT_MODE, publicKey); 85 return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength())); 86 }catch(Exception e){ 87 throw new RuntimeException("加密字符串[" + data + "]時遇到異常", e); 88 } 89 } 90 91 /** 92 * 私鑰解密 93 * @param data 94 * @param privateKey 95 * @return 96 */ 97 98 public static String privateDecrypt(String data, RSAPrivateKey privateKey){ 99 try{ 100 Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); 101 cipher.init(Cipher.DECRYPT_MODE, privateKey); 102 return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), privateKey.getModulus().bitLength()), CHARSET); 103 }catch(Exception e){ 104 throw new RuntimeException("解密字符串[" + data + "]時遇到異常", e); 105 } 106 } 107 108 /** 109 * 私鑰加密 110 * @param data 111 * @param privateKey 112 * @return 113 */ 114 115 public static String privateEncrypt(String data, RSAPrivateKey privateKey){ 116 try{ 117 Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); 118 cipher.init(Cipher.ENCRYPT_MODE, privateKey); 119 return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), privateKey.getModulus().bitLength())); 120 }catch(Exception e){ 121 throw new RuntimeException("加密字符串[" + data + "]時遇到異常", e); 122 } 123 } 124 125 /** 126 * 公鑰解密 127 * @param data 128 * @param publicKey 129 * @return 130 */ 131 132 public static String publicDecrypt(String data, RSAPublicKey publicKey){ 133 try{ 134 Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); 135 cipher.init(Cipher.DECRYPT_MODE, publicKey); 136 return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), publicKey.getModulus().bitLength()), CHARSET); 137 }catch(Exception e){ 138 throw new RuntimeException("解密字符串[" + data + "]時遇到異常", e); 139 } 140 } 141 142 private static byte[] rsaSplitCodec(Cipher cipher, int opmode, byte[] datas, int keySize){ 143 int maxBlock = 0; 144 if(opmode == Cipher.DECRYPT_MODE){ 145 maxBlock = keySize / 8; 146 }else{ 147 maxBlock = keySize / 8 - 11; 148 } 149 ByteArrayOutputStream out = new ByteArrayOutputStream(); 150 int offSet = 0; 151 byte[] buff; 152 int i = 0; 153 try{ 154 while(datas.length > offSet){ 155 if(datas.length-offSet > maxBlock){ 156 buff = cipher.doFinal(datas, offSet, maxBlock); 157 }else{ 158 buff = cipher.doFinal(datas, offSet, datas.length-offSet); 159 } 160 out.write(buff, 0, buff.length); 161 i++; 162 offSet = i * maxBlock; 163 } 164 }catch(Exception e){ 165 throw new RuntimeException("加解密閥值為["+maxBlock+"]的數據時發生異常", e); 166 } 167 byte[] resultDatas = out.toByteArray(); 168 IOUtils.closeQuietly(out); 169 return resultDatas; 170 } 171 172 173 174 public static void main (String[] args) throws Exception { 175 Map<String, String> keyMap = RSAUtils.createKeys(1024); 176 String publicKey = keyMap.get("publicKey"); 177 String privateKey = keyMap.get("privateKey"); 178 System.out.println("公鑰: \n\r" + publicKey); 179 System.out.println("私鑰: \n\r" + privateKey); 180 181 System.out.println("公鑰加密——私鑰解密"); 182 String str = "\n" + 183 "成長帶走的不只是時光\n" + 184 "還帶走了當初那些不害怕失去的勇氣\n" + 185 "讓自己忙一點,忙到沒時間去思考無關緊要的事,很多事就能悄悄淡忘了\n" + 186 "時間不一定能證明很多東西\n" + 187 "但是一定能看透很多東西\n" + 188 "堅信自己的選擇,不動搖,使勁跑,明天會更好"; 189 System.out.println("\r明文:\r\n" + str); 190 System.out.println("\r明文大小:\r\n" + str.getBytes().length); 191 String encodedData = RSAUtils.publicEncrypt(str, RSAUtils.getPublicKey(publicKey)); 192 System.out.println("密文:\r\n" + encodedData); 193 String decodedData = RSAUtils.privateDecrypt(encodedData, RSAUtils.getPrivateKey(privateKey)); 194 System.out.println("解密后文字: \r\n" + decodedData); 195 196 197 } 198 }
代碼運行截圖在這里:
稍微皮一下:
今天是2018年5月27日,首先呢恭喜小哥哥拿到了藍橋杯國二,祝福啊,來個撒花233333333333333333
ps.會不會被打,溜了溜了