SymmetricAlgorithm 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
모든 대칭 알고리즘의 구현에서 상속해야 하는 추상 기본 클래스를 나타냅니다.
public ref class SymmetricAlgorithm abstract : IDisposable
public abstract class SymmetricAlgorithm : IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class SymmetricAlgorithm : IDisposable
type SymmetricAlgorithm = class
interface IDisposable
[<System.Runtime.InteropServices.ComVisible(true)>]
type SymmetricAlgorithm = class
interface IDisposable
Public MustInherit Class SymmetricAlgorithm
Implements IDisposable
- 상속
-
SymmetricAlgorithm
- 파생
- 특성
- 구현
예제
다음 코드 예제에서는 지정된 Key 속성 및 초기화 벡터(IV)가 있는 클래스를 사용하여 Aes 지정된 inName
파일을 암호화하고 암호화된 결과를 지정된 outName
파일에 출력합니다. 메서드의 매개 변수 및 desIV
매개 변수는 desKey
8 바이트 배열입니다. 이 예제를 실행하려면 높은 암호화 팩이 설치되어 있어야 합니다.
void EncryptData( String^ inName, String^ outName, array<Byte>^aesKey, array<Byte>^aesIV )
{
//Create the file streams to handle the input and output files.
FileStream^ fin = gcnew FileStream( inName,FileMode::Open,FileAccess::Read );
FileStream^ fout = gcnew FileStream( outName,FileMode::OpenOrCreate,FileAccess::Write );
fout->SetLength( 0 );
//Create variables to help with read and write.
array<Byte>^bin = gcnew array<Byte>(100);
long rdlen = 0; //This is the total number of bytes written.
long totlen = (long)fin->Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
Aes^ aes = Aes::Create();
CryptoStream^ encStream = gcnew CryptoStream( fout,aes->CreateEncryptor( aesKey, aesIV ),CryptoStreamMode::Write );
Console::WriteLine( "Encrypting..." );
//Read from the input file, then encrypt and write to the output file.
while ( rdlen < totlen )
{
len = fin->Read( bin, 0, 100 );
encStream->Write( bin, 0, len );
rdlen = rdlen + len;
Console::WriteLine( "{0} bytes processed", rdlen );
}
encStream->Close();
fout->Close();
fin->Close();
}
private static void EncryptData(string inName, string outName, byte[] aesKey, byte[] aesIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
Aes aes = Aes.Create();
CryptoStream encStream = new CryptoStream(fout, aes.CreateEncryptor(aesKey, aesIV), CryptoStreamMode.Write);
Console.WriteLine("Encrypting...");
//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}
encStream.Close();
fout.Close();
fin.Close();
}
Private Shared Sub EncryptData(inName As String, outName As String, _
rijnKey() As Byte, rijnIV() As Byte)
'Create the file streams to handle the input and output files.
Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
Dim fout As New FileStream(outName, FileMode.OpenOrCreate, _
FileAccess.Write)
fout.SetLength(0)
'Create variables to help with read and write.
Dim bin(100) As Byte 'This is intermediate storage for the encryption.
Dim rdlen As Long = 0 'This is the total number of bytes written.
Dim totlen As Long = fin.Length 'Total length of the input file.
Dim len As Integer 'This is the number of bytes to be written at a time.
'Creates the default implementation, which is RijndaelManaged.
Dim rijn As SymmetricAlgorithm = SymmetricAlgorithm.Create()
Dim encStream As New CryptoStream(fout, _
rijn.CreateEncryptor(rijnKey, rijnIV), CryptoStreamMode.Write)
Console.WriteLine("Encrypting...")
'Read from the input file, then encrypt and write to the output file.
While rdlen < totlen
len = fin.Read(bin, 0, 100)
encStream.Write(bin, 0, len)
rdlen = Convert.ToInt32(rdlen + len)
Console.WriteLine("{0} bytes processed", rdlen)
End While
encStream.Close()
fout.Close()
fin.Close()
End Sub
설명
클래스에서 SymmetricAlgorithm 파생되는 클래스는 데이터에 대한 암호화 변환을 수행하기 위해 키() 및 초기화 벡터(Key)가 필요한 CBC(IV암호 블록 체인)라는 체인 모드를 사용합니다. 클래스 중 SymmetricAlgorithm 하나를 사용하여 암호화된 데이터의 암호를 해독하려면 속성과 IV 속성을 암호화에 사용된 것과 동일한 값으로 설정 Key 해야 합니다. 대칭 알고리즘이 유용하려면 비밀 키를 보낸 사람 및 수신자에게만 알려야 합니다.
Aes, DES및 RC2TripleDES 대칭 알고리즘의 구현입니다.
파생 클래스를 사용하는 경우 보안 관점에서 개체 사용을 완료한 후에 가비지 수집을 강제로 적용하는 것만으로는 충분하지 않습니다. 개체가 해제되기 전에 개체 내의 Clear 중요한 데이터를 0으로 0으로 하려면 개체의 메서드를 명시적으로 호출해야 합니다. 가비지 수집은 수집된 개체의 콘텐츠를 0으로 표시하지 않고 단순히 메모리를 다시 할당에 사용할 수 있는 것으로 표시합니다. 따라서 가비지 수집된 개체 내에 포함된 데이터는 할당되지 않은 메모리의 메모리 힙에 여전히 있을 수 있습니다. 암호화 개체의 경우 이 데이터에는 키 데이터 또는 일반 텍스트 블록과 같은 중요한 정보가 포함될 수 있습니다.
중요한 데이터를 포함하는 .NET Framework 모든 암호화 클래스는 메서드를 Clear
구현합니다. 호출될 때 메서드는 Clear
개체 내의 모든 중요한 데이터를 0으로 덮어쓰고 안전하게 가비지 수집할 수 있도록 개체를 해제합니다. 개체가 0으로 설정되고 해제된 경우 매개 변수가 설정된 메서드 disposing
를 호출 Dispose
하여 True
개체와 연결된 모든 관리 및 관리되지 않는 리소스를 삭제해야 합니다.
구현자 참고
상속 하는 경우는 SymmetricAlgorithm 클래스 멤버를 재정의 해야 합니다: CreateDecryptor(Byte[], Byte[])를 CreateEncryptor(Byte[], Byte[])를 GenerateIV(), 및 GenerateKey()합니다.
생성자
SymmetricAlgorithm() |
SymmetricAlgorithm 클래스의 새 인스턴스를 초기화합니다. |
필드
BlockSizeValue |
암호화 작업의 블록 크기(비트)를 나타냅니다. |
FeedbackSizeValue |
암호화 작업의 피드백 크기(비트 단위)를 나타냅니다. |
IVValue |
대칭 알고리즘에 대한 초기화 벡터(IV)를 나타냅니다. |
KeySizeValue |
대칭 알고리즘에서 사용하는 비밀 키의 크기(비트 단위)를 나타냅니다. |
KeyValue |
대칭 알고리즘에 대한 비밀 키를 나타냅니다. |
LegalBlockSizesValue |
대칭 알고리즘에서 지원하는 블록 크기(비트 단위)를 지정합니다. |
LegalKeySizesValue |
대칭 알고리즘에서 지원하는 키 크기(비트 단위)를 지정합니다. |
ModeValue |
대칭 알고리즘에 사용된 암호화 모드를 나타냅니다. |
PaddingValue |
대칭 알고리즘에 사용된 패딩 모드를 나타냅니다. |
속성
BlockSize |
암호화 작업의 블록 크기(비트 단위)를 가져오거나 설정합니다. |
FeedbackSize |
CFB(Cipher Feedback) 및 OFB(Output Feedback) 암호화 모드에 대한 암호화 작업의 피드백 크기(비트 단위)를 가져오거나 설정합니다. |
IV |
대칭 알고리즘에 대한 초기화 벡터(IV)를 가져오거나 설정합니다. |
Key |
대칭 알고리즘에 대한 비밀 키를 가져오거나 설정합니다. |
KeySize |
대칭 알고리즘에서 사용하는 비밀 키의 크기(비트 단위)를 가져오거나 설정합니다. |
LegalBlockSizes |
대칭 알고리즘에서 지원하는 블록 크기(비트 단위)를 가져옵니다. |
LegalKeySizes |
대칭 알고리즘에서 지원하는 키 크기(비트 단위)를 가져옵니다. |
Mode |
대칭 알고리즘의 작업 모드를 가져오거나 설정합니다. |
Padding |
대칭 알고리즘에 사용된 패딩 모드를 가져오거나 설정합니다. |
메서드
명시적 인터페이스 구현
IDisposable.Dispose() |
이 API는 제품 인프라를 지원하며 코드에서 직접 사용되지 않습니다. SymmetricAlgorithm에서 사용하는 관리되지 않는 리소스를 해제하고, 관리되는 리소스를 선택적으로 해제할 수 있습니다. |