3

I want to encrypt the DSA secret key with the RSA public key using java. However, when I do so, I get this error:

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 and RSA key size is set to 1024 and 2048 respectively. I know that using RSA we cannot encrypt messages having size more than the RSA key size. However, in this case, DSA key size is less than RSA key size.

I guess the problem is related to the getEncode() function because when I checked the return value of this function, I understood that the size of result is 335 byte.

I want to know how I can fix this problem? (I do not want to increase RSA the key size). I set DSA key size to 1024. Why DSA key size has the size of 335 byte after encoding?

DSA and RSA keygen functions as well as RSA encryption functions are as follow:

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;
}

and I call this function for encrypting DSA key with RSA public key:

PrivateKey WSK = Crypto.generateDSAKey().getPrivate();
encWSK = encryptRSA(WSK.getEncoded(), RSAPublicKey);
4

1 回答 1

2

DSA 私钥包含算法参数以及x值。以下是打印到标准输出的私钥示例:

Sun DSA Private Key 
parameters:
    p:
    fd7f5381 1d751229 52df4a9c 2eece4e7 f611b752 3cef4400 c31e3f80 b6512669
    455d4022 51fb593d 8d58fabf c5f5ba30 f6cb9b55 6cd7813b 801d346f f26660b7
    6b9950a5 a49f9fe8 047b1022 c24fbba9 d7feb7c6 1bf83b57 e7c6a8a6 150f04fb
    83f6d3c5 1ec30235 54135a16 9132f675 f3ae2b61 d72aeff2 2203199d d14801c7
    q:
    9760508f 15230bcc b292b982 a2eb840b f0581cf5
    g:
    f7e1a085 d69b3dde cbbcab5c 36b857b9 7994afbb fa3aea82 f9574c0b 3d078267
    5159578e bad4594f e6710710 8180b449 167123e8 4c281613 b7cf0932 8cc8a6e1
    3c167a8b 547c8d28 e0a3ae1e 2bb3a675 916ea37f 0bfa2135 62f1fb62 7a01243b
    cca4f1be a8519089 a883dfe1 5ae59f06 928b665e 807b5525 64014c3b fecf492a

x:     1f853beb d6e30242 cd12bd28 e7055830 22ac43a8

您可以简单地加密x但是假设您的收件人已经知道算法参数pqg

或者您可以发送加密的x值和未加密的参数(感谢 GregS)。

于 2013-05-13T19:28:55.830 回答