2015-04-17 81 views
-3

enter image description here我正在寻找一些帮助做AES 256位加密模式为CBC和IV为(16字节)00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00加密使用AES 256模式-CBC IV(0)

基本上我想复制this link上做了什么。所有密钥将以十六进制格式显示以下是我从其他链接复制的内容。

夫妇在下面解决问题:

  1. 我怎样才能让IV密钥的所有零的?
  2. 我怎么知道我的所有密钥都是在十六进制中,并且输出必须是十六进制的呢?


public static String Encrypt(String plainText, String key) 
{ 
    var plainBytes = Encoding.UTF8.GetBytes(plainText); 
    return Convert.ToBase64String(Encrypt(plainBytes, GetRijndaelManaged(key))); 
} 

static RijndaelManaged GetRijndaelManaged(String secretKey) 
{ 
    var keyBytes = new byte[16]; 
    var secretKeyBytes = Encoding.ASCII.GetBytes(secretKey); 
    Array.Copy(secretKeyBytes, keyBytes, Math.Min(keyBytes.Length, secretKeyBytes.Length)); 
    return new RijndaelManaged 
    { 
     Mode = CipherMode.ECB, 
     Padding = PaddingMode.PKCS7, 
     KeySize = 128, 
     BlockSize = 128, 
     Key = keyBytes, 
     IV = keyBytes 

    }; 
} 

static byte[] Encrypt(byte[] plainBytes, RijndaelManaged rijndaelManaged) 
{ 
    return rijndaelManaged.CreateEncryptor() 
     .TransformFinalBlock(plainBytes, 0, plainBytes.Length); 
} 
+1

你的意思是'IV = new byte [16]'和'KeySize = 256'? –

+0

是先生,它的值必须全为零 – Amit

+0

是的,字节数组的默认值全为零:http://stackoverflow.com/questions/22506274/in-c-sharp-what-is-the-default-创建新字节数组时的字节数 –

回答

2

两个简单的方法来加密,并与链接的页面解密兼容:

public static byte[] HexToBytes(string str, string separator = " ") 
{ 
    if (str == null) 
    { 
     throw new ArgumentNullException(); 
    } 

    if (separator == null) 
    { 
     separator = string.Empty; 
    } 

    if (str == string.Empty) 
    { 
     return new byte[0]; 
    } 

    int stride = 2 + separator.Length; 

    if ((str.Length + separator.Length) % stride != 0) 
    { 
     throw new FormatException(); 
    } 

    var bytes = new byte[(str.Length + separator.Length)/stride]; 

    for (int i = 0, j = 0; i < str.Length; i += stride) 
    { 
     bytes[j] = byte.Parse(str.Substring(i, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture); 
     j++; 

     // There is no separator at the end! 
     if (j != bytes.Length && separator != string.Empty) 
     { 
      if (string.CompareOrdinal(str, i + 2, separator, 0, separator.Length) != 0) 
      { 
       throw new FormatException(); 
      } 
     } 
    } 

    return bytes; 
} 

public static string BytesToHex(byte[] bytes, string separator = " ") 
{ 
    if (bytes == null) 
    { 
     throw new ArgumentNullException(); 
    } 

    if (separator == null) 
    { 
     separator = string.Empty; 
    } 

    if (bytes.Length == 0) 
    { 
     return string.Empty; 
    } 

    var sb = new StringBuilder((bytes.Length * (2 + separator.Length)) - 1); 

    for (int i = 0; i < bytes.Length; i++) 
    { 
     if (i != 0) 
     { 
      sb.Append(separator); 
     } 

     sb.Append(bytes[i].ToString("x2")); 
    } 

    return sb.ToString(); 
} 

public static byte[] SimpleEncrypt(SymmetricAlgorithm algorithm, CipherMode cipherMode, byte[] key, byte[] iv, byte[] bytes) 
{ 
    algorithm.Mode = cipherMode; 
    algorithm.Padding = PaddingMode.Zeros; 
    algorithm.Key = key; 
    algorithm.IV = iv; 

    using (var encryptor = algorithm.CreateEncryptor()) 
    { 
     return encryptor.TransformFinalBlock(bytes, 0, bytes.Length); 
    } 
} 

public static byte[] SimpleDecrypt(SymmetricAlgorithm algorithm, CipherMode cipherMode, byte[] key, byte[] iv, byte[] bytes) 
{ 
    algorithm.Mode = cipherMode; 
    algorithm.Padding = PaddingMode.Zeros; 
    algorithm.Key = key; 
    algorithm.IV = iv; 

    using (var encryptor = algorithm.CreateDecryptor()) 
    { 
     return encryptor.TransformFinalBlock(bytes, 0, bytes.Length); 
    } 
} 

这样使用它:

string text = "xxxyyy"; 
string key = "da 39 a3 ee 5e 6b 4b 0d 32 55 bf ef 95 60 18 90"; 
string iv = "f8 01 8b 76 7c db 80 9c ed 66 fd 63 e8 41 d6 04"; 

var encrypted = BytesToHex(
    SimpleEncrypt(
     new RijndaelManaged(), 
     CipherMode.CBC, 
     HexToBytes(key), 
     HexToBytes(iv), 
     Encoding.UTF8.GetBytes(text))); 

var decrypted = Encoding.UTF8.GetString(
    SimpleDecrypt(
     new RijndaelManaged(), 
     CipherMode.CBC, 
     HexToBytes(key), 
     HexToBytes(iv), 
     HexToBytes(encrypted))).TrimEnd('\0'); 

注意,该页面不好f或二进制数据,因为它使用Padding.Zeros。问题是,这些字节组成的明文:

00 01 02 03 04 05 06 07 

转化为

00 01 02 03 04 05 06 07 00 00 00 00 00 00 00 00 
加密之前

,因为它是填充到16个字符。这个操作不能逆转。您使用的PaddingMode.PKCS7更好,因为它可以颠倒,但与该页面不兼容!如果您使用字符串进行加密,这不是问题,因为您可以执行TrimEnd('\0')并删除多余的\0。你可以通过一个不小于16的整数的小型压缩文件来检查它,通过页面对它进行加密(“输入类型”选择文件),然后按下加密,然后按下载为二进制文件。然后按浏览,选择刚刚下载的文件,按解密,按下载为二进制文件。文件大小将与原始文件不同,但您仍然可以使用zip打开它。

相关问题