// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.casic.util; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; public class RSAUtils { public static String RSAPrivateKey = "RSAPrivateKey"; public static String RSAPublicKey = "RSAPublicKey"; public RSAUtils() { } public static void main(String[] args) throws Exception { Map<String, String> keyMap = new HashMap(); genKeyPair(); long ts = (new Date()).getTime() / 1000L; String timestamp = String.valueOf(ts); String message = "f3dwfgm2wg9g" + timestamp; System.out.println("随机生成的公钥为:" + (String)keyMap.get(0)); System.out.println("随机生成的私钥为:" + (String)keyMap.get(1)); String public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCOUbkwDVAIPSZTwc00XNSoZRPr37BMB8f5dnuWPL3DvgI9E1l4gVysu80XP/qiZjSkeTWLsxt47jjUeTICrLcC23jebqyNrXU9LpqebVprq8tPjhjPK49GlOT0LuOvdaKRXK6R7izgMdyROQuekDpnn7qKfTi6tQWTVyS7Ryf6DQIDAQAB"; String private_key = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAI5RuTANUAg9JlPBzTRc1KhlE+vfsEwHx/l2e5Y8vcO+Aj0TWXiBXKy7zRc/+qJmNKR5NYuzG3juONR5MgKstwLbeN5urI2tdT0ump5tWmury0+OGM8rj0aU5PQu4691opFcrpHuLOAx3JE5C56QOmefuop9OLq1BZNXJLtHJ/oNAgMBAAECgYBg5ofejdHmKiyEbroXYvVwSpIL0dwpiErCMcfn/Sd/tUm06A3NHNdKzPvRTsCJ/mjta5HnsmWMKzcKhBJm+84G36wutg1oXClXy0NK7wZy+mpj8Rxw57e9SpwCP5e3kMXhcnKalZHbb7KqUwWesZRQ24Mb0kxIBzRhJnaUVyj8SQJBANq90Sd3JXmwpHdU6UbYidFfxOZzLXas0Ti1MgwopClJTtjrZpxjtRRUrqGqea2hknhiu6xPxFB/On6IV6bS4KcCQQCmj4c2dKZ2pw0Dt+in9l4up9A2ztLlOLcjtAn9OMhmrG9L5PdXLRdTMeLbZguQ9srCUx5nfB3g0nEQMXrUG5IrAkEAoni2759Z4IdJwbD6DDsuMqv1rk4CiNFYHczIPau6M0hWHlzKvIJ6r30usLnU4xjNmEmNqDEfWcMGiZFvuWOMpQJABC+RN1YhJNQ36IMDrszrOwLuoaH6/NW7DIOmyJ1Xs/Tbay3a5W71qhqT1+2tlrwNBmsuVW1yFsdTHhQQQSpcNwJBAKD5zFqxWlOAWHbPInp3SvIV5RH+GCUZLxgEFvvLg2xi7vVigkzbf6BOGLIXi6rjyomrxNo/aGGQ1r5vAGU9M7c="; String messageEn = encrypt(message, public_key); System.out.println(message + "\t加密后的字符串为:" + messageEn); String messageDe = decrypt(messageEn, private_key); System.out.println("还原后的字符串为:" + messageDe); } public static Map<String, String> genKeyPair() throws NoSuchAlgorithmException { Map<String, String> keyMap = new HashMap(); KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); keyPairGen.initialize(1024, new SecureRandom()); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic(); String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded())); String privateKeyString = new String(Base64.encodeBase64(privateKey.getEncoded())); keyMap.put(RSAPublicKey, publicKeyString); keyMap.put(RSAPrivateKey, privateKeyString); return keyMap; } public static String encrypt(String str, String publicKey) throws Exception { byte[] decoded = Base64.decodeBase64(publicKey); RSAPublicKey pubKey = (RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(1, pubKey); String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8"))); return outStr; } public static String decrypt(String str, String privateKey) throws Exception { byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8")); byte[] decoded = Base64.decodeBase64(privateKey); RSAPrivateKey priKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(2, priKey); String outStr = new String(cipher.doFinal(inputByte)); return outStr; } }