Simple NodeJS Crypto with AS3Crypto Equivalent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NODEJS COFFEESCRIPT | |
=================== | |
crypto = require 'crypto' | |
method = 'des-ecb' | |
key = '12345678' | |
txt = "Hello World" | |
format = 'base64' | |
encrypt = ($text) -> | |
cipher = crypto.createCipheriv method, key, '' | |
crypted = cipher.update $text, 'utf8', format | |
crypted += cipher.final format | |
return crypted | |
decrypt = ($encrypted) -> | |
decipher = crypto.createDecipheriv method, key, '' | |
decrypted = decipher.update $encrypted, format, 'utf8' | |
decrypted += decipher.final 'utf8' | |
return decrypted | |
console.log 'Key = '+key | |
console.log "Text = "+txt | |
console.log "Encrypted = "+encrypt(txt) | |
console.log "Decrypted = "+decrypt(encrypt(txt)) | |
AS3 AS3CRYPTO | |
============= | |
package | |
{ | |
import flash.display.Sprite; | |
import flash.utils.ByteArray; | |
import com.hurlant.util.*; | |
import com.hurlant.crypto.*; | |
import com.hurlant.crypto.symmetric.*; | |
public class Main extends Sprite | |
{ | |
private var type:String = 'des-ecb'; | |
private var key:String = '12345678' | |
private var txt:String ='Hello World' | |
private var keyBytes:ByteArray = new ByteArray(); | |
private var pad:IPad = new PKCS5(); | |
private var cipher:ICipher; | |
public function Main() | |
{ | |
keyBytes.writeUTFBytes(key); | |
cipher = Crypto.getCipher(type, keyBytes, pad); | |
pad.setBlockSize(cipher.getBlockSize()); | |
trace("Key = "+key); | |
trace("Text = "+txt); | |
trace("Encrypted = "+encrypt(txt)); | |
trace("Decrypted = "+decrypt(encrypt(txt))); | |
} | |
private function encrypt(txt:String):String | |
{ | |
var data:ByteArray = new ByteArray(); | |
data.writeUTFBytes(txt); | |
cipher.encrypt(data); | |
return Base64.encodeByteArray(data); | |
} | |
private function decrypt(txt:String):String | |
{ | |
var data:ByteArray = Base64.decodeToByteArray(txt); | |
cipher.decrypt(data); | |
return Hex.toString(Hex.fromArray(data)); | |
} | |
} | |
} | |
RESULTS | |
======= | |
Key = 12345678 | |
Text = Hello World | |
Encrypted = 01Jk6AecyqldguNoHIO7dw== | |
Decrypted = Hello World | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment