Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C or C Plus Plus Can't encrypt/encrypt with Java and C# data #8

Open
Skycat opened this issue Nov 2, 2022 · 4 comments
Open

C or C Plus Plus Can't encrypt/encrypt with Java and C# data #8

Skycat opened this issue Nov 2, 2022 · 4 comments

Comments

@Skycat
Copy link

Skycat commented Nov 2, 2022

In C source code:

#define FIXED_KEY \
    size_t i;\
    uint8_t fixed_key[16];\
    memcpy(fixed_key, key, 16);\
    for (i = 0; (i < 16) && (fixed_key[i] != 0); ++i);\
    for (++i; i < 16; ++i) fixed_key[i] = 0;\

the last line will clear the key which after value 0, but in Java and C# source code the fixed_key not do this:
Java:

 private static byte[] fixKey(byte[] key) {
        if (key.length == 16) return key;
        byte[] fixedkey = new byte[16];
        if (key.length < 16) {
            System.arraycopy(key, 0, fixedkey, 0, key.length);
        }
        else {
            System.arraycopy(key, 0, fixedkey, 0, 16);
        }
        return fixedkey;
    }

C#:

private static Byte[] FixKey(Byte[] key) {
        if (key.Length == 16) return key;
        Byte[] fixedkey = new Byte[16];
        if (key.Length < 16) {
            key.CopyTo(fixedkey, 0);
        }
        else {
            Array.Copy(key, 0, fixedkey, 0, 16);
        }
        return fixedkey;
}
@Skycat Skycat changed the title C or C Plus Can't encrypt/encrypt with Java and C# data C or C Plus Plus Can't encrypt/encrypt with Java and C# data Nov 2, 2022
@andot
Copy link
Member

andot commented Nov 2, 2022

Java and C# will automatically fill the fixedkey with 0 during initialization.

@Skycat
Copy link
Author

Skycat commented Nov 2, 2022

Java and C# will automatically fill the fixedkey with 0 during initialization.

这样就导致C中使用key = {1, 0, 1, 0}, 通过FIXED_KEY后最终加密key会变成{1, 0, 0, 0, 0....},那在C中加密的结果在Java和C#中使用key = {1, 0, 1, 0}无法解密,因为Java和C#中的最终key为{1, 0, 1, 0, 0.........}

@andot
Copy link
Member

andot commented Nov 7, 2022

嗯,C 语言这里是把 \0 作为字符串结束符使用的。要满足你的要求,需要跟 #3 中说的那样,加两个支持设置 binary key 的函数才行。

@Skycat
Copy link
Author

Skycat commented Nov 7, 2022

嗯,当时没注意到这个issue,为了避免改实现库的代码我现在是在key上去掉0值去规避掉不同语言上兼容这个问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants