AES ECB模式在java中加密并在ruby中解密

时间:2021-11-16 18:24:46

I have to encryption method which write in java. My encryption java code:

我必须用java编写的加密方法。我的加密java代码:

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 < buf.length; i++) {
        String hex = Integer.toHexString(buf[i] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        sb.append(hex.toUpperCase());
    }
    return sb.toString();
}

My ruby decryption code (not work):

我的ruby解密代码(不起作用):

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

Anyone can help? thanks in advance!!!

有人可以帮忙吗?提前致谢!!!

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

当我使用'ef3192c8803a47cb829d487dd2f78a3d'作为密钥,并使用我的java代码加密'helloworld'时,我得到'3CAEF382FB17A045EADDEFC72D3D0362',但是当我尝试使用我的ruby代码解密它时,我打电话

decryption('3CAEF382FB17A045EADDEFC72D3D0362', 'ef3192c8803a47cb829d487dd2f78a3d')

I can't get 'helloworld', So I think the decryption code is not work.

我不能得到'helloworld',所以我认为解密代码不起作用。

Java decryption code( it's works)

Java解密代码(它的工作原理)

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;
}

2 个解决方案

#1


3  

The Java implementation is sort of messed up.

Java实现有点搞砸了。

The actual AES key used to encrypt in the java code is the SecretKey secretKey generated with the following code using the String sKey as input:

用于在java代码中加密的实际AES密钥是使用String sKey作为输入使用以下代码生成的SecretKey secretKey:

    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();

To be able to decrypt in the ruby code, you need the content of SecretKey secretKey. Try adding this trace statement after above code:

为了能够在ruby代码中解密,您需要SecretKey secretKey的内容。尝试在上面的代码后添加此trace语句:

    System.out.println("Key: " + ByteUtil.parseByte2HexStr(enCodeFormat));

And use that as the key in your ruby call.

并将其用作ruby调用中的键。

#2


1  

You're not using the same key on both ends. In the Java encryption, you're using the key as the seed for a random-number generator, and then using the output of that as the key. On the Ruby side, you're directly using the input value as the key.

你没有在两端使用相同的密钥。在Java加密中,您使用密钥作为随机数生成器的种子,然后使用该输出作为密钥。在Ruby方面,您直接使用输入值作为键。

#1


3  

The Java implementation is sort of messed up.

Java实现有点搞砸了。

The actual AES key used to encrypt in the java code is the SecretKey secretKey generated with the following code using the String sKey as input:

用于在java代码中加密的实际AES密钥是使用String sKey作为输入使用以下代码生成的SecretKey secretKey:

    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();

To be able to decrypt in the ruby code, you need the content of SecretKey secretKey. Try adding this trace statement after above code:

为了能够在ruby代码中解密,您需要SecretKey secretKey的内容。尝试在上面的代码后添加此trace语句:

    System.out.println("Key: " + ByteUtil.parseByte2HexStr(enCodeFormat));

And use that as the key in your ruby call.

并将其用作ruby调用中的键。

#2


1  

You're not using the same key on both ends. In the Java encryption, you're using the key as the seed for a random-number generator, and then using the output of that as the key. On the Ruby side, you're directly using the input value as the key.

你没有在两端使用相同的密钥。在Java加密中,您使用密钥作为随机数生成器的种子,然后使用该输出作为密钥。在Ruby方面,您直接使用输入值作为键。