应用通过TLS Socket进行加密数据传输
开发步骤
客户端TLS Socket流程:
1.import需要的socket模块。
2.绑定服务器IP和端口号。
3.双向认证上传客户端CA证书及数字证书;单向认证只上传CA证书,无需上传客户端证书。
4.创建一个TLSSocket连接,返回一个TLSSocket对象。
5.(可选)订阅TLSSocket相关的订阅事件。
6.发送数据。
7.TLSSocket连接使用完毕后,主动关闭。
// 创建一个(双向认证)TLS Socket连接,返回一个TLS Socket对象。
import socket from '@ohos.net.socket';
let tlsTwoWay: socket.TLSSocket = socket.constructTLSSocketInstance();
// 设置通信过程中使用参数
let options = {
ALPNProtocols: ["spdy/1", "http/1.1"],
// 连接到指定的IP地址和端口。
address: {
address: "192.168.xx.xxx",
port: xxxx, // 端口
family: 1,
},
// 设置用于通信过程中完成校验的参数。
secureOptions: {
key: "xxxx", // 密钥
cert: "xxxx", // 数字证书
ca: ["xxxx"], // CA证书
passwd: "xxxx", // 生成密钥时的密码
protocols: [socket.Protocol.TLSv12], // 通信协议
useRemoteCipherPrefer: true, // 是否优先使用对端密码套件
signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", // 签名算法
cipherSuite: "AES256-SHA256", // 密码套件
},
};
// 绑定本地IP地址和端口。
tlsTwoWay.bind({ address: '192.168.xxx.xxx', port: xxxx, family: 1 }, err => {
if (err) {
console.log('bind fail');
return;
}
console.log('bind success');
// 订阅TLS Socket相关的订阅事件
tlsTwoWay.on('message', value => {
console.log("on message")
let buffer = value.message
let dataView = new DataView(buffer)
let str = ""
for (let i = 0; i < dataView.byteLength; ++i) {
str += String.fromCharCode(dataView.getUint8(i))
}
console.log("on connect received:" + str)
});
tlsTwoWay.on('connect', () => {
console.log("on connect")
});
tlsTwoWay.on('close', () => {
console.log("on close")
});
// 建立连接
tlsTwoWay.connect(options).then(() => {
console.log('connect success');
// 发送数据
let sendBuf = 'client send to server...';
tlsTwoWay.send(sendBuf).then(() => {
console.log('client send ok');
}).catch((err: Object) => {
console.error('client send err: ' + JSON.stringify(err));
})
}).catch((err: Object) => {
console.log('connect failed ' + JSON.stringify(err));
});
// 连接使用完毕后,主动关闭。取消相关事件的订阅。
tlsTwoWay.close().then(() => {
console.log('close success');
}).catch((err: Object) => {
console.log('close failed ' + JSON.stringify(err));
});
tlsTwoWay.off('message');
tlsTwoWay.off('connect');
tlsTwoWay.off('close');
});
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
复制
本文参考引用HarmonyOS官方开发文档,基于API9。