|

楼主 |
发表于 2022-6-1 11:42:48
|
显示全部楼层
本帖最后由 范承蝶 于 2022-6-1 11:45 编辑
RSA代码后续以及测试运行截图:
(2)Base64Code
- package test;
- public class Base64{
- private static char[] Base64Code = {
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
- 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
- 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
- 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
- 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
- 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', '+', '/' };
-
- private static byte[] Base64Decode = {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, 62, -1, 63, -1, 63, 52, 53,
- 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
- -1, 0, -1, -1, -1, 0, 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, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1 };
-
- public static String encode(byte[] b)
- {
- int code = 0;
- if (b == null)
- return null;
- StringBuffer sb = new StringBuffer((b.length - 1) / 3 << 6);
- for (int i = 0; i < b.length; i++)
- {
- code |= b[i] << 16 - i % 3 * 8 & 255 << 16 - i % 3 * 8;
- if ((i % 3 != 2) && (i != b.length - 1))
- continue;
- sb.append(Base64Code[((code & 0xFC0000) >>> 18)]);
- sb.append(Base64Code[((code & 0x3F000) >>> 12)]);
- sb.append(Base64Code[((code & 0xFC0) >>> 6)]);
- sb.append(Base64Code[(code & 0x3F)]);
- code = 0;
- }
-
- if (b.length % 3 > 0)
- sb.setCharAt(sb.length() - 1, '=');
- if (b.length % 3 == 1)
- sb.setCharAt(sb.length() - 2, '=');
- return sb.toString();
- }
-
- public static byte[] decode(String code)
- {
- if (code == null)
- return null;
- int len = code.length();
- if (len % 4 != 0)
- throw new IllegalArgumentException("Base64 string length must be 4*n");
- if (code.length() == 0)
- return new byte[0];
- int pad = 0;
- if (code.charAt(len - 1) == '=')
- pad++;
- if (code.charAt(len - 2) == '=')
- pad++;
- int retLen = len / 4 * 3 - pad;
- byte[] ret = new byte[retLen];
- for (int i = 0; i < len; i += 4)
- {
- int j = i / 4 * 3;
- char ch1 = code.charAt(i);
- char ch2 = code.charAt(i + 1);
- char ch3 = code.charAt(i + 2);
- char ch4 = code.charAt(i + 3);
- int tmp = Base64Decode[ch1] << 18 | Base64Decode[ch2] << 12 | Base64Decode[ch3] << 6 | Base64Decode[ch4];
- ret[j] = (byte)((tmp & 0xFF0000) >> 16);
- if (i < len - 4)
- {
- ret[(j + 1)] = (byte)((tmp & 0xFF00) >> 8);
- ret[(j + 2)] = (byte)(tmp & 0xFF);
- }
- else {
- if (j + 1 < retLen)
- ret[(j + 1)] = (byte)((tmp & 0xFF00) >> 8);
- if (j + 2 < retLen)
- ret[(j + 2)] = (byte)(tmp & 0xFF);
- }
- }
- return ret;
- }
- }
复制代码
(3)RSATest
- import java.util.Map;
- import test.RSA;
-
- public class RSATest {
- static String publicKey;
- static String privateKey;
-
- static {
- try {
- Map<String, Object> keyMap = RSA.genKeyPair();
- publicKey = RSA.getPublicKey(keyMap);
- privateKey = RSA.getPrivateKey(keyMap);
- System.err.println("公钥: " + publicKey);
- System.err.println("私钥: " + privateKey);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args) throws Exception {
- test();
- testSign();
- }
-
- static void test() throws Exception {
- System.err.println("公钥加密——私钥解密");
- String source = "一边清零,一边拥有";
- System.out.println("加密前文字:" + source);
- byte[] data = source.getBytes();
- //公钥加密
- byte[] encodedData = RSA.encryptByPublicKey(data, publicKey);
- System.out.println("加密后文字:" + new String(encodedData));
- //私钥解密
- byte[] decodedData = RSA.decryptByPrivateKey(encodedData, privateKey);
- String target = new String(decodedData);
- System.out.println("解密后文字: " + target);
- }
-
- static void testSign() throws Exception {
- System.err.println("私钥加密——公钥解密");
- String source = "一边清零,一边拥有";
- System.out.println("原文字:" + source);
- byte[] data = source.getBytes();
- //私钥加密
- byte[] encodedData = RSA.encryptByPrivateKey(data, privateKey);
- System.out.println("加密后:" + new String(encodedData));
- //公钥解密
- byte[] decodedData = RSA.decryptByPublicKey(encodedData, publicKey);
- String target = new String(decodedData);
- System.out.println("解密后: " + target);
-
- System.err.println("私钥签名——公钥验证签名");
- String sign = RSA.sign(encodedData, privateKey);
- System.err.println("签名:\r" + sign);
- boolean status = RSA.verify(encodedData, publicKey, sign);
- System.err.println("验证结果:\r" + status);
- }
- }
复制代码
4、运行截图:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|