2016-05-29 145 views
1

我需要加密使用OpenSSL::Cipher::AES一些固定大小号,但是我难以理解什么包装的组合,我需要使用获得的已知结果。如何加密使用OpenSSL ::密码

我有Java的参考代码如下:

import java.io.UnsupportedEncodingException; 
import java.security.InvalidAlgorithmParameterException; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 
import javax.crypto.Cipher; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec; 
import org.apache.commons.codec.DecoderException; 
import org.apache.commons.codec.binary.Base64; 
import org.apache.commons.codec.binary.Hex; 
import com.google.common.primitives.Longs; 

class Encryptor { 
    private String initialVector; 
    private static final String TRANFORMATION = "AES/CBC/PKCS5Padding"; 
    private static final String ALGORITHM = "AES"; 

    String encrypt(SecretKeySpec key, long timestamp) throws Exception { 
    byte[] encryptedBytes = getEncryptingCipher(key).doFinal(Longs.toByteArray(timestamp)); 
    return Base64.encodeBase64String(encryptedBytes); 
    } 

    private Cipher getEncryptingCipher(SecretKeySpec key) throws 
    NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, 
    InvalidKeyException, 
    InvalidAlgorithmParameterException { 
     Cipher encryptingCipher = Cipher.getInstance(TRANFORMATION); 
     encryptingCipher.init(Cipher.ENCRYPT_MODE, key, new 
      IvParameterSpec(initialVector.getBytes())); 
     return encryptingCipher; 
    } 

    private SecretKeySpec getSecretKeySpec(String key) throws DecoderException { 
    byte[] keyBytes = Hex.decodeHex(key.toCharArray()); 
    return new SecretKeySpec(keyBytes, ALGORITHM); 
    } 

    void setInitialVector(String initialVector) { 
    this.initialVector = initialVector; 
    } 

    public static void main(String[] args) throws Exception { 
    Encryptor encryptor = new Encryptor(); 
    encryptor.setInitialVector("0000000000000000"); 
    //Expensive operation so only performed once, re-use the key spec instance 
    SecretKeySpec keySpec = 
    encryptor.getSecretKeySpec("b35901b480ca658c8be4341eefe21a80"); 
    long timestamp = 1464284796L; //System.currentTimeMillis()/1000; 
    String authToken = encryptor.encrypt(keySpec, timestamp); 
    System.out.println(authToken); 
    } 
} 

导致以下base64编码字符串被打印在控制台上:6BH3hg1cqQJOK6sG8gw7Xw==

我试图复制这种使用OpenSSL::Cipher,但我没有运气。 Ruby OpenSSL文档缺乏细节。我怀疑我的包装使用不正确。

require "openssl" 

key = ['b35901b480ca658c8be4341eefe21a80'].pack('H*') 
iv = '0000000000000000' 
message = [1464284796].pack "N*" 

cipher = OpenSSL::Cipher::AES.new(128, :CBC) 
cipher.encrypt 

cipher.iv = iv 
cipher.key = key 

encrypted = cipher.update(message) 
encrypted << cipher.final 

cipher64 = [encrypted].pack('m') 

puts "Generated: " + cipher64 
puts "Expected : " + '6BH3hg1cqQJOK6sG8gw7Xw==' 

回答

2

番石榴文档说为Longs#toByteArray(long)

toByteArray(long value)
返回value在一个8字节元件阵列的大端表示;相当于ByteBuffer.allocate(8).putLong(value).array()

但Ruby的Array#pack "N*"编码一个32位无符号整数,结果为4个字节。

message = [0, 1464284796].pack "N*" 

或在评论matt笔记使用大端64位整数:

message = [1464284796].pack "Q>" 
+1

你也可以使用'Q>'所以,你可以简单地用4个零填充它,它会直接给你一个64位无符号值,而不需要添加额外的零。 – matt