Home > Net >  C # in the System. The String into char * problem in C
C # in the System. The String into char * problem in C

Time:11-24

In a recent study on its technology run into a problem, about c # System. The String into a char * in c + +,
Because my encryption function is a standard C, can't switch to another language to write, but interface is C # to do, the user input plaintext, calling the underlying encryption function back to the C #, C by C # again saved to the SQL server database, the process can not change, so do programming is a bit difficult,
A and c # interface input clear
String plaintext=TextBox1. Text;
Encryption function called:
Encrypto (ref plaintext)
Written by encrypting function will be clear to the c + + DLL come back again,
Two, the c + + write data conversion function
Will first System: : String into a char *
Marshal: char * cplain=(char *) : StringToHGlobalAnsi (plaintext). ToPointer ();
Then C encryption function called
Native_c_encrypto - & gt; Native_encrypto (cplain, cipher, cplain_sz);
Cipher for the encrypted cryptograph and cipher is converted into c # String back to the interface:
Plaintext=Marshal: : PtrToStringAnsi ((IntPtr) cipher (char *));
Three, and then by the c # to write the database, these processes are not problems, data conversion, encryption, stored in the database are normal, but again by cipher text read from the database, and then decrypted shown in c # interface problems,
(a) to read from the cipher text can be read from the database of normal, see encrypted before reading is also the result of the thing, is the code, such as string "helloworld" encrypted into "Diao spaniel %? D, ", anyway is garbled,
String ciphertext=TextBox2. Text;
Decryption function:
Decrypto (ref ciphertext)
(2) the "Diao spaniel %? D, "into a char * format, it's wrong, by adjusting the converted string obviously is not the same as the encrypted string, even before and after the encryption of the length of the string, I called the encryption algorithm aes - CTR, so the encryption before and after the length of the string should be consistent, the conversion function is:
Marshal: char * cipher=(char *) : StringToHGlobalAnsi (ciphertext.) ToPointer ();
(3) to call C decryption decryption function, the results must be right, after the call to convert C # String shown in the interface:
Call C decryption function:
Native_c_encrypto - & gt; Native_decrypto (cipher, cplain cipher_sz);
Ciphertext=Marshal: : PtrToStringAnsi ((IntPtr) (char *) cplain);
According to what is below the code:
Hellow? ,
Four, the problem analysis:
Encryption function should be no problem, because I used a pure c + + write a local function test, test String helloworld, encryption can be normal, so you need to be char * in c + + to c # String conversion when out of the question, because the encrypted String is garbled, cannot use normal display character, want to ask next everybody have a good solution, do not need to display properly, just after the encryption and decryption right then, cannot have restrictions on the length of the String at the same time, at least 20 characters inside should be no problem, I test to decrypt the String length is 5 and no problem, but after more than five characters decryption is messed up,
Thank you all! If solved, late can you be more additional points again,

CodePudding user response:

Baidu "c # pinvoke marshaling the string"

Tip:
1. StringBuilder
2. The Charset character agreed

CodePudding user response:

Ps:

Cipher text can be read from the database of normal read, see encrypted before reading is also the result of the thing, is the code, such as string "helloworld" encrypted into "Diao spaniel %? D, "and it's the code

I want to say "it don't meet your expectations"?? "Helloworld" encrypted into "Diao spaniel %? D, "is that he is encrypted, become you don't understand, don't encryption?? We should think "hellowrold" encrypted into "hellowrold" is normal?
We can only say that
Diao spaniel %? D, can you decrypt into "hellowrold" is our concern

CodePudding user response:

In the way, the des algorithm is public, pure test you can use c + + implementation, and then call in c #, but since it is public des algorithm, then nature had des library in c #, you don't need to do with c + + from the project,

, of course, some people may say, is my own, using c + + algorithm to safety, but we say, any modern cryptography first sentence should be "the key is the security of the algorithm, the algorithm itself is not", so you see the "eight key, a 128 - bit key, a 256 - bit key, 512 key" discussion, rather than "XXX algorithm discussion"

CodePudding user response:

Encrypted string to be in the form of binary, or converted to hexadecimal equivalent string or base64 preservation, otherwise it will cause loss of information, such as:



Or

  •  Tags:  
  • C#
  • Related