Last active
August 29, 2015 14:01
-
-
Save Kennyl/6496012ba95808f345fe to your computer and use it in GitHub Desktop.
Various Script Language for AES256
This file contains hidden or 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
var crypto = require('crypto'); | |
var data = "中あ"; | |
// data="bestwellgoodwill" | |
console.log('Original cleartext: ' + data); | |
var algorithm = 'aes-256-cbc'; | |
var key = 'hello'; | |
var clearEncoding = 'utf8'; | |
//var cipherEncoding = 'hex'; | |
//If the next line is uncommented, the final cleartext is wrong. | |
var cipherEncoding = 'binary';// or base64 | |
/*加密*/ | |
//var cipher = crypto.createCipher(algorithm, key); | |
var k = new Buffer("5D41402ABC4B2A76B9719D911017C59228B46ED3C111E85102909B1CFB50EA0F","hex"); | |
var iv = new Buffer("DE37085F7EDA3711FA2D9FE7E0FC29BE","hex"); | |
// var cipher = crypto.createCipher(algorithm, k); | |
var cipher = crypto.createCipheriv(algorithm, k,iv ); | |
// var cipherChunks = new Buffer(); | |
var cipherChunks=""; | |
cipherChunks +=cipher.update(data, clearEncoding, cipherEncoding); | |
cipherChunks +=cipher.final(cipherEncoding); | |
console.log(cipherEncoding + ' ciphertext: ' + cipherChunks); | |
/*解密*/ | |
var encMessage ="8TR+Yd1fOJS4nYhzd4Ixpw==" | |
encMessage=cipherChunks | |
var decipher = crypto.createDecipher(algorithm, key); | |
var plainChunks = []; | |
// for (var i = 0;i < encMessage.length;i++) { | |
plainChunks.push(decipher.update(encMessage, cipherEncoding, clearEncoding)); | |
// } | |
plainChunks.push(decipher.final(clearEncoding)); | |
console.log("UTF8 plaintext deciphered: " + plainChunks.join('')); | |
/* | |
# echo -n "中あ" | openssl enc -aes-256-cbc -nosalt -a -k hello -p -md md5 | |
# echo "8TR+Yd1fOJS4nYhzd4Ixpw==" | openssl enc -aes-256-cbc -d -nosalt -k hello -a | |
*/ |
This file contains hidden or 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
<?php | |
//5D41402ABC4B2A76B9719D911017C592 | |
$string = "中あ"; | |
// $string="bestwellgoodwill"; | |
$key =pack("H*","5D41402ABC4B2A76B9719D911017C59228B46ED3C111E85102909B1CFB50EA0F"); | |
$iv =pack("H*","DE37085F7EDA3711FA2D9FE7E0FC29BE"); | |
//$key =("5D41402ABC4B2A76B9719D911017C592"); | |
//$iv =("28B46ED3C111E85102909B1CFB50EA0F"); | |
$method = 'aes-256-cbc'; | |
// printf(" base 64 , \n"); | |
echo( openssl_encrypt ($string, $method,$key,false,$iv)); //base64 | |
// printf("\n\n binary , \n"); | |
// echo( openssl_encrypt ($string, $method,$key,OPENSSL_RAW_DATA,$iv)); // not base64 | |
printf("\n-------zero pad \n"); | |
echo(openssl_decrypt("8TR+Yd1fOJS4nYhzd4Ixpw==", $method, $key,OPENSSL_ZERO_PADDING,$iv)); | |
printf("\n----false\n"); | |
echo(openssl_decrypt("8TR+Yd1fOJS4nYhzd4Ixpw==", $method, $key,false,$iv)); | |
printf("\n-----\n"); | |
//echo(strtohex($pass)); | |
?> | |
echo -n "中あ" | openssl enc -aes-256-cbc -nosalt -a -k hello -p -md md5 | |
echo "8TR+Yd1fOJS4nYhzd4Ixpw==" | openssl enc -aes-256-cbc -d -nosalt -k hello -a |
This file contains hidden or 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
# -*- coding: utf-8-*- | |
from Crypto.Cipher import AES | |
import binascii | |
import base64 | |
def encrypt(message): | |
BS=16 | |
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) | |
#pythong need pad by ourself | |
# IV = Random.new().read(BLOCK_SIZE) | |
key =binascii.unhexlify("5D41402ABC4B2A76B9719D911017C59228B46ED3C111E85102909B1CFB50EA0F") | |
iv = binascii.unhexlify("DE37085F7EDA3711FA2D9FE7E0FC29BE") | |
aes = AES.new( key, AES.MODE_CBC ,iv) #will use 256 CBC if key is 256 long | |
# print pad(message) | |
return base64.b64encode(aes.encrypt(pad(message))) | |
def decrypt(message): | |
unpad = lambda s : s[0:-ord(s[-1])] | |
# unpad = lambda s: s[:-s[-1]] | |
key =binascii.unhexlify("5D41402ABC4B2A76B9719D911017C59228B46ED3C111E85102909B1CFB50EA0F") | |
iv = binascii.unhexlify("DE37085F7EDA3711FA2D9FE7E0FC29BE") | |
aes =AES.new(key,AES.MODE_CBC,iv) | |
return unpad(aes.decrypt(base64.b64decode(message))) | |
# print encrypt('bestwellgoodwill') | |
print encrypt('中あ') | |
print decrypt("8TR+Yd1fOJS4nYhzd4Ixpw==") | |
print encrypt('あ中') | |
print decrypt("UrPhJpBrhkIRrI0Gp6yATA==") | |
# print encrypt('あ中') | |
print decrypt(encrypt('あ中12323132312sdfsfsdfddffd2312')) | |
# echo -n "中あ" | openssl enc -aes-256-cbc -nosalt -a -k hello -p -md md5 | |
# echo "8TR+Yd1fOJS4nYhzd4Ixpw==" | openssl enc -aes-256-cbc -d -nosalt -k hello -a |
This file contains hidden or 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
require "openssl" | |
require "base64" | |
data ="中あ" | |
# data='bestwellgoodwill' | |
cipher = OpenSSL::Cipher::AES.new(256, :CBC) | |
cipher.encrypt | |
key = ["5D41402ABC4B2A76B9719D911017C59228B46ED3C111E85102909B1CFB50EA0F"].pack("H*") | |
iv = ["DE37085F7EDA3711FA2D9FE7E0FC29BE"].pack("H*") | |
cipher.key = key | |
cipher.iv =iv | |
encrypted = cipher.update(data) + cipher.final | |
# puts "encrypt" | |
puts [encrypted].pack("m").strip | |
eMessage = Base64.decode64("8TR+Yd1fOJS4nYhzd4Ixpw==" ) | |
eMessage = ("8TR+Yd1fOJS4nYhzd4Ixpw==" ).unpack("m").join() | |
cipher = OpenSSL::Cipher::AES.new(256, :CBC) | |
cipher.decrypt | |
cipher.key = key | |
cipher.iv =iv | |
puts (cipher.update(eMessage) + cipher.final) | |
# echo -n "中あ" | openssl enc -aes-256-cbc -nosalt -a -k hello -p -md md5 | |
# echo "8TR+Yd1fOJS4nYhzd4Ixpw==" | openssl enc -aes-256-cbc -d -nosalt -k hello -a |
This file contains hidden or 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
echo -n "中あ" | openssl enc -aes-256-cbc -nosalt -a -k hello -p -md md5 | |
echo "8TR+Yd1fOJS4nYhzd4Ixpw==" | openssl enc -aes-256-cbc -d -nosalt -k hello -a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment