2
我想使用java对RSA公钥加密DSA私钥。但是,当我这样做时,出现此错误:使用RSA公钥加密DSA私钥
javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:337)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
DSA和RSA密钥大小分别设置为1024和2048。我知道使用RSA我们不能加密超过RSA密钥大小的消息。但是,在这种情况下,DSA密钥大小小于RSA密钥大小。
我想这个问题与getEncode()函数有关,因为当我检查这个函数的返回值时,我明白了结果的大小是335字节。
我想知道我该如何解决这个问题? (我不想增加RSA密钥的大小)。我将DSA密钥大小设置为1024.为什么编码后DSA密钥大小的大小为335字节?
DSA和RSA密钥生成功能,以及RSA加密功能如下:
public static KeyPair generateDSAKey() {
KeyPair pair = null;
try {
KeyPairGenerator keyGen = KeyPairGenerator
.getInstance("DSA", "SUN");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024, random);
pair = keyGen.generateKeyPair();
} catch (Exception e) {
e.printStackTrace();
}
return pair;
}
public static KeyPair generateRSAKey() {
KeyPairGenerator kpg;
KeyPair kp = null;
try {
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
kp = kpg.genKeyPair();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return kp;
}
public static byte[] encryptRSA(byte[] msg, PublicKey pubKey) {
byte[] cipherData = null;
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
cipherData = cipher.doFinal(msg);
} catch (Exception e) {
e.printStackTrace();
}
return cipherData;
}
,我调用此函数与RSA公钥加密DSA密钥:
PrivateKey WSK = Crypto.generateDSAKey().getPrivate();
encWSK = encryptRSA(WSK.getEncoded(), RSAPublicKey);
或者他可以加密x值并发送未加密的域参数。 – 2013-05-13 21:19:45