[英]Ruby Equivalent
我正在用Ruby实现Java库。 我遇到了以下路障。 是否可以在ruby中实现以下代码? 字节[],IvParameterSpec,SecretKeySpec有红宝石等效物吗?
private String decrypt(String token)
{
//parse token into its IV and token components
byte[] ivAndToken = Base64.decodeBase64(token);
byte[] iv = new byte[ivLength];
System.arraycopy(ivAndToken, 0, iv, 0, ivLength);
int length = ivAndToken.length - ivLength;
byte[] tokenBytes = new byte[length];
System.arraycopy(ivAndToken, ivLength, tokenBytes, 0, length);
//prepare initialization vector specification
IvParameterSpec spec = new IvParameterSpec(iv);
//create cipher instance based on transformer params
Cipher cipher = Cipher.getInstance(algorithm + mode + padding, CRYPTO_PROVIDER);
//convert key bytes into valid key format
Key key = new SecretKeySpec(Base64.decodeBase64(symkey), algorithm);
//initialize cipher for decryption
cipher.init(Cipher.DECRYPT_MODE, key, spec);
//decrypt the payload
String plaintext = new String(cipher.doFinal(tokenBytes));
return plaintext;
}
如果您希望算法的行为与Java中的行为完全相同,则可能必须在Ruby上同时实现IvParameterSpec
和SecretKeySpec
。 byte[]
当然只是一个字节数组。 您可能想要使用它们的文档(上面的链接),并且希望您了解块密码操作模式的工作原理。
如果您不这样做,则SecretKey指的是对称密钥(例如:密码短语),IV是初始化向量 ,用于对同一纯文本进行不同加密的加密随机数生成不同的密文。 ECB以外的所有操作模式都需要IV。 有关更多详细信息,请参见此Wikipedia页面 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.