elliptic icon indicating copy to clipboard operation
elliptic copied to clipboard

curve25519 and BouncyCastle(bc) in c# Different compute shared secret

Open shamohai opened this issue 5 years ago • 2 comments

js `
private async GetX25519PublicKey() {

let ec = new ECC.ec('curve25519');
let keyPair = ec.genKeyPair();
let publicKey = keyPair.getPublic('hex');
let res: any = await this.TokenAPIPost('api/Account/GetX25519PublicKey', {
  publicKey: publicKey
});
let shareKey = keyPair
  .derive(ec.keyFromPublic(res.publicKey, 'hex').getPublic())
  .toString('hex');
return shareKey;

} ` Get the pubkey from the js lib library, pass it to the backend c # code, and compute the shared secret key.

c# code ` public static Dictionary<string, string> GenerateX25519Keys(string publicKey) {

        SecureRandom secureRandom = new SecureRandom();
        byte[] privateByte = new byte[X25519.ScalarSize];
        byte[] publicByte = new byte[X25519.PointSize];
        byte[] shareByte = new byte[X25519.PointSize];
        secureRandom.NextBytes(privateByte);
        X25519.ScalarMultBase(privateByte, 0, publicByte, 0);
        X25519.ScalarMult(privateByte, 0, Hex.Decode(publicKey), 0, shareByte, 0);
        Dictionary<string, string> dc = new Dictionary<string, string>();
        dc.Add("PublicKey", Hex.ToHexString(publicByte));
        dc.Add("PrivateKey", Hex.ToHexString(privateByte));
        dc.Add("ShareKey", Hex.ToHexString(shareByte));
        return dc;
    }

` The front-end js gets the pubkey of the back-end c # and compute the shared key.

Front-end and back-end shared keys are inconsistent?

shamohai avatar Dec 11 '19 09:12 shamohai

example:

js keys: publicKey:66b26b74ad89dc45ac1c5dde9a2894c03eb2c49824e98da5669ac672812a6c93 privateKey:066b2cc50751b56f4bcabf018282b960c5f72946ff7106544b886ecd143ef292 shareKey:3a6dd22cc425d812c5d001e1090a638d150c560b7aa10ebf9130e8e1e1944511

c# keys: publicKey: 7572f65ee139a14e6dbfb8b8ddda343de25c63324fd1157cd836a8f217b82576 privateKey: 3648ec90d4236303fe9732b948a95434f055a33c6873df8d62839414191af253 shareKey: 8c4f85546b5ca33e8e02602459aaf28881eb2fb1080662a84bf9fec29b946b1b

shamohai avatar Dec 11 '19 09:12 shamohai

Please see the issue #122

pranithan-kang avatar Aug 06 '20 08:08 pranithan-kang