现在想使用RSA+AES加密技术来进行加密解密求懂的大神来看看~~~~
在网上找到一个范例:
先说下文字描述:
一,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方法。
求解释我到底该如调用方法呢~~我想最后需要用一个秘钥方式来解密~~现在等先感谢各位了~
[解决办法]
先生成一对密钥对—ES),然后用密钥对数据进行加密,RSA的公钥对密钥对加密,传递的数据以及用公钥加密过的密钥。数据接收方用RSA私钥对密钥进行解密,然后用密钥对数据解密。
[解决办法]
给你个连接 你看看http://www.cnblogs.com/thefeelingofsimple/archive/2012/11/29/2794724.html
我用是IOS ,网上一大堆