PeculiarVentures / webcrypto-liner

webcrypto-liner is a polyfill that let's down-level User Agents (like IE/Edge) use libraries that depend on WebCrypto. (Keywords: Javascript, WebCrypto, Shim, Polyfill)
MIT License
148 stars 26 forks source link

Android mobile can't work #70

Closed cqupt-yifanwu closed 4 years ago

cqupt-yifanwu commented 4 years ago
<html>
    <head>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.7.0/polyfill.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/asmCrypto/2.3.2/asmcrypto.all.es5.min.js"></script>
            <script src="https://cdn.rawgit.com/indutny/elliptic/master/dist/elliptic.min.js"></script>
            <!-- Crypto -->
            <script src="webcrypto-liner.shim.js"></script>
    </head>
    <body>
        <p id="text"></p>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/asmCrypto/2.3.2/asmcrypto.all.es5.min.js"></script>
        <script src="https://cdn.rawgit.com/indutny/elliptic/master/dist/elliptic.min.js"></script>
        <script> 
        const getECDHSecretKey = async (serverKey) => {
            alert(crypto.subtle);
            alert(crypto.subtle.generateKey);
            if (!serverKey) {
                serverKey = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEAVd5buXuu/ZPIVcZCegnRjC4GNgGI550Idq4vA0IxQtYGJa4ik1hCTNAoYzT2xqjtWQYTVYExyLnt9C+Eg0FzA==';
            }
            // 生成ecdh密钥对
            const {publicKey, privateKey} = await crypto.subtle.generateKey(
                {
                    name: "ECDH",
                    namedCurve: "P-256",
                },
                false,
                ["deriveKey", "deriveBits"]
            );

            alert('publicKey|' + publicKey);

            // 将公钥导出为spki编码格式
            const spkiPublicKey = await crypto.subtle.exportKey(
                "spki",
                publicKey
            );

            alert('spkiPublicKey|' + spkiPublicKey);

            // arrayBuffer转base64
            const _arrayBufferToBase64 = buffer => {
                var binary = '';
                var bytes = new Uint8Array(buffer);
                var len = bytes.byteLength;
                for (var i = 0; i < len; i++) {
                    binary += String.fromCharCode( bytes[i]);
                }
                return window.btoa(binary);
            }

            // base64格式公钥
            const base64PublicKey = _arrayBufferToBase64(spkiPublicKey);

            alert('base64PublicKey|' + base64PublicKey);

            // base64转arrayBuffer
            const _base64ToArrayBuffer = base64 => {
                var binary_string = window.atob(base64);
                var len = binary_string.length;
                var bytes = new Uint8Array(len);
                for (var i = 0; i < len; i++) {
                    bytes[i] = binary_string.charCodeAt(i);
                }
                return bytes.buffer;
            }

            serverKey = _base64ToArrayBuffer(serverKey);

            // 生成 服务端生成公钥的 CryptoKey
            const serverCryptoKey = await crypto.subtle.importKey(
                "spki",
                serverKey,
                {
                    name: "ECDH",
                    namedCurve: "P-256",
                },
                false,
                []
            )

            // 计算密钥
            const secretKey = await crypto.subtle.deriveKey(
                {
                    name: "ECDH",
                    namedCurve: "P-256",
                    public: serverCryptoKey
                },
                privateKey,
                {
                    name: "AES-GCM",
                    length: 256,
                },
                true,
                ["encrypt", "decrypt"]
            );

            // 导出密钥 arraybuffer格式
            const bufferSecretKey = await crypto.subtle.exportKey(
                "raw",
                secretKey
            );

            // 共享密钥转为base64
            document.getElementById('text').innerHTML = _arrayBufferToBase64(bufferSecretKey);
            alert( _arrayBufferToBase64(bufferSecretKey));
            return  _arrayBufferToBase64(bufferSecretKey);
        }

        getECDHSecretKey();
</script>
</body>
</html>

crypto.subtle.generateKey has someting wrong

rmhrisk commented 4 years ago

We have tested on android mobile In the past and it does work for us.

We will need more information to help, for example what version of Android? Which browser and what version? What error we’re you having?

cqupt-yifanwu commented 4 years ago

i found elliptic.js not import successful , i resolved it, thx