RSA非堆成加密的密钥生成以及加密和解密

RSA非堆成加密有公钥和私钥,公钥用于加密,私钥用于解密

一,密钥生成

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.util.Map;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class CreateSecretKey {

    public static String getPublicKey(Map<String , Object> keyMap) throws Exception{
        Key key = (Key)keyMap.get("PUBLIC_KEY");
        return encryptBASE64(key.getEncoded()) ;
    }
    
    public static String getPrivateKey(Map<String, Object> keyMap) throws Exception{
        Key key = (Key)keyMap.get("PRIVATE_KEY");
        return encryptBASE64(key.getEncoded()) ; 
    }
    
    public static byte[] decryptBASE64(String key) throws Exception{
        return new BASE64Decoder().decodeBuffer(key);
    }
    
    public static String encryptBASE64(byte[] key) throws Exception{
        return new BASE64Encoder().encode(key);
    }
    
    public static RsaPairKey initKey() throws Exception{
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        keyPairGen.initialize(2048);
        KeyPair keyPair = keyPairGen.generateKeyPair();
        return new RsaPairKey(keyPair) ;
    }
    
    static class RsaPairKey{
        private String privateKey ; 
        
        private String publicKey ;
        
        public RsaPairKey(KeyPair pairKey) {
            try {
                this.privateKey = encryptBASE64(pairKey.getPrivate().getEncoded()) ;
                this.publicKey = encryptBASE64(pairKey.getPublic().getEncoded());
            } catch (Exception e) {
            }
        }
        
        public String getPrivateKey() {
            return this.privateKey ;
        }
        
        public String getPublicKey() {
            return this.publicKey ;
        }
    }

}

二,加密和解密

需要先引入依赖

        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>

import org.jasypt.encryption.StringEncryptor;

import com.ulisesbocchio.jasyptspringboot.encryptor.SimpleAsymmetricConfig;
import com.ulisesbocchio.jasyptspringboot.encryptor.SimpleAsymmetricStringEncryptor;
import com.ulisesbocchio.jasyptspringboot.util.AsymmetricCryptography.KeyFormat;

public class GenerateEncryUtils {

    public static void main(String[] args) {
        SimpleAsymmetricConfig config = new SimpleAsymmetricConfig();
        config.setKeyFormat(KeyFormat.PEM);
        config.setPublicKey("-----BEGIN PUBLIC KEY-----\r\n" + 
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoIogBPmly4Z104zmD5VL\r\n" + 
                "gs5sj2Iu5DNWXX2KNIRgqnW6eIfNHPWOoXA6Nq5Waw064iA32LeOVGger4GdZp9t\r\n" + 
                "FZSdk9kGwCekzy0nAC/pIfjJsCmsi9X7Xs+tVRnettkjonVhqI2hmrN2iO73oBDk\r\n" + 
                "SVZm0sDEXInu13RbXvKoUo1RTDCRdSMO8uYV/I+ygiV7/IdwEiB4k5c9j4RmWmMj\r\n" + 
                "4dWk9miHjdMzziAnRUc3BzeRTdu4DIW9oupV8JYwK6FNonv3pbVU4dOfzAdPQzHI\r\n" + 
                "4odCBCE4oXrVoQpn/c9JzYVG0/qOijyeHFgAF9q19XqG7nALHVgS3m2skyrFwBa6\r\n" + 
                "BwIDAQAB\r\n" + 
                "-----END PUBLIC KEY-----\r\n" + 
                "");
        StringEncryptor encryptor = new SimpleAsymmetricStringEncryptor(config);
        String username = "username";
        String password = "password";
        String enName = encryptor.encrypt(username);
        String enPsw = encryptor.encrypt(password);
        System.out.println("Encrypted username :"+enName);
        System.out.println("Encrypted password :"+enPsw);
    }
    
    public static String encryString(String pubKey , String value) {
        SimpleAsymmetricConfig config = new SimpleAsymmetricConfig();
        config.setKeyFormat(KeyFormat.PEM);
        config.setPublicKey(pubKey);
        StringEncryptor encryptor = new SimpleAsymmetricStringEncryptor(config);
        return encryptor.encrypt(value);
    }
    
    public static String decryString(String priKey , String value) {
        SimpleAsymmetricConfig config = new SimpleAsymmetricConfig();
        config.setKeyFormat(KeyFormat.PEM);
        config.setPrivateKey(priKey);
        StringEncryptor encryptor = new SimpleAsymmetricStringEncryptor(config);
        return encryptor.decrypt(value);
    }
}
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值