0

我正在处理 angular 6 和 .net core api 端的加密/解密请求/响应。我正在使用 AES 以角度加密数据,并使用 RSA 公钥加密 AES 密钥并将其发送到 .net core api。我创建了动作过滤器来解密请求,并首先解密请求我必须使用 RSA 私钥解密 AES 密钥,但是在使用 RSA 私钥解密 AES 密钥时它给了我一个错误:

  • ex {Org.BouncyCastle.Crypto.InvalidCipherTextException: 在 Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding.DecodeBlock(Byte[] input, Int32 inOff, Int32 inLen) 在 Phyzii.Core.Api.Security.RSA.Decrypt(String cipherText)} System.Exception {Org.BouncyCastle.Crypto.InvalidCipherTextException}

这是我在角端的 RSA 加密代码:

import JSEncrypt from 'jsencrypt';
encryptObj = new JSEncrypt();
transitionIn(data: any) {
    this.aesSecretKey=this.makeUniqueKey(10);
    console.log(this.aesSecretKey);
    this.data.DATAOBJ = this.aesEncrypt(this.aesSecretKey, data);
    this.encryptObj.setPublicKey(this.publicKeyClient);
    this.data.KEY = this.encryptObj.encrypt(this.aesSecretKey);
    return this.data;
}

这是我在 C# 端的 RSA 解密代码:

private static AsymmetricCipherKeyPair ReadPemFile(string flag)
{
    string filePath = flag == "PUBLIC" ? "D:/Crypto/private_key.pem" : "D:/Crypto/private_key_server.pem";
    AsymmetricCipherKeyPair keys;
    using (var reader = File.OpenText(filePath))// file containing RSA PKCS1 private key
        keys = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject();
    AsymmetricKeyParameter private_key = keys.Private;
    AsymmetricKeyParameter public_key = keys.Public;
    return keys;
}


//cipherText = "gOItOryuGy0UXHfoNqo0omcXLIOS6dhLJas5zeDNA7MfvsHYwP4ccSWU9JwTrIRiYUq/NB9oRn62ZQ5ynDnsGXUmHfVT4oPxtQZE1fXTTMN5ycfgthegesmXoZMMcWxA/wnwjLAgE17MNaunKY307W+nyc3jEMT1QsWUoOBESo0="
public static string Decrypt(string cipherText) 
{
    try
    {
        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
        AsymmetricCipherKeyPair keys = ReadPemFile("PRIVATE");
        AsymmetricKeyParameter private_key = keys.Private;

        // Pure mathematical RSA implementation
        // RsaEngine eng = new RsaEngine();

        // PKCS1 v1.5 paddings
        // Pkcs1Encoding eng = new Pkcs1Encoding(new RsaEngine());

        // PKCS1 OAEP paddings
        Pkcs1Encoding eng = new Pkcs1Encoding(new RsaEngine());
        eng.Init(false, private_key);

        int length = cipherTextBytes.Length;
        int blockSize = eng.GetInputBlockSize();
        List<byte> plainTextBytes = new List<byte>();
        for (int chunkPosition = 0; chunkPosition < length; chunkPosition += blockSize)
        {
            int chunkSize = Math.Min(blockSize, length - chunkPosition);
            plainTextBytes.AddRange(eng.ProcessBlock(cipherTextBytes, chunkPosition, chunkSize));
        }
        return Encoding.UTF8.GetString(plainTextBytes.ToArray());
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

如果我true在 Pkcs1Encoding Init 中设置

Pkcs1Encoding eng = new Pkcs1Encoding(new RsaEngine());
eng.Init(true, private_key); 

然后它正在以这种格式解密数据�����Ŧ���%�Rc��\u000e�\b\u0004����I�]&P~�+�뛡�^s�V�ʗ' \b��?Jv�F�ge\u001b�S���^�\u0002��v/|�vh�}�z�[A�}��\u0002u\\�Pp����\u0011k9\u001e\n�E\b�\u0003��\u001a#��}��y��\u000eTG�U\a�A_KV�\u007fs����?3���*/*\n\n~�w�Q��'��\a:���q��BH\u0004R�#c��'d�\u001f���\0 5\u007f���fs�&lt;���\u0012\t����|�[\u0015\b+��8\u0003

这是不正确的,可能是什么问题?

4

0 回答 0