Created
July 10, 2013 05:48
-
-
Save cowboyrushforth/5963752 to your computer and use it in GitHub Desktop.
Java<->Ruby AES-256-CBC PKCS5 Working 2 way encryption
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
# RUBY | |
require 'openssl' | |
require 'digest/sha2' | |
require 'json' | |
def bin2hex(str) | |
str.unpack('C*').map{ |b| "%02X" % b }.join('') | |
end | |
def hex2bin(str) | |
[str].pack "H*" | |
end | |
# the encryption in ruby | |
# you can provide salt and iv, or not | |
# if not it will be generated for you | |
def encrypt(salt=nil, iv=nil, payload, pwd) | |
cipher = OpenSSL::Cipher::Cipher.new('AES-256-CBC') | |
cipher.encrypt | |
if salt.nil? | |
salt = OpenSSL::Random.random_bytes(8) | |
else | |
salt = hex2bin(salt) | |
end | |
if iv.nil? | |
iv = cipher.random_iv | |
else | |
iv = hex2bin(iv) | |
end | |
key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pwd, salt, 1024, cipher.key_len) | |
cipher.key = key | |
iv = cipher.random_iv | |
cipher.iv = iv | |
cipher.padding = 1 | |
encrypted_binary = cipher.update(payload) + cipher.final | |
return bin2hex(salt), bin2hex(iv), bin2hex(encrypted_binary) | |
end | |
# the decryption | |
# you must provide the salt, iv, and encrypted paylaod | |
# you need the password too, but that can be in config | |
def decrypt(salt, iv, encrypted_payload, pwd) | |
cipher = OpenSSL::Cipher::Cipher.new('AES-256-CBC') | |
cipher.decrypt | |
key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pwd, hex2bin(salt), 1024, cipher.key_len) | |
cipher.key = key | |
cipher.iv = hex2bin(iv) | |
cipher.padding = 1 | |
plaintext = cipher.update(hex2bin(encrypted_payload)) | |
plaintext << cipher.final | |
return plaintext | |
end | |
# JAVA | |
import javax.crypto.Cipher; | |
import javax.crypto.Mac; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.security.AlgorithmParameters; | |
import java.security.spec.KeySpec; | |
import javax.crypto.Cipher; | |
import javax.crypto.SecretKey; | |
import javax.crypto.SecretKeyFactory; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.PBEKeySpec; | |
import javax.crypto.spec.SecretKeySpec; | |
class EncryptorDecryptor { | |
public static String bin2hex(byte[] data) | |
{ | |
if (data==null) | |
{ | |
return null; | |
} | |
int len = data.length; | |
String str = ""; | |
for (int i=0; i<len; i++) { | |
if ((data[i]&0xFF)<16) | |
str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF); | |
else | |
str = str + java.lang.Integer.toHexString(data[i]&0xFF); | |
} | |
return str; | |
} | |
public static byte[] hex2bin(String str) { | |
if (str==null) { | |
return null; | |
} else if (str.length() < 2) { | |
return null; | |
} else { | |
int len = str.length() / 2; | |
byte[] buffer = new byte[len]; | |
for (int i=0; i<len; i++) { | |
buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16); | |
} | |
return buffer; | |
} | |
} | |
public static String encrypt(String salt, String iv, String plaintext, String pwd) throws Exception { | |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); | |
KeySpec spec = new PBEKeySpec(pwd.toCharArray(), hex2bin(salt), 1024, 256); | |
SecretKey tmp = factory.generateSecret(spec); | |
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); | |
byte[] iv_bytes = hex2bin(iv); | |
String value = new String(iv_bytes); | |
IvParameterSpec ivspec = new IvParameterSpec(iv_bytes); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
cipher.init(Cipher.ENCRYPT_MODE, secret, ivspec); | |
byte[] encrypted = cipher.doFinal(plaintext.getBytes()); | |
return new String(bin2hex(encrypted)); | |
} | |
public static String decrypt(String salt, String iv, String payload, String pwd) throws Exception { | |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); | |
KeySpec spec = new PBEKeySpec(pwd.toCharArray(), hex2bin(salt), 1024, 256); | |
SecretKey tmp = factory.generateSecret(spec); | |
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); | |
byte[] iv_bytes = hex2bin(iv); | |
String value = new String(iv_bytes); | |
IvParameterSpec ivspec = new IvParameterSpec(iv_bytes); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
cipher.init(Cipher.DECRYPT_MODE, secret, ivspec); | |
byte[] payload_bytes = hex2bin(payload); | |
byte[] decrypted = cipher.doFinal(payload_bytes); | |
return new String(decrypted); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
delete line #33