다음을 통해 공유


CryptographicEngine.Encrypt(CryptographicKey, IBuffer, IBuffer) 메서드

정의

대칭 또는 비대칭 알고리즘을 사용하여 데이터를 암호화합니다.

public:
 static IBuffer ^ Encrypt(CryptographicKey ^ key, IBuffer ^ data, IBuffer ^ iv);
 static IBuffer Encrypt(CryptographicKey const& key, IBuffer const& data, IBuffer const& iv);
public static IBuffer Encrypt(CryptographicKey key, IBuffer data, IBuffer iv);
function encrypt(key, data, iv)
Public Shared Function Encrypt (key As CryptographicKey, data As IBuffer, iv As IBuffer) As IBuffer

매개 변수

key
CryptographicKey

암호화에 사용할 암호화 키입니다. 비대칭 또는 대칭 키일 수 있습니다. 자세한 내용은 AsymmetricKeyAlgorithmProviderSymmetricKeyAlgorithmProvider를 참조하세요.

data
IBuffer

암호화할 데이터입니다.

iv
IBuffer

초기화 벡터를 포함하는 버퍼입니다. 대칭 알고리즘의 경우 null 일 수 있으며 비대칭 알고리즘의 경우 항상 null 이어야 합니다. IV(초기화 벡터)를 사용하여 데이터를 암호화한 경우 동일한 IV를 사용하여 데이터의 암호를 해독해야 합니다. GenerateRandom 메서드를 사용하여 임의 데이터가 포함된 IV를 만들 수 있습니다. 생성되지 않은 벡터와 같은 다른 EV에는 사용자 지정 구현이 필요합니다. 자세한 내용은 암호화 키를 참조하세요.

CBC(암호화 블록 체인) 블록 암호화 모드 알고리즘에는 초기화 벡터가 필요합니다. 자세한 내용은 설명 부분을 참조하세요.

반환

암호화된 데이터입니다.

예제

public IBuffer SampleCipherEncryption(
    String strMsg,
    String strAlgName,
    UInt32 keyLength,
    out BinaryStringEncoding encoding,
    out IBuffer iv,
    out CryptographicKey key)
{
    // Initialize the initialization vector.
    iv = null;

    // Initialize the binary encoding value.
    encoding = BinaryStringEncoding.Utf8;

    // Create a buffer that contains the encoded message to be encrypted. 
    IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg, encoding);

    // Open a symmetric algorithm provider for the specified algorithm. 
    SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strAlgName);

    // Determine whether the message length is a multiple of the block length.
    // This is not necessary for PKCS #7 algorithms which automatically pad the
    // message to an appropriate length.
    if (!strAlgName.Contains("PKCS7"))
    {
        if ((buffMsg.Length % objAlg.BlockLength) != 0)
        {
            throw new Exception("Message buffer length must be multiple of block length.");
        }
    }

    // Create a symmetric key.
    IBuffer keyMaterial = CryptographicBuffer.GenerateRandom(keyLength);
    key = objAlg.CreateSymmetricKey(keyMaterial);

    // CBC algorithms require an initialization vector. Here, a random
    // number is used for the vector.
    if (strAlgName.Contains("CBC"))
    {
        iv = CryptographicBuffer.GenerateRandom(objAlg.BlockLength);
    }

    // Encrypt the data and return.
    IBuffer buffEncrypt = CryptographicEngine.Encrypt(key, buffMsg, iv);
    return buffEncrypt;
}

설명

Microsoft에서 지원하는 대칭 알고리즘 중에서 초기화 벡터가 필요합니다.

적용 대상

추가 정보