gpt4 book ai didi

java - 如何解决javax.crypto.IllegalBlockSizeException : Data must not be longer than 256 bytes

转载 作者:行者123 更新时间:2023-12-01 11:40:58 35 4
gpt4 key购买 nike

enter image description here我有一个 16 字节长度的 AES key 。我想将 16 字节 key 加密 3 次。在第一次迭代中, key 大小更改为 16 到 256 字节。在下一次迭代中, key 大小更改为 256 到 689 字节。下一次迭代会引发屏幕截图中显示的异常。这是因为我的 RSA 算法不支持长度超过 256 字节的 key .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 ;

}
}

最佳答案

您可以使用对称 key 来加密和解密要传输的数据 (> 256)。 RSA 只能加密一定范围内的数据(例如 256 字节),这取决于 RSA key 长度。

这意味着,如果您想要传输大于 256 字节的任何内容,则必须先传输 < 256 字节的对称 key ,以便您可以拥有以下内容:

  1. Generate a symmetric key (< 256 bytes)
  2. Encrypt symmetric key with RSA
  3. Transfer encrypted symmetric key
  4. Decrypt symmetric key with RSA
  5. Encrypt data (> 256 bytes) with symmetric key
  6. Transfer encrypted data
  7. Decrypt encrypted data with symmetric key

或者(同时传输加密对称 key 和加密数据)

  1. Generate a symmetric key (< 256 bytes)
  2. Encrypt symmetric key with RSA
  3. Encrypt data (> 256 bytes) with symmetric key
  4. Transfer encrypted symmetric key & encrypted data
  5. Decrypt symmetric key with RSA
  6. Decrypt encrypted data with symmetric key

关于java - 如何解决javax.crypto.IllegalBlockSizeException : Data must not be longer than 256 bytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29531739/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com