简体   繁体   English

如何将加密数据从C ++传输到Java

[英]How to transfer encrypt data from c++ to java

I've a simple encryption program using JNI (JAVA , C++ . Briefly : I pass a simple string into c++ program. C++ program encrypts that string for me and return cipher string. I'll print ciphertext for a client. 我有一个使用JNI (JAVA,C ++的简单加密程序。简要地说:我将一个简单的字符串传递给c ++程序。C++程序会为我对该字符串进行加密并返回密文字符串。我将为客户端打印密文。

Problem : When I encrypt in c++ and pass it to java some characters cannot be transfered. 问题:当我使用c ++加密并将其传递给java时,某些字符无法传输。 but when I print it using c++ console it's right. 但是当我使用c ++控制台打印它时是正确的。 one of that character is : 该字符之一是:

â , ü

cipher text in c++ console : C ++控制台中的密文:

ozmzâx~w|(~i}|64ío(üuvt*po~,s|*nY|(yy~(ktztskk|sgX ozmzâx〜W |(〜I} |64ío(üuvt*婆〜,S | *纽约|(YY〜(ktztskk | SGX

cipher text that transfered in java: 在Java中传输的密文:

ozmz?x~w|(~i}|64ío(?uvt*po~,s|*nY|(yy~(ktztskk|sgX ozmz X〜W |?(〜I} |?64ío(UVT *婆〜,S | *纽约|(YY〜(ktztskk | SGX

(â , ü is lost) (â,ü丢失)

So because of this fault when I want to decrypt the text, those characters don't decrypt correctly (but in c++ decrypt correctly!) 因此由于这个错误,当我想解密文本时,这些字符无法正确解密(但在c ++中正确解密!)

plain text : 纯文本 :

Hello, This is a test message for encryptor test, we will test it for our application 您好,这是加密测试的测试消息,我们将针对我们的应用程序对其进行测试

cipher text java : 密文java:

Rmvpy4*Prq},s u~m}x uos}iqi rybozmz?x~w|(~i}|64ᄀo(?uvt po~,s|*nY|(yy| (ktztskk|sgX Rmvpy4 * Prq},s u〜m } x uos} iqi rybozmz? x〜w |(〜i} | 64 ᄀ o(?uvt po〜,s | * nY |(yy |(ktztskk | sgX

decrypt text : 解密文本:

Hello, This is a test message for encrケptor test, キe キill test it for our application 您好,这是针对Encr ケ ptor测试的测试消息,请为我们的应用程序进行测试

(fail in "encryptor" and "we" and "will") what can I do? (“加密器”,“我们”和“将”失败)我该怎么办?

I transfer chars from c++ to java in jbyteArray format. 我将字符从c ++以jbyteArray格式传输到Java。

C++ CODE : C ++代码:

JNIEXPORT jbyteArray JNICALL Java_com_mf_dems_HelloJNI_encryptTest
(JNIEnv *env, jobject thisObject, jobject encryptorContext, jstring 
jInputBlock)
{

//get class
jclass encryptorContextClass = env->GetObjectClass(encryptorContext);

//get keyLength
jfieldID keyLengthFieldId = env->GetFieldID(encryptorContextClass, 
"keyLength", "I");
jint keyLength = env->GetIntField(encryptorContext, keyLengthFieldId);

//get blockLength
jfieldID blockLengthFieldId = env->GetFieldID(encryptorContextClass, 
"blockLength", "I");
jint blockLength = env->GetIntField(encryptorContext, blockLengthFieldId);

//get key
jfieldID keyFieldId = env->GetFieldID(encryptorContextClass, "key", "[C");
jobject jKeyArray =env->GetObjectField(encryptorContext, keyFieldId);
jchar *key =env->GetCharArrayElements((jcharArray)(jKeyArray), NULL);

//get inputBlock
const jchar *inputBlock = env->GetStringChars(jInputBlock, NULL);

jbyte *buf = new jbyte[blockLength];

unsigned int i=0;
for(i ; i< blockLength ; i++)
{
    buf[i] =((inputBlock[i] +10) ^ key[i] ^ key[i+15] ^ 0x11);
}

env->ReleaseStringChars(jInputBlock, inputBlock);
env->ReleaseCharArrayElements((jcharArray)(jKeyArray), key, 0);

jbyteArray jOut = env->NewByteArray(blockLength);
env->SetByteArrayRegion(jOut, 0, blockLength, buf);
delete [] buf;
return jOut;

} }

JAVA CODE : JAVA代码:

 EncryptorContext encryptorContext = new EncryptorContext();
    encryptorContext.setBlockLength(17);
    encryptorContext.setKeyLength(32);
    encryptorContext.setKey(new char[encryptorContext.getKeyLength()]);
    String plainText = "Hello, This is a test message for encryptor test, we will test it for our application.";
    String cipherText = "";
    //set key
    for (int i = 0; i < encryptorContext.getKeyLength(); i++) {
        encryptorContext.getKey()[i] = (char) (i + 1);
    }
    //ENCRYPT
    for (int i = 0; i < (plainText.length() -encryptorContext.getBlockLength()); i +encryptorContext.getBlockLength()) 
    {
       byte [] out = helloJNI.encryptTest(encryptorContext, 
       plainText.substring(i, i + encryptorContext.getBlockLength()));
       byte[] latin1 = new String(out, "ISO-8859-1").getBytes("UTF-8");
        for (byte b : out) {
            cipherText+=(char)b;
        }
    }
     System.out.println(cipherText);

The solution is to encode the encrypted output into a character based encoding. 解决方案是将加密输出编码为基于字符的编码。 On decryption first decode back to binary. 解密时,首先解码回二进制文件。 Some encryption implementations handle Base64 with an option, orbiter by default and others you will require you to do the encoding/decoding yourself. 一些加密实现通过选项(默认情况下是轨道器)处理Base64,而其他一些则需要您自己进行编码/解码。

Encryption is byte based, not character, not all byte values can be displayed/are characters. 加密是基于字节的,而不是字符,不是所有的字节值都可以显示/是字符。 The solution is to encode the encrypted binary data in a character encoding such as ASCII, two main methods are two common ways to represent binary bytes so they are displayable: Base64 (good for computers) and Hexadecimal (good for developers).. 解决方案是使用诸如ASCII之类的字符编码对加密的二进制数据进行编码,两种主要方法是表示二进制字节的两种常用方法,因此它们可以显示: Base64 (适用于计算机)和Hexadecimal (适用于开发人员)。

-more- -更多-
The two character displays: 显示两个字符:
ozmzâx~w|(~i}|64ío(üuvt*po~,s|*nY|(yy~(ktztskk|sgX
ozmz?x~w|(~i}|64ío(?uvt*po~,s|*nY|(yy~(ktztskk|sgX
are different because the two systems are using slightly different characters for some byte values, the lower one is using a ? 之所以有所不同,是因为两个系统在某些字节值上使用略有不同的字符,而下一个系统在使用? for values it does not have a glyph for. 对于值,它没有字形。

The plain text is 86 characters and with padding is 96 characters. 纯文本为86个字符,带填充的为96个字符。 This will encrypt tp 96 characters. 这将加密tp 96个字符。 The encrypted text is displaying 50 characters so 46 bytes are missing, these are byte values with no displayable value and/or the display terminated early. 加密的文本显示50个字符,因此缺少46个字节,这些字节是没有可显示值的字节值和/或显示提早终止。

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

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