简介
RSA算法是一种非对称加密算法,与对称加密算法不同的是,RSA算法有两个不同的密钥,一个是公钥,一个是私钥。 下面我用hutool工具包来实现一个加解密示例代码
依赖Hutool工具包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
java示例,加密和解密
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import java.io.IOException;
import java.nio.charset.Charset;
public class HutoolAsymmetricCrypto {
public static String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKm99vGN+V9p1JhvLF3n903gTbAlsL5y0Sq5LpOpFUARobw+QVskXl6FPKfjvXT+OHCEE2I98njDWthyOy6dCRbcBFqhfOuKdjFdXpkTd1rdn9C6oJe3oFsHLAnznzoiPye1KiH0He/53jiDAHdG3QLPWI15g2QAjmA6Ua3s/4iTAgMBAAECgYAz52rDom5Gxs3DbvtaG7x7i8M1wdCx69oZ/lZHwMRGux2BumexvwU8UfGiRPmB9Nq9TSXAPMEQPGxjE0sVjd9hJdcAhsBOJTLfJrBD2XTLj5IEWb+nagLDSDCa58x4fduU8ENHqdlkTivimIryZqjHNXJiONO81KcRxsUx6158AQJBAOh/8H6NT2XBRaaaA5cUlWxK4xL1dugHBJdgGV+dGxu7KGAy6beiyI1PFK/iWsHL080n7z7EqLdnO1vxsZuUo8ECQQC65iJsgZW8qJoKKr9DC70b7VqmSezf0f0UfH6Fz6ICrq3vYWbfxrM7tQtyECzZvyXVaGvOV3EWNa0FUxmkFrFTAkBsbsbV7se52ELIqYnFG2d44xA2Vl7HZgaBJuOOkTIEY+w3uWcBKx1HsjsesH3NZmd4hghxiLx7FN09S2KfAdrBAkEAqNvBoD0X1wKRYbwrX1zct72jl/fUboefWwH7odPtMPU3Kr7anjU8vCYfOCfLDdQ4T25vcFbqUkGel9/zm5/xBwJAZ0YSZXg4WOaewDDssO3bcYotX/4DAjkrIc3QuiOuNejrTdTQQlCIGcXK2XOr77qELuaEEgsy87/kPmMCFXdcBg==";
public static String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpvfbxjflfadSYbyxd5/dN4E2wJbC+ctEquS6TqRVAEaG8PkFbJF5ehTyn4710/jhwhBNiPfJ4w1rYcjsunQkW3ARaoXzrinYxXV6ZE3da3Z/QuqCXt6BbBywJ8586Ij8ntSoh9B3v+d44gwB3Rt0Cz1iNeYNkAI5gOlGt7P+IkwIDAQAB";
public static void main(String[] args) {
//非对称加密
RSA rsa = new RSA();
//获得私钥(自动生成一个私钥)
//rsa.getPrivateKey();
System.out.println(rsa.getPrivateKeyBase64());
//获得公钥(自动生成一个公钥)
//rsa.getPublicKey();
System.out.println(rsa.getPublicKeyBase64());
//下面演示的事私钥加密,公钥解密。 也可以公钥加密,私钥解密
String input = encode();
decode(input);
}
/**
* 私钥加密
* @return
*/
public static String encode() {
RSA rs2 = new RSA(PRIVATE_KEY, null);
String str = Base64.encode(rs2.encrypt("王结胜", KeyType.PrivateKey));
System.out.println(str);
return str;
}
/**
* 公钥解密
* @param input
* @throws IOException
*/
public static void decode(String input) {
RSA rs2 = new RSA(null, PUBLIC_KEY);
String str = Base64.encode(rs2.decrypt(input, KeyType.PublicKey));
System.out.println(new String(Base64.decode(str), Charset.defaultCharset()));
}
}
输出
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKRx7DD7CdYpG88L7YIWxlAL4IPMUHWUuBxgK+OLFKuf+h2RHmoeQ7rlz6cFTrUNCR6eZlqJed0OBaiywyOq5XI978Lm4ZHFTlpftlG84Lppo5Sp6/vadO1xtN3BuI9/vVGpLrQvSUAqMIpOJ9S9KsMunvK58GmU/SmEzO5SIKkvAgMBAAECgYABVFGjFiE+08Q/laQZsIXX5DcWoBJo11PwGGLvPixjmstF0+qzOh1oDfvyW+hsx3cyzsSbDl0OCWzISVgRTYwsFexyQhIPeOET3lP+8YYFPkqfcFUqFB69MTMPCg0CIqAIipjkq8HWN5J9X91ppwgFh90ENahp6IgnzJmDpKCgwQJBAOD4pFlzYbdiuGRH3fxP848j/8OF1D7fHOAem3RXYwQAdEvQre3EGIPJmOsPeA87QairvuvLIrCQzCWJTTgW0CECQQC7IDNGDXp/HESf+ovCCFY2qURrkVjzOrmx1xX886/l3Sih9xHUEkMXa2oEGp/rdakzipo1P96MEDQIIvG6VI9PAkAPQGwL8GmQgC3WBpVDFLyDWMJvI/SGqqOPgiOx2HbK/6jituwDsYaj8JfgX7UY+0BxSetwmt3oMHjWlp9YjRthAkEAqAVrxLUJjuT1AajYoOsXbpFibCvgcNYEMxlsFutAv+drOrM8gO287GR4lyLJAYVu3mbPvIyS6bUK41ttLeALpQJBAMlyKnnYI+aX5zIBs5lmpo0KrMHlAb3vsgdA1d0b+80AZBE37XXvpoZsZdjSDVFtREV06ZtR/Aq7UEyl3Lw7tCc=
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCkceww+wnWKRvPC+2CFsZQC+CDzFB1lLgcYCvjixSrn/odkR5qHkO65c+nBU61DQkenmZaiXndDgWossMjquVyPe/C5uGRxU5aX7ZRvOC6aaOUqev72nTtcbTdwbiPf71RqS60L0lAKjCKTifUvSrDLp7yufBplP0phMzuUiCpLwIDAQAB
cYbaGVcod8C2xZE4dl8qpmSAinliIpR3q12zoOvCTHGlALVtIOmIzSt/5Wp/Y3kvpqTPCm9gjf2MrEtb6nPB23VJopfdwY2evVGknOzb1voEfanYx20X9bmUW5XF/Y1z8vg4Hj/Fm5iVjjfnepNto5uha7CAC5qHUIPxPSfgrHA=
王结胜
注意事项
可以用
RSA rsa = new RSA();
来生成一对公钥和私钥。(如果没有的情况下)当然也可以定义这个生成规则