现在想使用RSA+AES加密技术来进行加密解密求懂的大神来看看~~~~

Inuyashazc Inuyashazc 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在网上找到一个范例:
先说下文字描述:
一,A先生成一个对称秘钥,这个秘钥可以是随机生成的,
二,A用B的公钥加密第一步生成的这个对称秘钥
三,A把加密过的对称秘钥发给B
四,A用第一步生成的这个对称秘钥加密实际要发的消息
五,A把用对称秘钥加密的消息发给B
对于B
他先收到A发来的对称秘钥,这个秘钥是用B的公钥加密过的,所以B需要用自己的私钥来解密这个秘钥
然后B又收到A发来的密文,这时候用刚才解密出来的秘钥来解密密文

代码:

public class RSA {

public static final int KEYSIZE = 512;

private KeyPair keyPair;
private Key publicKey;
private Key privateKey;

/**
* 生成秘钥对
* @return
* @throws NoSuchAlgorithmException
*/
public KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
pairgen.initialize(RSA.KEYSIZE, random);
this.keyPair = pairgen.generateKeyPair();
return this.keyPair;
}

/**
* 加密秘钥
* @param key
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
*/
public byte[] wrapKey(Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.WRAP_MODE, this.privateKey);
byte[] wrappedKey = cipher.wrap(key);
return wrappedKey;
}

/**
* 解密秘钥
* @param wrapedKeyBytes
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
*/
public Key unwrapKey(byte[] wrapedKeyBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.UNWRAP_MODE, this.publicKey);
Key key = cipher.unwrap(wrapedKeyBytes, "AES", Cipher.SECRET_KEY);
return key;
}

public Key getPublicKey() {
return publicKey;
}

public void setPublicKey(Key publicKey) {
this.publicKey = publicKey;
}

public Key getPrivateKey() {
return privateKey;
}

public void setPrivateKey(Key privateKey) {
this.privateKey = privateKey;
}

}


我想先问下这里面有用到RES吗?(新人不太懂求解~)然后我有点看不太懂。
我看他的步骤是:先生成秘钥对。那我main应该先调用的就是generateKeyPair方法。
求解释我到底该如调用方法呢~~我想最后需要用一个秘钥方式来解密~~现在等先感谢各位了~
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
Inuyashazc
Web 开发 81,108 成员 341,720 内容