Java DESede 加密,OpenSSL 等效

发布于 2024-12-29 12:41:45 字数 1683 浏览 1 评论 0原文

我在 Java 中使用 DESede 加密,并且想在 C++ 中使用 OpenSSL 的 API 进行相同的加密。

Java 代码:

    String secretKey = "abcdefghijklmnopqrstuvwx";

    byte[] bytes = secretKey.getBytes("UTF-8");
    SecretKey key = new SecretKeySpec(bytes, "DESede");

    Cipher ecipher = Cipher.getInstance("DESede");
    ecipher.init(Cipher.ENCRYPT_MODE, key);

    String input = "holahola1"; 

    byte[] utf8 = input.getBytes("UTF8");
    byte[] enc = ecipher.doFinal(utf8); // Encrypt

输出:[94, -45, 64, -105, 99, -55, 99, -42, 95, 122, 72, 117, -119, 95, -43, 40]

C++ 代码:

    unsigned char intext[256], outtext[256];

    memset(intext, 0, sizeof(intext));
    memset(outtext, 0, sizeof(outtext));

    char *output = (char *)outtext;
    DES_key_schedule keyschedc1;
    DES_key_schedule keyschedc2;
    DES_key_schedule keyschedc3;
    DES_cblock keyc1;
    DES_cblock keyc2;
    DES_cblock keyc3;

    DES_string_to_key("abcdefgh", &keyc1);
    DES_string_to_key("ijklmnop", &keyc2);
    DES_string_to_key("qrstuvwx", &keyc3);
    DES_set_key((DES_cblock *)keyc1, &keyschedc1);
    DES_set_key((DES_cblock *)keyc2, &keyschedc2);
    DES_set_key((DES_cblock *)keyc3, &keyschedc3); 

    strcpy((char *)intext, "holahola1");

    for (int i=0; i<16; i += 8)
    {
        DES_ecb3_encrypt((DES_cblock *)(intext + i),
                        (DES_cblock *)(outtext + i), 
                        &keyschedc1, &keyschedc2, 
                        &keyschedc3,DES_ENCRYPT);
    }              

输出:[ 30 55 9 -58 -18 -65 -66 123 -32 72 30 110 120 69 101 -81]

怎么了?有人可以帮助我吗?谢谢!。

I'm using DESede encryptation in Java and I want to make the same encryptation using the OpenSSL's API in C++.

Java code:

    String secretKey = "abcdefghijklmnopqrstuvwx";

    byte[] bytes = secretKey.getBytes("UTF-8");
    SecretKey key = new SecretKeySpec(bytes, "DESede");

    Cipher ecipher = Cipher.getInstance("DESede");
    ecipher.init(Cipher.ENCRYPT_MODE, key);

    String input = "holahola1"; 

    byte[] utf8 = input.getBytes("UTF8");
    byte[] enc = ecipher.doFinal(utf8); // Encrypt

output: [94, -45, 64, -105, 99, -55, 99, -42, 95, 122, 72, 117, -119, 95, -43, 40]

C++ code:

    unsigned char intext[256], outtext[256];

    memset(intext, 0, sizeof(intext));
    memset(outtext, 0, sizeof(outtext));

    char *output = (char *)outtext;
    DES_key_schedule keyschedc1;
    DES_key_schedule keyschedc2;
    DES_key_schedule keyschedc3;
    DES_cblock keyc1;
    DES_cblock keyc2;
    DES_cblock keyc3;

    DES_string_to_key("abcdefgh", &keyc1);
    DES_string_to_key("ijklmnop", &keyc2);
    DES_string_to_key("qrstuvwx", &keyc3);
    DES_set_key((DES_cblock *)keyc1, &keyschedc1);
    DES_set_key((DES_cblock *)keyc2, &keyschedc2);
    DES_set_key((DES_cblock *)keyc3, &keyschedc3); 

    strcpy((char *)intext, "holahola1");

    for (int i=0; i<16; i += 8)
    {
        DES_ecb3_encrypt((DES_cblock *)(intext + i),
                        (DES_cblock *)(outtext + i), 
                        &keyschedc1, &keyschedc2, 
                        &keyschedc3,DES_ENCRYPT);
    }              

output: [30 55 9 -58 -18 -65 -66 123 -32 72 30 110 120 69 101 -81]

What's wrong? Can someone help me? Thanks!.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半仙 2025-01-05 12:41:45

我得到了它。 Java DESede 加密,使用具有 ecb 模式的三重 DES,并使用 PKCS#5 填充。因此,我使用 PKCS#5 填充文本,它解决了所有问题。

C++ 代码示例:

unsigned char intext[256], outtext[256];

memset(intext, 0x07, sizeof(intext)); //PKCS#5 padding (See documentation)
memset(outtext, 0, sizeof(outtext));
char *output = (char *)outtext;

DES_key_schedule keyschedc1;
DES_key_schedule keyschedc2;
DES_key_schedule keyschedc3;
DES_cblock keyc1;
DES_cblock keyc2;
DES_cblock keyc3;

DES_set_key((DES_cblock *)"abcdefgh", &keyschedc1);
DES_set_key((DES_cblock *)"ijklmnop", &keyschedc2);
DES_set_key((DES_cblock *)"qrstuvwx", &keyschedc3); 

strcpy((char *)intext, "holahola1");

for (int i=0; i<16; i += 8)
{
    DES_ecb3_encrypt((DES_cblock *)(intext + i),
                    (DES_cblock *)(outtext + i), 
                    &keyschedc1, &keyschedc2, 
                    &keyschedc3,DES_ENCRYPT);
}              

I got it. Java DESede encryption, uses a triple DES with ecb mode, and uses a PKCS#5 padding. So, I fill my text, using PKCS#5, and it solve all.

Example C++ code:

unsigned char intext[256], outtext[256];

memset(intext, 0x07, sizeof(intext)); //PKCS#5 padding (See documentation)
memset(outtext, 0, sizeof(outtext));
char *output = (char *)outtext;

DES_key_schedule keyschedc1;
DES_key_schedule keyschedc2;
DES_key_schedule keyschedc3;
DES_cblock keyc1;
DES_cblock keyc2;
DES_cblock keyc3;

DES_set_key((DES_cblock *)"abcdefgh", &keyschedc1);
DES_set_key((DES_cblock *)"ijklmnop", &keyschedc2);
DES_set_key((DES_cblock *)"qrstuvwx", &keyschedc3); 

strcpy((char *)intext, "holahola1");

for (int i=0; i<16; i += 8)
{
    DES_ecb3_encrypt((DES_cblock *)(intext + i),
                    (DES_cblock *)(outtext + i), 
                    &keyschedc1, &keyschedc2, 
                    &keyschedc3,DES_ENCRYPT);
}              
流年已逝 2025-01-05 12:41:45

尝试使用 DES_ecb3_encrypt 代替,因为您使用的是 3 个单独的密钥。

Try DES_ecb3_encrypt instead as you are using 3 separate keys.

给妤﹃绝世温柔 2025-01-05 12:41:45
byte[] bytes = secretKey.getBytes("UTF-8");
SecretKey key = new SecretKeySpec(bytes, "**DESede/PKCS#5**");
byte[] bytes = secretKey.getBytes("UTF-8");
SecretKey key = new SecretKeySpec(bytes, "**DESede/PKCS#5**");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文