【发布时间】:2015-06-14 10:04:37
【问题描述】:
我有一个 16 字节长度的 AES 密钥。我想加密 16 字节密钥 3 次。在第一次迭代中,密钥大小更改为 16 到 256 字节。下一次迭代,密钥大小更改为 256 到 689 字节。下一次迭代引发了屏幕截图中显示的异常。 这是因为我的 RSA 算法不支持超过 256 字节的密钥大小 .RSA加密源码如下所示
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.sql.SQLException;
import javax.crypto.Cipher;
public class RSAKeyPack implements Serializable {
private static final long serialVersionUID = 2L;
PublicKey publicKey;
PrivateKey privateKey;
//KeyPairGenerator keyPairGenerator;
transient KeyPairGenerator keyPairGenerator;
private void getGenerator() throws NoSuchAlgorithmException {
if (keyPairGenerator == null) {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024); //1024 used for normal securities
KeyPair keyPair = keyPairGenerator.generateKeyPair();
publicKey = keyPair.getPublic();
privateKey = keyPair.getPrivate();
}
}
public RSAKeyPack()
{
try {
getGenerator();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*try
{
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); //1024 used for normal securities
KeyPair keyPair = keyPairGenerator.generateKeyPair();
publicKey = keyPair.getPublic();
privateKey = keyPair.getPrivate();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}*/
}
public PublicKey getPublicKey() {
return publicKey;
}
public void setPublicKey(PublicKey publicKey) {
this.publicKey = publicKey;
}
public PrivateKey getPrivateKey() {
return privateKey;
}
public void setPrivateKey(PrivateKey privateKey) {
this.privateKey = privateKey;
}
public BigInteger getParamModulus(PublicKey publickey) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException
{
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec rsaPubKeySpec = keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
//RSAPrivateKeySpec rsaPrivKeySpec = keyFactory.getKeySpec(privateKey, RSAPrivateKeySpec.class);
System.out.println("PubKey Modulus : " + rsaPubKeySpec.getModulus());
return rsaPubKeySpec.getModulus();
}
public BigInteger getParamExponent(PublicKey publickey) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException
{
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec rsaPubKeySpec = keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
//RSAPrivateKeySpec rsaPrivKeySpec = keyFactory.getKeySpec(privateKey, RSAPrivateKeySpec.class);
System.out.println("PubKey Modulus : " + rsaPubKeySpec.getPublicExponent());
return rsaPubKeySpec.getPublicExponent();
}
public static PublicKey readPublicKey(BigInteger modulus,BigInteger exponent) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException{
//Get Public Key
RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey publicKey = fact.generatePublic(rsaPublicKeySpec);
return publicKey;
}
public byte[] encryptData(byte[] data,PublicKey pubKey) throws IOException {
byte[] encryptedData = null;
try {
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
System.out.println("data key length after encryption"+data.length);
encryptedData = cipher.doFinal(data);
System.out.println("data key length after encryption"+encryptedData.length);
} catch (Exception e) {
System.out.println("----------------ENCRYPTION ABANDONED!!!------------");
e.printStackTrace();
}
return (encryptedData);
}
public byte[] decryptData(byte[] data,PrivateKey privateKey) throws IOException {
byte[] descryptedData = null;
try {
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
descryptedData = cipher.doFinal(data);
System.out.println("data key length after decryption "+data.length);
} catch (Exception e) {
e.printStackTrace();
}
return descryptedData ;
}
}
【问题讨论】:
-
1.您还没有向我们展示产生错误的实际代码。不可能将第一个 RSA 密文反馈给 RSA 以获得第二个密文。 2.当您发布正确的问题示例时,请正确缩进您的代码。 3、为什么要做3次RSA?
标签: java encryption aes rsa