2012-04-28 2 views
0

CBC 모드와 Zero Padding을 사용하여 AES 128 암호화 문자열을 암호화하려고합니다. 슬프게도, 나는 이것을 시도하는 방법을 모른다. 많은 시도가 실패했다. 나는 C# 코드를 가지고 있으며 누군가 내 암호화 작업을 도와 줄 수 있는지 궁금해하고 있었다. 코드 :목표 C 영숫자 패딩을 사용하는 ABC CBC 암호화

`using System; 
using System.Security.Cryptography; 
using System.Text; 
using System.IO; 

byte[] request = UTF8Encoding.UTF8.GetBytes("{string which needs encrypting}"); 

byte[] key = UTF8Encoding.UTF8.GetBytes("{key}"); 
byte[] iv = UTF8Encoding.UTF8.GetBytes("{iv}"); 

AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); 

aes.Key = key; 
aes.IV = iv; 


aes.Mode = CipherMode.CBC; 

aes.Padding = PaddingMode.Zeros; 

ICryptoTransform cTransform = aes.CreateEncryptor(); 

byte[] result = cTransform.TransformFinalBlock(request, 0, request.Length); 

aes.Clear() 

string encryptedRequest = Convert.ToBase64String(result, 0, result.Length);` 

필자는 공통 암호를 보았다,하지만 난 [난 어쩌면 내가 간과 훨씬 어쨌든 cccrypto 몰라요?] 감사 CBC 모드 옵션을 볼 수 없습니다;

답변

2

[방법] CommonCrypto.m

- (NSData *) dataEncryptedUsingAlgorithm: (CCAlgorithm) algorithm 
           key: (id) key 
       initializationVector: (id) iv 
          options: (CCOptions) options 
           error: (CCCryptorStatus *) error 

IV 매개 변수 선언

그래서
@param  iv    Initialization vector, optional. Used for 
          Cipher Block Chaining (CBC) mode. If present, 
          must be the same length as the selected 
          algorithm's block size. If CBC mode is 
          selected (by the absence of any mode bits in 
          the options flags) and no IV is present, a 
          NULL (all zeroes) IV will be used. This is 
          ignored if ECB mode is used or if a stream 
          cipher algorithm is selected. 

, 당신은 통과 할 수 niliv에 매개 변수

0

다음 링크를 참조하십시오.

AES1 AES2 AES3

당신은 거기에서 소스 코드 라이브러리를 다운로드하고 당신을 위해 그 중 하나가 유용한 경우 프로젝트에 사용할 수 있습니다.

2

Common Crypto은 볼 수있는 적절한 장소입니다. 특히, CCCryptorCreate의 세 번째 매개 변수 인 CCOptions를 0으로 설정하려고합니다. 즉, 기본값은 CBC입니다. CommonCrypto.h는 실제로 맨 페이지보다 더 나은 문서이므로 열어야합니다. 여기에 그것을 수행하는 방법의 예 : 나는 IV에 대한 NULL 전달 (모든 0을 의미)와 올바르게 키와 키 크기 설정을 가지고 가정

CCCryptorRef cryptorRef; 
CCCryptorStatus rc; 
rc = CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, 0, key, keySize, NULL, &cryptorRef); 

. 필자의 테스트에서는 32 바이트 (256 비트)의 키를 사용했다.

관련 문제