Note that there are some explanatory texts on larger screens.

plurals
  1. POAES ECB mode encrypt in java and decrypt in ruby
    primarykey
    data
    text
    <p>I have to encryption method which write in java. My encryption java code:</p> <pre><code>public static String encrypt(String content, String sKey) { try { SecretKey secretKey = null; KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(sKey.getBytes()); kgen.init(128, secureRandom); secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] byteContent = content.getBytes("utf-8"); byte[] result = cipher.doFinal(byteContent); return ByteUtil.parseByte2HexStr(result); } catch (Exception e) { e.printStackTrace(); } return content; } ## ByteUtil.java ... public static String parseByte2HexStr(byte buf[]) { StringBuffer sb = new StringBuffer(); for (int i = 0; i &lt; buf.length; i++) { String hex = Integer.toHexString(buf[i] &amp; 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } </code></pre> <p><strong>My ruby decryption code (not work):</strong> </p> <pre><code>require "openssl" require 'digest/sha2' require 'base64' def decryption(encrypted, key) decipher = OpenSSL::Cipher::AES.new(128, :ECB) decipher.decrypt decipher.padding = 0 decipher.key = [key].pack('H*') encrypted = [encrypted].pack('H*') plain = decipher.update(encrypted) + decipher.final p plain p plain.unpack('s*') p plain.unpack('m*') p plain.unpack('u*') p plain.unpack('h*') p plain.unpack('a*') end </code></pre> <p>Anyone can help? thanks in advance!!!</p> <p>when I use 'ef3192c8803a47cb829d487dd2f78a3d' as key, and encrypt 'helloworld' with my java code, I get '3CAEF382FB17A045EADDEFC72D3D0362', But when I try use my ruby code to decrypt it, I call</p> <pre><code>decryption('3CAEF382FB17A045EADDEFC72D3D0362', 'ef3192c8803a47cb829d487dd2f78a3d') </code></pre> <p>I can't get 'helloworld', So I think the decryption code is not work.</p> <p><strong>Java decryption code( it's works)</strong></p> <pre><code>public static String decrypt(String content, String sKey) { try { SecretKey secretKey = null; KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(sKey.getBytes()); kgen.init(128, secureRandom); secretKey = kgen.generateKey(); byte[] data = ByteUtil.parseHexStr2Byte(content); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] result = cipher.doFinal(data); return new String(result, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return content; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload