Skip to content

Instantly share code, notes, and snippets.

@luikore
Created December 2, 2013 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luikore/7746976 to your computer and use it in GitHub Desktop.
Save luikore/7746976 to your computer and use it in GitHub Desktop.
AES, ruby vs java
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
class aes{
static byte[] ptext;
static SecretKeySpec key;
public static void main(String[] args) throws Exception {
ptext = read("ptext");
key = new SecretKeySpec(read("key"), "AES");
long t = System.currentTimeMillis();
doCiphers();
System.out.println("finished in " + (System.currentTimeMillis() - t) + " ms");
}
// wrap the loop inside a method to enable JIT
static void doCiphers() throws Exception {
for (int i=0; i<1000; i++) {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, key);
c.doFinal(ptext);
}
}
static byte[] read(String fileName) throws Exception {
RandomAccessFile f = new RandomAccessFile(fileName, "r");
byte[] b = new byte[(int)f.length()];
f.read(b);
f.close();
return b;
}
}
require "openssl"
$str = File.read(__FILE__) * 1000
$key = 'm23a0sdf' * 4
# store them for java testing
File.open 'ptext', 'w' do |f|
f << $str
end
File.open 'key', 'w' do |f|
f << $key
end
def crypt
1000.times do
c = OpenSSL::Cipher.new 'aes-256-cbc'
c.encrypt
c.key = $key
c.update $str
c.final
end
end
t = Time.now.to_f
crypt
diff = (Time.now.to_f - t) * 1000
puts "finished in #{diff} ms"
$ ruby -v
ruby 2.1.0dev (2013-04-17) [x86_64-darwin12.3.0]
$ ruby aes.rb
finished in 1003.9401054382324 ms
$ java -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06-451-11M4406)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01-451, mixed mode)
$ javac aes.java
$ java -d64 -server aes
finished in 4720 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment