简要教程
【案例简介】
#Brandis:为每个人提供端到端加密 此应用程序旨在说明端到端安全连接的易用性加密可以在现代web浏览器中实现,只需使用少量的JavaScript代码。Brandis本身不实现加密;相反,它依赖于[Web加密API](https://www.w3.org/TR/WebCryptoAPI/)由浏览器提供,并向此API公开一个用户界面允许非程序员使用它。
要开始,请使用顶部的按钮生成公钥/私钥对,然后按照那里的指示去做。此应用程序的源代码位于公共域中,可以在上访问[GitHub](https://github.com/brandis-project/brandis). 我们建议你将此页面下载到您的计算机并脱机使用。如果你是个程序员,您可以检查源代码以检查后门。
**注**:目前该应用程序仅适用于最新的桌面版本火狐,谷歌浏览器。
Brandis主要用于演示;它是用更少的时间组合起来的
【案例截图】
【核心代码】
let alreadyGenerated = false;
async function generateKeypair() {
if (alreadyGenerated)
return;
alreadyGenerated = true;
const keyParams = {
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: "SHA-256" },
};
try {
const key = await crypto.subtle.generateKey(keyParams, true, ["encrypt", "decrypt"]);
const publicKey = await crypto.subtle.exportKey("jwk", key.publicKey);
const privateKey = await crypto.subtle.exportKey("jwk", key.privateKey);
document.getElementById("public-key").value = JSON.stringify(publicKey, null, " ");
document.getElementById("private-key").value = JSON.stringify(privateKey, null, " ");
} catch (e) {
handleError(e);
}
}
const rsaOptions = { name: "RSA-OAEP", hash: { name: "SHA-256" }, };
const CHUNK_SIZE = 190;
async function encryptMesage() {
const publicKeyElement = document.getElementById("encrypt-public-key");
const inputElement = document.getElementById("encrypt-input");
const outputElement = document.getElementById("encrypt-output");
let rawPublicKey;
try {
rawPublicKey = JSON.parse(publicKeyElement.value);
} catch (e) {
handleError("Invalid public key");
return;
}
try {
const plaintextBuffer = stringToArrayBufferUTF16(inputElement.value);
const publicKey = await crypto.subtle.importKey("jwk", rawPublicKey,
rsaOptions, false, ["encrypt"]);
const encryptedChunks = [];
for (let i = 0; i < plaintextBuffer.byteLength; i += CHUNK_SIZE) {
const partialSize = Math.min(CHUNK_SIZE, plaintextBuffer.byteLength - i);
encryptedChunks.push(await crypto.subtle.encrypt(rsaOptions,
publicKey, plaintextBuffer.slice(i, i + partialSize)));
}
const encryptedBuffer = joinBuffers(encryptedChunks);
outputElement.value = toBase64(encryptedBuffer);
} catch (e) {
handleError(e);
}
}


