RSA非对称加密算法
工具类
package com. safedog. datasafe. system. utils ; import cn. hutool. core. codec. Base64 ;
import cn. hutool. crypto. SecureUtil ;
import cn. hutool. crypto. asymmetric. AsymmetricAlgorithm ;
import cn. hutool. crypto. asymmetric. KeyType ;
import cn. hutool. crypto. asymmetric. RSA ; import java. security. KeyPair ;
public class RsaUtil { private static String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIquJdhq+eFMtAqLHbjb9LpAthtBcV/KFsE8PO9lejKIbE8VaQ67fElvZQR0MH+PirEH4nETHwgBIy0es7Ftuop3aG0nx4uqVeBvHHJnBO/kX/2oU013zoACpIyFj0zycYOnYW63fjhKjLS7vJxTE1cZugIAlSaSJ9DRr6rvI5YvAgMBAAECgYANKxIe5bGKii0CX0lYN1KaqPBh0CoCzX6xpJWoqbsbt0aJj9/k7TqVAoXWEW5VfnzN6JDA6e7lZK5BZ4vqih/PSFKAjh99+ZscrWbhpWbApdZKEJtpjWAqeO3wstBTWBYIJrbfI0QTAR3RsVLiZht85RKGHe6Osaz13rmxhn4oiQJBAMDHpdOgRhV+/qeIcX2TUJcAYjErN0Cjpp/djAyMiOZNuExbpx0edztOEYJujQJOoGDgCusLI4OW79kLSnWFvI0CQQC4KLPi0Sqh1deD4jRSiMfhg1GgpRkFhpevW9p4uMswPkb8He8B3ijCh7S+HWfDDbutO73iS399KIf2ULFDCjSrAkB9CsKf4wyycr4g0tqyVjRmzYPjWqGrFrtEZIKrvRgrMexB8w0RUSqLWBb5YFWsUm2qiIfsbzwo5WVLhgxU8FsdAkB26SlGIsNBXj/bk5svU8ykq9NwREqEy+RzwX6vBruEeiIBtx5V+WoeOdwWCgyu4NY3RGUWkqNOYHZyMKZU4GkjAkEAlAL09WVrvMnqBRfvyL9CTKV16nEQ3wjbWSW2AqAqwyQZ4xjRultsE0XFnyCzVylVTPArR5nhDIt+US7lqsyy0w==" ; private static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCKriXYavnhTLQKix242/S6QLYbQXFfyhbBPDzvZXoyiGxPFWkOu3xJb2UEdDB/j4qxB+JxEx8IASMtHrOxbbqKd2htJ8eLqlXgbxxyZwTv5F/9qFNNd86AAqSMhY9M8nGDp2Fut344Soy0u7ycUxNXGboCAJUmkifQ0a+q7yOWLwIDAQAB" ; public static void getKey ( ) { KeyPair pair = SecureUtil . generateKeyPair ( "RSA" ) ; privateKey = Base64 . encode ( pair. getPrivate ( ) . getEncoded ( ) ) ; System . out. println ( "私钥\t" + privateKey) ; publicKey = Base64 . encode ( pair. getPublic ( ) . getEncoded ( ) ) ; System . out. println ( "公钥\t" + publicKey) ; } public static String encrypt ( String text) { RSA rsa = new RSA ( AsymmetricAlgorithm . RSA_ECB_PKCS1 . getValue ( ) , privateKey, publicKey) ; return rsa. encryptBase64 ( text, KeyType. PublicKey ) ; } public static String decrypt ( String text) { RSA rsa = new RSA ( AsymmetricAlgorithm . RSA_ECB_PKCS1 . getValue ( ) , privateKey, publicKey) ; return rsa. decryptStr ( text, KeyType. PrivateKey ) ; } }