【问题标题】:Encrypt a DSA private key with RSA public key使用 RSA 公钥加密 DSA 私钥
【发布时间】:2013-05-07 16:18:09
【问题描述】:

我想使用 java 使用 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 keygen函数以及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);

【问题讨论】:

    标签: java encryption rsa dsa


    【解决方案1】:

    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
    

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

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

    【讨论】:

    • 或者他可以加密x值并发送未加密的域参数。
    猜你喜欢
    • 2015-03-21
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    相关资源
    最近更新 更多