문제

AES와 RSA를 사용하여 디지털 엔벨로프를 구현해야하지만 RSA 알고리즘의 .NET 구현에 문제가 있습니다.

나는 임의의 대칭 키로 데이터 (AES)를 암호화했지만 이제는 RSA를 사용하여 키를 암호화해야합니다.

키는 바이트 배열입니다 (byte[]) 그리고 공개 키는 나에게 모듈러스와 공개 지수, 바이트 배열 만 말한다 (byte[]).

이 두 매개 변수 만 사용하여 RSA로 AES 생성 키를 어떻게 암호화 할 수 있습니까?

다음 코드는 파일에서 메시지를 검색하고 AES로 암호화합니다. 그 후, 공개 키는 공개 키 파일에서 읽히고 모듈러스와 지수는 적절한 바이트 어레이에 있습니다. 어떻게 계속 암호화 할 것인가? symmetricKey RSA와 함께?

String msgString = Systematic.GetFileContents(messagePath);
Byte[] initVector = new byte[] { 50, 60, 70, 80, 90, 40, 50, 60, 70, 80, 90, 40, 60, 80, 70, 90 };
Byte[] symetricKey = AesCrypt.GenerateRandomKey();
Byte[] encryptedMessage = AesCrypt.Encrypt(msgString, symetricKey, initVector, mode);
Byte[] modulus = null;
Byte[] publicExp = null; 
DataFormatHelper.ReadPublicKey(publicKeyPath, "RSA", ref modulus, ref publicExp);

추신 : 답변에 대한 답변으로 rsa.ImportParameters: 나는 함께 시도했다 rsa.ImportParameters(keyInfo) 그러나 그것은 던졌습니다 CryptographicException ("Bad Data"). 배열 크기는 어떻습니까? 현재, 모듈러스는 128 바이트이고 지수 64 바이트입니다.

도움이 되었습니까?

해결책

사용 rsacryptoserviceprovider

static public byte[] RSAEncrypt(byte[] data,
    RSAParameters keyInfo, 
    bool doOAEPPadding)
{
    byte[] encryptedData;
    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
    {
        //Import the RSA Key information. This only needs
        //toinclude the public key information.
        rsa.ImportParameters(keyInfo);

        //Encrypt the passed byte array and specify OAEP padding.  
        //OAEP padding is only available on Microsoft Windows XP or later.  
        encryptedData = rsa.Encrypt(data, doOAEPPadding);
    }
    return encryptedData;       
}

그래서 당신이 필요로하는 것은 rsaparameters 그러나 설정해야 할 것은 모듈러스와 암호화 지수 만 있으면됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top