我有一个用于加密和解密的java代码,我想将其更改/转换为ruby代码。我在opensslgem中查找了一下,但发现ruby中提供了“rsa/ecb/oaepithha-256和mgf1padding”组合。如何实施?
public class EncryptDecryptService {
public String encryptRequestObject(RequestObject requestObject) throws UnsupportedEncodingException, FileNotFoundException, CertificateException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
PublicKey publicKey = getPublicKey(requestObject.getKeyFilename());
byte[] message = requestObject.getString().getBytes("UTF-8");
byte[] secret = encrypt(publicKey, message);
return Base64.encodeBase64String(secret);
}
public String decryptRequestObject(RequestObject requestObject) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
PrivateKey privateKey = getPrivateKey(requestObject.getKeyFilename(), requestObject.getKeyPassword());
byte[] cipherText = Base64.decodeBase64(requestObject.getString());
byte[] decrypted = decrypt(privateKey, cipherText);
return new String(decrypted, "UTF-8");
}
private PublicKey getPublicKey(String filename) throws FileNotFoundException, CertificateException {
FileInputStream fin = new FileInputStream(filename);
CertificateFactory factory = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) factory.generateCertificate(fin);
PublicKey publicKey = certificate.getPublicKey();
return publicKey;
}
private PrivateKey getPrivateKey(String filename, String password) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {
FileInputStream fin = new FileInputStream(filename);
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(fin, password.toCharArray());
String str = ks.aliases().nextElement();
PrivateKey privateKey = (PrivateKey) ks.getKey(str, password.toCharArray());
return privateKey;
}
private byte[] encrypt(PublicKey key, byte[] plainText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plainText);
}
private byte[] decrypt(PrivateKey key, byte[] cipherText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(cipherText);
}
}
1条答案
按热度按时间42fyovps1#
oaep使用多个参数,包括两个摘要,一个用于oaep(即,对oaep标签进行散列),另一个用于掩码生成函数(mgf1),请参阅rfc8017,第节。7.1.
标识符
RSA/ECB/OAEPWithSHA-256AndMGF1Padding
不明确,取决于提供程序。例如,sunjce提供程序使用sha-256作为oaep摘要,sha-1作为mgf1摘要,bouncycastle提供程序使用sha-256作为这两个摘要。下面是一个用java代码加密和用ruby代码解密的例子(相反的方向是模拟的)。
在java端,sunjce提供程序用于wlog,并确定了涉及的摘要:
具有
对应于发布的
encrypt()
方法(附加输出除外)。代码产生(例如)以下输出:为完整起见,应提及的是,参数的明确说明也可用于:
因此,由于上面描述的矛盾,这个明确的规范是更健壮的替代方案。
在确定了摘要之后(因为提供者是已知的或者上面的输出是显式的),就可以进行ruby实现了。
ruby的一个可能的oaep实现是openssloaep。
这样,用于解密的ruby代码可以实现如下:
以原始明文作为输出。