Last active
April 29, 2024 09:42
-
-
Save myouju/8b867420cab56b43d513f4655d8a27bb to your computer and use it in GitHub Desktop.
encryption - encrypt / decrypt by ruby, python, and golang with AES-256-CFB
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
package main | |
import ( | |
"fmt" | |
"io" | |
"encoding/base64" | |
"crypto/rand" | |
"crypto/cipher" | |
"crypto/aes" | |
"crypto/md5" | |
"golang.org/x/crypto/pbkdf2" | |
) | |
const saltlen = 8 | |
const keylen = 32 | |
const iterations = 10002 | |
func encrypt(plaintext string, password string) string { | |
header := make([]byte, saltlen + aes.BlockSize) | |
salt := header[:saltlen] | |
if _, err := io.ReadFull(rand.Reader, salt); err != nil { | |
panic(err) | |
} | |
iv := header[saltlen:aes.BlockSize+saltlen] | |
if _, err := io.ReadFull(rand.Reader, iv); err != nil { | |
panic(err) | |
} | |
key := pbkdf2.Key([]byte(password), salt, iterations, keylen, md5.New) | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
panic(err) | |
} | |
ciphertext := make([]byte, len(header) + len(plaintext)) | |
copy(ciphertext, header) | |
stream := cipher.NewCFBEncrypter(block, iv) | |
stream.XORKeyStream(ciphertext[aes.BlockSize+saltlen:], []byte(plaintext)) | |
return base64Encode(ciphertext) | |
} | |
func decrypt(encrypted string, password string) string { | |
a, err := base64Decode([]byte(encrypted)) | |
if err != nil { | |
panic(err) | |
} | |
ciphertext := a | |
salt := ciphertext[:saltlen] | |
iv := ciphertext[saltlen:aes.BlockSize+saltlen] | |
key := pbkdf2.Key([]byte(password), salt, iterations, keylen, md5.New) | |
block, err := aes.NewCipher(key) | |
if (err != nil) { | |
panic(err) | |
} | |
if len(ciphertext) < aes.BlockSize { | |
return "" | |
} | |
decrypted := ciphertext[saltlen+aes.BlockSize:] | |
stream := cipher.NewCFBDecrypter(block, iv) | |
stream.XORKeyStream(decrypted, decrypted) | |
return string(decrypted) | |
} | |
func base64Encode(src []byte) string { | |
return base64.StdEncoding.EncodeToString(src) | |
} | |
func base64Decode(src []byte) ([]byte, error) { | |
return base64.StdEncoding.DecodeString(string(src)) | |
} | |
func main() { | |
text := "maeno" | |
password := "pass" | |
enc_text := encrypt(text, password) | |
fmt.Println(enc_text) | |
fmt.Println(decrypt(enc_text,password)) | |
fmt.Println(decrypt("yzFQOv38nMY3bfY17mhDjtMlKqcK0gEJHHdON9k=\n",password)) | |
} |
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
import hashlib | |
import base64 | |
import Crypto.Random | |
import M2Crypto.EVP | |
PASSWORD = b"pass" | |
SALTLEN = 8 | |
KEYLEN = 32 | |
IVLEN = 16 | |
ITERATIONS = 10002 | |
ENCRYPT = 1 | |
DECRYPT = 0 | |
def encrypt(text): | |
salt = Crypto.Random.get_random_bytes(SALTLEN) | |
dk = hashlib.pbkdf2_hmac("md5", PASSWORD, salt, ITERATIONS, IVLEN+KEYLEN) | |
key = dk[:KEYLEN] | |
iv = dk[KEYLEN:] | |
cipher = M2Crypto.EVP.Cipher('aes_256_cfb', key, iv, ENCRYPT) | |
encrypted = cipher.update(text) + cipher.final() | |
return base64.b64encode(salt+iv+encrypted) | |
def decrypt(encrypted): | |
data = base64.b64decode(encrypted) | |
salt = data[:SALTLEN] | |
data = data[SALTLEN:] | |
dk = hashlib.pbkdf2_hmac("md5", PASSWORD, salt, ITERATIONS, IVLEN+KEYLEN) | |
key = dk[:KEYLEN] | |
iv = dk[KEYLEN:] | |
cipher = M2Crypto.EVP.Cipher('aes_256_cfb', key, iv, DECRYPT) | |
text = cipher.update(data) + cipher.final() | |
return text[IVLEN:] | |
if __name__ == '__main__': | |
a = encrypt("maeno") | |
print a | |
print decrypt(a) | |
print decrypt("ZRYc5tvMdbO7jdyygwLGythQ5+d4ts+laesOnhA=\n") | |
print decrypt("yaZNBg2XcOkQrIv4EfOeeujL9XG3QEd2RrujivU=") |
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
require 'openssl' | |
require 'base64' | |
$password = "pass" | |
$saltlen = 8 | |
$keylen = 32 | |
$iterations = 10002 | |
def aes256_encrypt(text) | |
cipher = OpenSSL::Cipher::Cipher.new("AES-256-CFB") | |
salt = OpenSSL::Random.random_bytes($saltlen) | |
key_iv = OpenSSL::PKCS5.pbkdf2_hmac($password, salt, $iterations, $keylen+cipher.iv_len, "md5") | |
cipher.key = key_iv[0, cipher.key_len] | |
cipher.iv = key_iv[cipher.key_len, cipher.iv_len] | |
cipher.encrypt | |
text = cipher.update(text) + cipher.final | |
text = salt + key_iv[cipher.key_len, cipher.iv_len] + text | |
Base64.encode64(text) | |
end | |
def aes256_decrypt(encrypted) | |
data = Base64.decode64(encrypted) | |
salt = data[0,$saltlen] | |
data = data[$saltlen, data.size] | |
cipher = OpenSSL::Cipher::Cipher.new("AES-256-CFB") | |
key_iv = OpenSSL::PKCS5.pbkdf2_hmac($password, salt, $iterations, $keylen+cipher.iv_len, "md5") | |
cipher.key = key_iv[0, cipher.key_len] | |
cipher.iv = key_iv[cipher.key_len, cipher.iv_len] | |
cipher.decrypt | |
data = cipher.update(data) + cipher.final | |
data[cipher.iv_len, data.size] | |
end | |
a = aes256_encrypt("maeno") | |
p a | |
p aes256_decrypt(a) | |
p aes256_decrypt("PBg3yu4a5Hh//HN1qUHHHiIiVgjUlMAbCw2mf6Y=") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment