Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

RSA Encryption -- Same Key Different Results

See more RSA Examples

The RSA encryption algorithm produces different results for each call, even when encrypting the same data with the same key. Decryption however, will produce the correct results. This example demonstrates.

*** The reason this occurs is that RSA encryption uses PKCS1 v1.5 padding, and this padding scheme uses random bytes. It is random bytes in the padding that causes the result to be different each time.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const rsa = try chilkat.Rsa.init();
    defer rsa.deinit();

    const pub_key_xml = "<RSAPublicKey><Modulus>xxyv1RDPU0MvfFIIa98HppXdcuI7zSu8uIqyGAy/VoxPvxZFX0acajznvjVRHipHbpcO6ryo2LwXUPf89qOqLb3Qd1lfD2ZnH+TQ6MZXNxfFRxTpTUd+tTR4EBYpd2t6kzq8ZRJYLdlviaMQQqUEwR54k7Op5HJYVKUcHIkP1xE=</Modulus><Exponent>AQAB</Exponent></RSAPublicKey>";

    const priv_key_xml = "<RSAKeyValue><Modulus>xxyv1RDPU0MvfFIIa98HppXdcuI7zSu8uIqyGAy/VoxPvxZFX0acajznvjVRHipHbpcO6ryo2LwXUPf89qOqLb3Qd1lfD2ZnH+TQ6MZXNxfFRxTpTUd+tTR4EBYpd2t6kzq8ZRJYLdlviaMQQqUEwR54k7Op5HJYVKUcHIkP1xE=</Modulus><Exponent>AQAB</Exponent><P>4cpW9fvG99Jsz8/AO7PDHTl+pPRAglksrR2kClLV2g9DEeFe/bvmCxLUgMCJ+0eGQ1zA6aA7McKr13zTQ7jKpQ==</P><Q>4cCS/kFlq/P1ExF37Fkh4pCodOEGutepLEG7Q/KljT3ZGlAY+2l8fGu4f+hrkUuGoFl7NOMaJflULoPIgQaq/Q==</Q><DP>lkjcSsvzqh3YKRXJiLNkyf3rypV8noYGU4+oEOsDxilkZfFRDafUPUiiQrRk4ui/d/SzvozU+ZDuWfaOk8PatQ==</DP><DQ>SYCD25i7W8Mwdibn3uIecEAdOQDTSh5RjIFSUYs9b8FFYJXXrHPp/jCsf6jS7RmkGa1Iui1/JAIL8KEjtS7QmQ==</DQ><InverseQ>EDAJa3TpNdPQ3GIdBpnTgFTQY5A60DcszsUW/iCYoXQdPVJ9BLBxVTe9jiLzGuNuzLkVBwQlCy0Bf84hACRV9A==</InverseQ><D>cMFdDYKkddlRNczaugOmOH8b1egpx2liSPs6GYZ2gFObAXJiPK8m+r6c2ckls7hrlUP0DZhi4cG6Tn7xANb0Ek17P7QquVhQYOmFy/YHzm+IJbcwwq7pJHhZBhtcjyXqfUZ+BADGE//GQbrSVwVltpOj5KcxG88NAprLn2MMxfE=</D></RSAKeyValue>";

    const pub_key = try chilkat.PublicKey.init();
    defer pub_key.deinit();
    pub_key.loadFromString(pub_key_xml) catch {
        std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
        return;
    };

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    priv_key.loadXml(priv_key_xml) catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    rsa.usePublicKey(pub_key) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    // Encrypt a string and return the encrypted data base64-encoded:
    rsa.setEncodingMode("base64");

    const plain_text = "RSA gives different results with each call, weird but OK";

    var use_private_key: bool = false;
    const encrypted_str1 = try rsa.encryptStringENC(alloc, plain_text, use_private_key);
    std.debug.print("{s}\n", .{encrypted_str1});

    // Do it again. The results are different...
    const encrypted_str2 = try rsa.encryptStringENC(alloc, plain_text, use_private_key);
    std.debug.print("{s}\n", .{encrypted_str2});

    // Now decrypt both strings, and the results are correct
    // in both cases:
    const rsa2 = try chilkat.Rsa.init();
    defer rsa2.deinit();
    rsa2.usePrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    rsa2.setEncodingMode("base64");
    use_private_key = true;

    const decrypted_str1 = try rsa2.decryptStringENC(alloc, encrypted_str1, use_private_key);
    std.debug.print("{s}\n", .{decrypted_str1});

    const decrypted_str2 = try rsa2.decryptStringENC(alloc, encrypted_str2, use_private_key);
    std.debug.print("{s}\n", .{decrypted_str2});
}