简体   繁体   English

通过 OpenSSL 从 BouncyCastle 的加密字符串解密字符串的问题

[英]Issues decrypting a string by OpenSSL from encrypted string by BouncyCastle

I am using BouncyCastle and C# to crypt a string with RSA engine and a public key.我正在使用 BouncyCastle 和 C# 来加密带有 RSA 引擎和公钥的字符串。

I want to decrypt the string by OpenSSL (Linux) and the private key by terminal;我想通过 OpenSSL (Linux) 解密字符串,通过终端解密私钥; actually by OpenSSL library but I have problems by commands terminal too.实际上是通过 OpenSSL 库实现的,但我也遇到了命令终端的问题。

I created the keys by OpenSSL commands我通过 OpenSSL 命令创建了密钥

openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048

I have issues to decrypt the string by OpenSSL, I wrote a stub for crypt a file.我有问题无法通过 OpenSSL 解密字符串,我写了一个存根来加密文件。

This is the stub of the crypting这是加密的存根

  public static void CryptStringByPublicKey(string stringToCrypt)
        {

            var key = GetPublicKey();

            var e = new Pkcs1Encoding(new RsaEngine());

            e.Init(true, key);

            var bytes = Encoding.ASCII.GetBytes(stringToCrypt);

            var encryptedBytes = e.ProcessBlock(bytes, 0, bytes.Length);

            // Which Encoding?
            var stringToSave = Encoding.UTF8.GetString(encryptedBytes);

            File.WriteAllText(@"C:\temp\encrypted_file", stringToSave);



        }

        public static RsaKeyParameters GetPublicKey()
        {

            const string privateKeyString = @"C:\temp\public_key.pem";


            RsaKeyParameters publicKey;

            using (var reader = File.OpenText(privateKeyString))
                publicKey = (RsaKeyParameters)new PemReader(reader).ReadObject();

            return publicKey;
        }

This is the commands line on Linux to decrypt这是Linux上解密的命令行

openssl rsautl -decrypt -in encrypted_file -out message.decrypted -inkey lora_private.pem 


I expected the decrypted string in message.decrypted but openssl respond我期望 message.decrypted 中的解密字符串但 openssl 响应

140251030225344:error:0406506C:rsa routines:rsa_ossl_private_decrypt:data greater than mod len:../crypto/rsa/rsa_ossl.c:399:

I tried to figure problems in encoding the char array, I tried all kinds of encoding (UTF8, ASCII).我试图找出编码字符数组的问题,我尝试了各种编码(UTF8、ASCII)。

Someone can help me with any suggestions?有人可以帮我提供任何建议吗?

Can't comment as too new. 不能评论太新。

Try encrypting as base64 before sending if you must send as text. 如果必须以文本形式发送,请尝试在发送之前以base64加密。 I have done this in reverse (php openssl --> c#) 我已经反向完成了此操作(php openssl-> c#)

Then remove the spaces. 然后删除空格。

byte[] encryptedBytes = Convert.FromBase64String(encrypted.Replace(" ", "+"));

I had forgot that my encrypted bytes were base64 encoded.忘记了我的加密字节是 base64 编码的。 OpenSSL will happily decode your base64 string for you, and you can make a nice one-liner. OpenSSL 会很乐意为您解码 base64 字符串,您可以制作一个不错的单行。 DON'T forget the -A option:不要忘记 -A 选项:

openssl.exe base64 -A -d -in enc64.txt | openssl.exe pkeyutl -decrypt -inkey .\keys\privatekey.pem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 BouncyCastle 在 C# 中使用 OpenSSL 使用 AES256-GCM 解密由 PHP 加密的字符串 - Decrypting a string encrypted by PHP with AES256-GCM using OpenSSL in C# using BouncyCastle 无法在 C# 中解密由 openssl 完成的加密字符串。 总是得到 Org.BouncyCastle.Security.InvalidKeyException - Cannot decrypt in C# an encrypted string done by openssl. Always get Org.BouncyCastle.Security.InvalidKeyException 从openssl在bouncycastle中解密的C#中的编码问题 - Encoding problem in C# decrypting in bouncycastle from openssl 使用RSACryptoProvider从PHP中加密的字符串解密时出现“错误数据” - “Bad Data” when decrypting using RSACryptoProvider from string encrypted in PHP 在C#中解密AES加密的字符串 - Decrypting AES encrypted string in C# 解密C#中的PHP加密字符串? - Decrypting a PHP encrypted string in C#? 在Python中解密使用.NET加密的字符串 - Decrypting in Python an string encrypted using .NET 在C#中解密用3DES加密的ColdFusion中的字符串 - Decrypting a string in ColdFusion encrypted with 3DES in C# 使用PHP加密的ASP.Net解密字符串 - Decrypting string using ASP.Net encrypted by Php 为什么只有一半的加密字符串无法正确解密? - Why is only one half of my encrypted string not decrypting properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM