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

Modulus/Exponent-based encryption wrong outcome #205

Open
itm-platform opened this issue Aug 24, 2021 · 0 comments
Open

Modulus/Exponent-based encryption wrong outcome #205

itm-platform opened this issue Aug 24, 2021 · 0 comments

Comments

@itm-platform
Copy link

Hi there,
I need to RSA-OAEP encrypt the following string
"{\"credentialData\":[{\"name\":\"username\", \"value\":\"TenantTwo\"},{\"name\":\"password\", \"value\":\"moss1KLOH5pont-bah\"}]}"

using the following exponent/modulus

exponent = 'AQAB';
modulus = '0R3Mmpn2LSnGiuFPMOldR/9hySiSsWKs01Oue+ecg0ZLKMepXArJ3gmCngBdQxcsocTXwyW725J/EDrIdgJ5DhMSVPevyvg3yiLKKFVFgG3q7Hbxf7NMgXHcXg3dgELO4TXHhRjrTzjlfnC2gaUHBGDteUJ0rc94zkvYUYPtAFJ8M4xSyoflRTeVxMgFAQRgo0C/pTZXh3Pr6YFBcQF2FoicpqT7jRUG0rQAJ7Cw1N61YXKU/ss+ofEywYXBKa/jrXxBhOOhTpgEkNbpxbDb4KAWxTv4CahMDsYIP6dH9+86pRfWisG9O5SYkUbHl+oUKdnwfW7SNvzYA1zcHXDSTQ==';

The goal is encrypting credentials to create a data source in a power bi gateway.

The correct result (obtained using a PowerShell script or C#) is this one

dbjZOiaP+lL6G+irfS+jSQOMuE4+PdgcBUTMVBe1294mnPtTHq224nySjA1KB7Vn+M16T0PrwFsHGZ3/tDQy3enJFf8Yun7MnDoGx7tWeqwg0wimS6scZoiUJ31pZ896ZhatOus3+QvJjBgS6sVktX50BTu66QaIqZ0DJLSfy/bh58lKGZ8O4YoI3UCDO/vtv4fUuOB+0qUHhUmPDyu/v6ydjwOfUF4tFE1QNSKMbrWSqhGZFAXxaoB1OVBAC+TzOEiXar5iq8AZ5hkiNoyeGmWHXXY7K3P+skbYUe8D6vxDpSkLd7Q0U8GcHuEwkrIMaeeW9LL6qpnPv6nl45piTg==AADFHJUs5X180+lTIZ6TiMtk/DDXVPLHuR/3R4UvIqESRe0JajaYj94Z/UAsblnO6ECmOss2ppWXqad8gv9JZEgIfxgHrNrhr40W0Eq1neMSJ6dcoOY7QczycNdiBjb4guHy5M+y2zO7o6UaQMtI+ZiQWC4pOYTHNgstzMhjF8uijOxwMaL7WrsAoj1FtiGaUCiC5FUJuBaxtYs+2L3szysq

Now, what I get from node-rsa is quite different

Cxlwo+i38IxfBvNvFmxFAlcdIAVM8QVQ40/+8xNMalLnmgaFf3A9w0TxXmjLWuZbNFAHSZzf3/XGSGQpo+bjZJMDooqoe/exXS5uF+EXS9m3EOOaY81wK7E6TR/BpNHoNnZeP/ng64usrTRucvmTTCj4tRjWt8y7XxE13zjceLPtKLwdo6dsdzdYfIiqFa8/1a15XPwq1pv6KuAWNY9ELzr5C2XPs8b7rOwggjmBL7hdIiDEVzW1SFeTTykbeHB7G9jlrcIiZR+7Pcq3CW4xTSMzUsrG1Ku1vg1Qo4fjpZJKNfE0q/VwMkRfBO3MLD2mf5KTTg/zO7rrz2xedUDmTQ==

This is the code I used

const encryptCredentials = () => {
    // using https://stackoverflow.com/questions/56961746/power-bi-api-post-datasource-errors-with-dmts-invalidencryptionalgorithmerror
    const nodeRSA = require("node-rsa");
    const credentials = "{\"credentialData\":[{\"name\":\"username\", \"value\":\"TenantTwo\"},{\"name\":\"password\", \"value\":\"moss1KLOH5pont-bah\"}]}";

    const exponentString = 'AQAB';
    const modulusString = '0R3Mmpn2LSnGiuFPMOldR/9hySiSsWKs01Oue+ecg0ZLKMepXArJ3gmCngBdQxcsocTXwyW725J/EDrIdgJ5DhMSVPevyvg3yiLKKFVFgG3q7Hbxf7NMgXHcXg3dgELO4TXHhRjrTzjlfnC2gaUHBGDteUJ0rc94zkvYUYPtAFJ8M4xSyoflRTeVxMgFAQRgo0C/pTZXh3Pr6YFBcQF2FoicpqT7jRUG0rQAJ7Cw1N61YXKU/ss+ofEywYXBKa/jrXxBhOOhTpgEkNbpxbDb4KAWxTv4CahMDsYIP6dH9+86pRfWisG9O5SYkUbHl+oUKdnwfW7SNvzYA1zcHXDSTQ==';

    const key = new nodeRSA();
    // key.generateKeyPair(); // tried this to upgrade to 2048

    const modulus = new Buffer(modulusString, 'base64');
    const exponent = new Buffer(exponentString, 'base64');

    const pubKey = key.importKey({ n: modulus, e: exponent }, 'components-public');

    const encrypted = pubKey.encrypt(credentials, 'base64');

    return encrypted
}
console.log(encryptCredentials())

In several posts, they say it may be due to a 512-key rather than 2048, but couldn't find a way to fix it.

Any ideas?

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

1 participant