简体   繁体   English

Java实现的C ++加密解密功能(Cryptix工具箱)

[英]Java implementation of C++ encrypt decrypt function(Cryptix toolbox)

Hi I am trying to encrypt and decrypt data that is used in 2 systems( The one is C++ and the other Java) I found a Demo project from code project : Encryption this is the c++ encrypt decrypt functions and it works when I implement the code using the functions : 嗨,我正在尝试对2个系统中使用的数据进行加密和解密(一个是C ++,另一个是Java),我从代码项目中找到了一个Demo项目: 加密这是c ++的加密解密功能,当我实现代码时可以使用使用功能:

void main()
{
    try
    {
        CRijndael oRijndael;
        oRijndael.MakeKey("abcdefghabcdefgh", "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, 16);
        char szDataIn[] = "Password12345678";

        char szDataOut[17] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

        oRijndael.EncryptBlock(szDataIn, szDataOut);

        cout << "[" << szDataIn << "]" << endl;
        cout << "[" << szDataOut << "]" << endl;
        memset(szDataIn, 0, 16);

        oRijndael.DecryptBlock(szDataOut, szDataIn);
        cout << "[" <<  szDataIn << "]"  << endl;

    }
    catch(exception& roException)
    {
        cout << roException.what() << endl;
    }
}

This gives me the following output in the console : 这在控制台中提供了以下输出:

在此处输入图片说明

The data is encrypted end decrypted.. 数据加密结束解密。

Could any one please help me or point me in the right direction for the Java implementation to encrypt and decrypt the same data getting the same results ( encrypt in C++ and then decrypt in Java and encrypt in java decrypt in c++)? 谁能帮我或指明正确的方向,以便Java实现对相同的数据进行加密和解密,以获得相同的结果(在C ++中进行加密,然后在Java中进行解密,在Java ++中使用c ++进行解密)? In the post they talk about Cryptix toolkit? 在帖子中,他们谈论了Cryptix工具包?

Thanks in advance. 提前致谢。

This Java code will encrypt/decrypt and produce the same output as your example encoded to hex. 此Java代码将加密/解密,并产生与编码为十六进制的示例相同的输出。 The output appears to be identical (and should be), but to be 100% sure you'll need to hex encode the output in your C++ example. 输出看起来是相同的(应该是),但是要100%确保您需要在C ++示例中对输出进行十六进制编码。

Do note though that your example will only encrypt and decrypt a single block (16 bytes). 请注意,尽管您的示例将仅加密和解密单个块(16字节)。 If you want more than that you'll need to use the CRijndael Encrypt and Decrypt methods (instead of EncryptBlock and DecryptBlock ). 如果您想要的还不止这些,则需要使用CRijndael EncryptDecrypt方法(而不是EncryptBlockDecryptBlock )。 The Java code below will work with both, no need to modify it. 下面的Java代码可以同时使用,而无需对其进行修改。

public static void main(String[] args) throws DecoderException, InvalidKeyException,
        NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException {
    //Hex encoding/decoding done with Apache Codec
    byte[] key = "abcdefghabcdefgh".getBytes();
    String text = "Password12345678";

    byte[] encrypted = encrypt(key, text.getBytes());
    byte[] decrypted = decrypt(key, encrypted);

    System.out.println("Text: " + text);
    System.out.println("Encrypted: " + Hex.encodeHexString(encrypted));
    System.out.println("Decrypted: " + new String(decrypted));
}

public static byte[] encrypt(byte[] key, byte[] unencrypted) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException{
    //Set up the cipher and encrypt
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
    byte[] encrypted = cipher.doFinal(unencrypted);

    return encrypted;
    }

public static byte[] decrypt(byte[] key, byte[] encrypted) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException{
    //Decrypt the encrypted text
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"));
    byte[] decrypted = cipher.doFinal(encrypted);

    return decrypted;
}

Output 输出量

Text: Password12345678
Encrypted: 6c7d800fad2bb8593db92847bbbdbeff
Decrypted: Password12345678

A big word of warning to you though, this encryption (ECB, no padding) is highly insecure and you should never use it in a real system. 提醒您的是,这种加密(ECB,无填充)是高度不安全的,因此您切勿在实际系统中使用它。 You should really be using CBC with an Initialization Vector and PKCS5 padding. 您确实应该将CBC与初始化向量和PKCS5填充一起使用。

Be sure not to export encrypted data from char array directly. 确保不要直接从char数组导出加密的数据。 Either use fixed size output or use byte buffer. 使用固定大小的输出或使用字节缓冲区。 It's mandatory because in encrypted string you may have byte combinations that could be interpreted as controll characters, fe \\0 mark. 这是强制性的,因为在加密的字符串中,您可能具有字节组合,这些字节组合可以解释为控制字符,fe \\0标记。 If that, printing output to file using ...<< szDataOut; 如果是这样,使用...<< szDataOut;输出打印到文件...<< szDataOut; may corrupt your encrypted string and unable to full read it by second application (no matter if java or c++). 可能会损坏您的加密字符串,并且无法被第二个应用程序完整读取(无论是Java还是c ++)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM