私はJavaで暗号化と復号化の責任を負うコードスニペットを持っており、Rubyで変換する必要があります。ここに投稿する前に私は4リンクを通過しましたが、運はありません。私はコードの下に書いても同じ結果を得るためにRubyへのJavaの暗号化/復号化
aes-cbc-pkcs5padding-encrypt-in-java-decrypt-in-ruby
aes-cbc-pkcs5padding-implementation-in-ruby-for-rails
symmetric encryption algorithms in Ruby
*
/**
*
*/
package in.bets.gsm.util;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
* @author VKatz
*
*/
public class SecurePath {
/**
*
*/
public SecurePath() {
// TODO Auto-generated constructor stub
}
public static String key = "Bar12345Bar12345";
public static String initVector = "RandomInitVector";
public static String encrypt(String value) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec [skeySpec][4] = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
System.out.println("encrypted string: "
+ Base64.encodeBase64String(encrypted));
return Base64.encodeBase64String(encrypted);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static String decrypt(String encrypted) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
return new String(original);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String encText = encrypt("abceeffslaj");
System.out.println("Decripted text :: " + decrypt("XZy6gJinORmH+LOiZL6/Jw=="));
}
}
Output:
Simple Text :: abceeffslaj
Encrypted text :: XZy6gJinORmH+LOiZL6/Jw==
Decripted Text :: abceeffslaj
私の努力:ルビー
require "openssl"
require "base64"
require 'byebug'
include Base64
plain_text = "abceeffslaj"
cipher = OpenSSL::Cipher::AES128.new(:CBC)
cipher.encrypt
key = cipher.random_key
iv = cipher.random_iv
cipher_text = cipher.update(plain_text) + cipher.final
cipher = OpenSSL::Cipher::AES128.new(:CBC)
cipher.decrypt
cipher.key = key
cipher.iv = iv
decrypted_plain_text = cipher.update(cipher_text) + cipher.final
puts "AES128 in CBC mode"
puts "Key: " + urlsafe_encode64(key)
puts "Iv: " + urlsafe_encode64(iv)
puts "Plain text: " + plain_text
puts "Cipher text: " + urlsafe_encode64(cipher_text)
puts "Decrypted plain text: " + decrypted_plain_text
出力:
AES128 in CBC mode
Key: CJ-SNuUllNKl1vAllEazKg==
Iv: ZMb2W6K07oaAXuvoL8Ckpg==
Plain text: abceeffslaj
Cipher text: jyutt1ljXW9Xn-HFxpvcEg==
Decrypted plain text: abceeffslaj
私たちがここに気づくことができるかもしれませんが暗号文がJavaコードとの違いです。
非常に助かります。
「どんな助けもありがとう!」 - あなたのコードで何が問題になっていますか?エラーメッセージが表示されますか?エラーメッセージとは何ですか?結果はあなたが期待している結果を得ていないのですか?あなたはどんな結果を期待していますか、そしてなぜ、あなたはどのような結果を得ていますか、そしてその二つの違いは何ですか?あなたが観察している行動は、望ましい行動ではありませんか?望ましい行動とは何か、なぜ行動が観察され、どのように違うのですか? –
あなたのコメントのために@JörgWMittagありがとう私は期待された結果で私の質問を編集しました、ここでは暗号が違うことに気付くことができます。 – VKatz