Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- <input type="file" style="height: 100%; width: 100%"></input> -->
<div id="dropbox">
  <div id="info">
	<p>Drop an archive here or <span style="text-decoration: underline">click here</span>.</p>
    <p id="details">Drop a <code>.zrp</code> archive to decrypt it or a <code>.zip</code> archive to encrypt it.</p>
    </div>
</div>
              
            
!

CSS

              
                #dropbox {
  height: calc(100vh - 2.5rem);
  border: dashed;
  border-color: lightgray;
  border-radius: 5px;
  margin: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  cursor: pointer;
}

#dropbox.dropping-down {
  animation-name: blackborder;
  animation-duration: 0.25s;
  animation-fill-mode: forwards
}

#info {
	margin: 0.5rem
}

p {
  margin-bottom: 1rem;
}

@keyframes blackborder {
  from {border-color: lightgray}
  to {border-color: black}
}
              
            
!

JS

              
                const uint8ToBuffer = array => array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
const bufferToUint8 = array => Array.from(new Uint8Array(array))
const updateDetails = info => document.querySelector("#details").innerHTML = info
const generateUint8 = length => (new Uint8Array(length)).map(() => Math.floor(Math.random() * 256))

let firstMessageTimeout

const timer = {
	time: 0,

	start() {
		return this.time = Date.now()
	},

	stop() {
		return this.time = Date.now() - this.time
	}
}

const concatUint8 = (...arrays) => {
		let totalLength = 0;
		for (const arr of arrays) {
				totalLength += arr.length;
		}
		const result = new Uint8Array(totalLength);
		let offset = 0;
		for (const arr of arrays) {
				result.set(arr, offset);
				offset += arr.length;
		}
		return result;
}

const download = (filename, blob) => {
		
	const blobUrl = URL.createObjectURL(blob)
	const tempLink = document.createElement("a")
	tempLink.href = blobUrl
	tempLink.download = filename
	document.body.appendChild(tempLink)
	// tempLink.dispatchEvent(
	// 	new MouseEvent('click', { 
	// 		bubbles: true, 
	// 		cancelable: true, 
	// 		view: window 
	// 	})
	// )
	tempLink.click()
	document.body.removeChild(tempLink)

}

const triggerFilePicker = () => new Promise((accept, reject) => {
	const tempInput = document.createElement("input")
	tempInput.type = "file"
	tempInput.accept = ".zip,.zrp"
	tempInput.oninput = (event) => {
		if (event.target.files.length === 0) reject()
		else {
			const file = event.target.files[0]
			accept(file)
		}
	}
	tempInput.dispatchEvent(
		new MouseEvent('click', { 
			bubbles: true, 
			cancelable: true, 
			view: window 
		})
	)
})

const stopPropagation = (e) => {
	e.stopPropagation()
	e.preventDefault()
}

const dropbox = document.querySelector("#dropbox")
dropbox.addEventListener("dragenter", (e) => {
	stopPropagation(e)
	dropbox.classList.add("dropping-down")
}, false)
dropbox.addEventListener("dragover", (e) => {
	stopPropagation(e)
	dropbox.classList.add("dropping-down")
}, false)
dropbox.addEventListener("dragleave", (e) => {
	stopPropagation(e)
	dropbox.classList.remove("dropping-down")
}, false)
dropbox.addEventListener("dragend", (e) => {
	stopPropagation(e)
	dropbox.classList.remove("dropping-down")
}, false)
dropbox.addEventListener("drop", (e) => {
	stopPropagation(e)
	dropbox.classList.remove("dropping-down")
	const dt = e.dataTransfer
	const files = dt.files
	handleFile(files[0])
}, false)
dropbox.addEventListener("click", async (e) => {
	stopPropagation(e)
	triggerFilePicker().then(file => handleFile(file))
}, false)

const handleFile = async (file) => {

	timer.start()
	
	const archiveFile = file
	const archiveName = file.name.substr(0, archiveFile.name.length - 4)
	const archiveType = file.name.substr(this.length - 3)
		
	let blob
	
	try {
		
		if (archiveType === "zrp") {
			
			const key = bufferToUint8(await archiveFile.slice(0, 16).arrayBuffer())
			const iv = key
			const cbc = new aesjs.ModeOfOperation.cbc(key, iv)
			updateDetails("Obtaining key...")

			const decryptedFile = cbc.decrypt(bufferToUint8(await archiveFile.slice(16).arrayBuffer())).slice(16)
			// DYK that C#'s implementation of AES (AESManaged) prepends a random data with a length of 16 bytes? Yeah, it's weird.
			updateDetails("Decrpyting...")
			
			download(`${archiveName}.zip`, new Blob([uint8ToBuffer(decryptedFile)], {type: 'application/zip'}))
			updateDetails(`<code>${archiveName}.zrp</code> has been decrypted to the <code>.zip</code> format in ${timer.stop()/1000} seconds.`)
			
		} else if (archiveType === "zip") {
		
			let key = generateUint8(16)
			const iv = key
			console.log(key)
			const cbc = new aesjs.ModeOfOperation.cbc(key, iv)
			updateDetails("Generating key...")
		
			let padding = new Uint8Array()
			if (await archiveFile.size % 16) {
				const paddingCount = 16 - archiveFile.size % 16
				padding = new Uint8Array(paddingCount)
				padding = padding.map(() => paddingCount)
			}
			updateDetails("Adding padding...")

			const encryptedFile = concatUint8(key, cbc.encrypt(concatUint8(generateUint8(16), bufferToUint8(await archiveFile.arrayBuffer()), padding)))
			// DYK that C#'s implementation of AES (AESManaged) prepends a random data with a length of 16 bytes? Yeah, it's weird.
			updateDetails("Encrypting...")
			
			download(`${archiveName}.zrp`, new Blob([uint8ToBuffer(encryptedFile)], {type: 'application/octet-stream'}))
			updateDetails(`<code>${archiveName}.zip</code> has been encrypted to the <code>.zrp</code> format in ${timer.stop()/1000} seconds.`)
			
		} else {
		
			throw "NotAnArchive"
		
		}

		} catch (e) {
	
		if (e === "NotAnArchive") updateDetails(`This is neither a <code>.zrp</code> archive nor a <code>.zip</code> archive!`)
		else updateDetails(`Something went wrong: ${e}`)
	
	} finally {
		
		try {clearInterval(firstMessageTimeout)}
		catch (e) {}
		firstMessageTimeout = setTimeout(() => updateDetails("Drop a <code>.zrp</code> archive to decrypt it or a <code>.zip</code> archive to encrypt it."), 10000)
		
	}	
	
}
              
            
!
999px

Console