Searched all over on this. When using Rust openssl crate is it possible to return the encrypted Vec as a UTF-8 string or does the PKCS1 padding prevent this indefinitely? I would be sending the encrypted data back to the user from a .NET/C# Web in a http API call so a string would be preferable.
#[no_mangle]
pub extern "C" fn rsa_encrypt(public_key: *const c_char, data_to_encrypt: *const c_char) -> *mut c_char {
let public_key_string = unsafe {
assert!(!public_key.is_null());
CStr::from_ptr(public_key)
}.to_str().unwrap();
let data_to_encrypt_string = unsafe {
assert!(!data_to_encrypt.is_null());
CStr::from_ptr(data_to_encrypt)
}.to_str().unwrap();
let rsa = Rsa::public_key_from_pem(public_key_string.as_bytes()).unwrap();
let mut buf: Vec<u8> = vec![0; rsa.size() as usize];
rsa.public_encrypt(data_to_encrypt_string.as_bytes(), &mut buf, Padding::PKCS1).unwrap();
return CString::new(String::from_utf8(buf).unwrap()).unwrap().into_raw()
}
Tried to export as a Vec but that doesn't satisfy transforming to a string on the C# side.