<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - aes algorithm regarding]]></title>
		<link>http://railsforum.com/viewtopic.php?id=51558</link>
		<description><![CDATA[The most recent posts in aes algorithm regarding.]]></description>
		<lastBuildDate>Mon, 10 Dec 2012 19:29:01 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[aes algorithm regarding]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=157467#p157467</link>
			<description><![CDATA[<p>Hi </p><p>I was planning to convert Java code to ruby script </p><p>JAVACODE</p><p> private static final String PK = &quot;037629a6&quot;;<br />&nbsp; &nbsp; private static final String EK = &quot;Td-b6vsXyJoeDzmCXe40GrT_B4fszAAA&quot;;</p><p>&nbsp; &nbsp; private static String encrypt(String secret){<br />&nbsp; &nbsp; &nbsp; &nbsp;String base64_encrypted_secret = null; <br />&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;Secret with key=&quot;+base64_encrypted_secret);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; String secret_with_key = String.format(&quot;%s:%s&quot;, secret, PK);<br />&nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;Secret with key=&quot;+secret_with_key);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; String secret_with_padding = pad(secret_with_key);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;Secret with padding=&quot;+pad(secret_with_padding));</p><p>//&nbsp; &nbsp; &nbsp; &nbsp; // Gen a 16 byte IV<br />&nbsp; &nbsp; &nbsp; &nbsp; try {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] iv = new byte[]{<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;iv=&quot;+iv);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] ek = EK.getBytes();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SecretKeySpec keyspec = new SecretKeySpec(Base64.decodeBase64(EK.getBytes()), &quot;AES&quot;);&nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cipher cipher = Cipher.getInstance(&quot;AES/CBC/NoPadding&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cipher.init(Cipher.ENCRYPT_MODE, keyspec, paramSpec);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;keyspec=&quot;+cipher);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logln(String.format(&quot;secret_with_padding = %s&quot;, secret_with_padding));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;byte[] encrypted_value = cipher.doFinal(secret_with_padding.getBytes());<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;logln(String.format(&quot;%s&quot;, encrypted_value));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String encrypted_secret = String.format(&quot;%s%s&quot;, new String(iv), new String(encrypted_value));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;Encrypted Secret=&quot;+encrypted_secret);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;Decrypt&quot;+decrypt(base64_encrypted_secret));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;logln(encrypted_secret);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;base64_encrypted_secret = Base64.encodeBase64String(encrypted_secret.getBytes());<br />&nbsp; &nbsp; &nbsp; &nbsp; } catch(Exception e){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />//&nbsp; &nbsp; &nbsp; &nbsp; //<br />&nbsp; &nbsp; &nbsp; &nbsp; return base64_encrypted_secret;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; private static String pad(String data){<br />&nbsp; &nbsp; &nbsp; &nbsp; int paddingBytesNeeded = (16 - (data.length() % 16));<br />&nbsp; &nbsp; &nbsp; &nbsp; logln(String.format(&quot;paddingBytesNeeded = %d&quot;, paddingBytesNeeded));<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; if(paddingBytesNeeded == 0) paddingBytesNeeded = 16;<br />&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; paddingBytesNeeded; i++) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = String.format(&quot;%s%c&quot;, data, (char)paddingBytesNeeded);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logln(data);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;Encrypted&nbsp; data=&quot;+data);<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; //<br />&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;Encrypted padding=&quot;+data);<br />&nbsp; &nbsp; &nbsp; &nbsp; return data;<br />&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; RUBY CODE</p><br /><p>require &quot;openssl&quot;<br />require &quot;digest&quot;<br />require &#039;base64&#039;</p><p>module FuzeEncryption<br />&nbsp; def FuzeEncryption.encrypt(secret)<br />&nbsp; &nbsp; base64_encrypted_secret = nil<br />&nbsp; &nbsp; puts base64_encrypted_secret</p><p>&nbsp; &nbsp; #secret with key<br />&nbsp; &nbsp; secret_with_key = &quot;xxxxxxxxx:037629a6&quot;<br />&nbsp; &nbsp; puts secret_with_key</p><p>&nbsp; &nbsp; #secret with padding<br />&nbsp; &nbsp; secret_with_padding = FuzeEncryption.pad(secret_with_key)<br />&nbsp; &nbsp; puts secret_with_padding</p><p>&nbsp; &nbsp; #iv<br />&nbsp; &nbsp; iv = [ 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07]<br />&nbsp; &nbsp; puts iv.to_s</p><p>&nbsp; &nbsp; #ek<br />&nbsp; &nbsp; ek = &quot;Td-b6vsXyJoeDzmCXe40GrT_B4fszAAA&quot;<br />&nbsp; &nbsp; keyspec = Base64.decode64(ek)<br />&nbsp; &nbsp; puts&nbsp; keyspec</p><p>&nbsp; &nbsp; #cipher<br />&nbsp; &nbsp; cipher = OpenSSL::Cipher::Cipher.new(&quot;aes-128-cbc&quot;)<br />&nbsp; &nbsp; cipher.encrypt<br />&nbsp; &nbsp; cipher.key = keyspec<br />&nbsp; &nbsp; cipher.iv = iv.to_s<br />&nbsp; &nbsp; puts cipher</p><p>&nbsp; &nbsp; #encrypted value<br />&nbsp; &nbsp; encrypted_value = &quot;&quot;<br />&nbsp; &nbsp; encrypted_value =cipher.update(secret_with_padding)<br />&nbsp; &nbsp; encrypted_value = cipher.final<br />&nbsp; &nbsp; puts encrypted_value</p><p>&nbsp; &nbsp; #encrypted secret<br />&nbsp; &nbsp; encrypted_secret = &quot;#{iv.to_s}#{encrypted_value.to_s}&quot;<br />&nbsp; &nbsp; base64_encrypted_secret = Base64.encode64(encrypted_secret)<br />&nbsp; &nbsp; puts base64_encrypted_secret<br />&nbsp; end<br />&nbsp; def FuzeEncryption.pad(data)</p><p>&nbsp; &nbsp; paddingBytesNeeded = (16 - (data.length % 16))<br />&nbsp; &nbsp; unless paddingBytesNeeded == 0<br />&nbsp; &nbsp; &nbsp; paddingBytesNeeded = 16<br />&nbsp; &nbsp; end<br />&nbsp; &nbsp; for i in 0..paddingBytesNeeded<br />&nbsp; &nbsp; &nbsp; data = &quot;#{data}#{paddingBytesNeeded}&quot;<br />&nbsp; &nbsp; end<br />&nbsp; &nbsp; return data</p><p>&nbsp; end <br />end</p><br /><p>Can any body tell me what Am I doing wrong.</p><p>arti</p>]]></description>
			<author><![CDATA[dummy@example.com (artificialman@yahoo.com)]]></author>
			<pubDate>Mon, 10 Dec 2012 19:29:01 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=157467#p157467</guid>
		</item>
	</channel>
</rss>
