Java 使用RSA加密图像时出错
我需要用RSA加密大小为151*15的图像 这是加密图像文件的java代码Java 使用RSA加密图像时出错,java,image,encryption,rsa,public-key-encryption,Java,Image,Encryption,Rsa,Public Key Encryption,我需要用RSA加密大小为151*15的图像 这是加密图像文件的java代码 import javax.crypto.Cipher; plaintext = time; cipher = Cipher.getInstance('RSA'); keygen = java.security.KeyPairGenerator.getInstance('RSA'); keyPair = keygen.genKeyPair(); cipher.init(Cipher.ENCRYPT_MODE, keyPai
import javax.crypto.Cipher;
plaintext = time;
cipher = Cipher.getInstance('RSA');
keygen = java.security.KeyPairGenerator.getInstance('RSA');
keyPair = keygen.genKeyPair();
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate())
plaintextUnicodeVals = uint16(plaintext)
plaintextBytes = typecast(plaintextUnicodeVals, 'int8')
ciphertext = cipher.doFinal(plaintextBytes);
这是要加密的图像文件
我犯了以下错误
发生Java异常:
javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
请给我提示或程序,以便我能朝正确的方向接近
谢谢您需要使用AES加密大数据。RSA无法加密大于密钥大小的数据。 因此,您可以使用RSA加密AES密钥,并使用AES(256位)加密整个图像(即,为每个图像生成不同的AES密钥) 此外,如果要将图像分割为117字节大小的大量块并逐个加密,RSA速度非常慢,因此不适合加密大型数据
例如:
public static byte[] encrypt(byte[] data) {
try {
KeyPair keyPair = initalizeKeyPair();
final javax.crypto.Cipher rsa = javax.crypto.Cipher.getInstance("RSA");
rsa.init(javax.crypto.Cipher.ENCRYPT_MODE, keyPair.getPublic());
SecureRandom random = new SecureRandom();
final byte[] secretKey = new byte[16];
random.nextBytes(secretKey);
final javax.crypto.Cipher aes = javax.crypto.Cipher.getInstance("AES");
SecretKeySpec k = new SecretKeySpec(secretKey, "AES");
aes.init(javax.crypto.Cipher.ENCRYPT_MODE, k);
final byte[] ciphedKey = rsa.doFinal(secretKey);
final byte[] ciphedData = aes.doFinal(data);
byte[] result = new byte[256 + ciphedData.length];
System.arraycopy(ciphedKey, 0, result, 0, 256);
System.arraycopy(ciphedData, 0, result, 256, ciphedData.length);
return result;
} catch (... e) {
throw new SomeException(e);
}
}
您需要使用AES加密大数据。RSA无法加密大于密钥大小的数据。 因此,您可以使用RSA加密AES密钥,并使用AES(256位)加密整个图像(即,为每个图像生成不同的AES密钥) 此外,如果要将图像分割为117字节大小的大量块并逐个加密,RSA速度非常慢,因此不适合加密大型数据
例如:
public static byte[] encrypt(byte[] data) {
try {
KeyPair keyPair = initalizeKeyPair();
final javax.crypto.Cipher rsa = javax.crypto.Cipher.getInstance("RSA");
rsa.init(javax.crypto.Cipher.ENCRYPT_MODE, keyPair.getPublic());
SecureRandom random = new SecureRandom();
final byte[] secretKey = new byte[16];
random.nextBytes(secretKey);
final javax.crypto.Cipher aes = javax.crypto.Cipher.getInstance("AES");
SecretKeySpec k = new SecretKeySpec(secretKey, "AES");
aes.init(javax.crypto.Cipher.ENCRYPT_MODE, k);
final byte[] ciphedKey = rsa.doFinal(secretKey);
final byte[] ciphedData = aes.doFinal(data);
byte[] result = new byte[256 + ciphedData.length];
System.arraycopy(ciphedKey, 0, result, 0, 256);
System.arraycopy(ciphedData, 0, result, 256, ciphedData.length);
return result;
} catch (... e) {
throw new SomeException(e);
}
}
我看不到加密文本图像的值,而只是加密文本(显然低于117字节)。你能告诉我这个思考过程吗?不要删除一个,再问一个几乎相同的问题。@CoreyOgburn我用matlab将图像读入矩阵X。它的大小是151*15=2265。然后我将矩阵X转换成1*2265的行向量,这是给定代码中的时间输入。我看不到加密文本图像的值,而只是加密文本(显然低于117字节)。你能告诉我这个思考过程吗?不要删除一个,再问一个几乎相同的问题。@CoreyOgburn我用matlab将图像读入矩阵X。它的大小是151*15=2265。然后我将矩阵X转换成1*2265的行向量,这是给定代码中的时间输入。实际上我必须使用RSA,尽管这需要时间。这只是一个演示项目。如果您告诉我如何在java中分割图像。您需要建立一个列表,其中每个元素由大小不超过117字节的元素组成,然后对每个元素进行加密,然后将它们连接成一个字节[]。但相信我,这太过分了。使用AES:它得到美国政府的批准,非常安全。实际上我必须使用RSA,尽管这需要时间。这只是一个演示项目。如果你告诉我如何用java分割图像。你需要建立一个列表,其中每个元素由大小不超过117字节的元素组成,然后对每个元素进行加密,然后将它们连接成一个字节[]。但相信我,这太过分了。使用AES:它得到美国政府的批准,非常安全。