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

Question. using with serde or string #11

Open
1tang2bang92 opened this issue Jun 27, 2022 · 1 comment
Open

Question. using with serde or string #11

1tang2bang92 opened this issue Jun 27, 2022 · 1 comment

Comments

@1tang2bang92
Copy link

Hello.
I only know the definition of Homomorphic Encryption.
And I want to use a cupcake for my personal project.

However, there are only a few simple examples. So, I leave a question here.
I want to use a homomorphic encryption with a string or serde data.
Can you give me some examples code?

And is it right to ask here?

@1tang2bang92 1tang2bang92 changed the title Question. using with serde and string Question. using with serde or string Jun 27, 2022
@Zennii
Copy link

Zennii commented Mar 21, 2023

I haven't spent a ton of time with the library but here's an example using strings that might help you out. It's extremely similar to the example code. Really all you need to do is convert what you want to encrypt into a vector of bytes, beware that it will be padded with zeros so maybe you should have zero as a termination or some other known way to find the end of your data again:

    let fv = cupcake::default();
    let (pk, sk) = fv.generate_keypair();

    let msg = "This is my encrypted message.".as_bytes().to_vec();

    // NOTE: The library seems to pad zeros for you, equivalent to the below, so you don't need to:
    // msg.resize(fv.n, 0);

    let ctv = fv.encrypt(&msg, &pk);

    // Decrypt our message...
    let pt_actual: Vec<u8> = fv.decrypt(&ctv, &sk);
    // ...and then cut off all the trailing zeros
    let pt_cut = pt_actual
        .into_iter()
        .take_while(|v| *v != 0)
        .collect::<Vec<_>>();

    let decrypted_msg = String::from_utf8(pt_cut).expect("Bad utf8");
    println!("Decrypted: {:?}", decrypted_msg);

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