2011-12-16 50 views
0

我知道我们可以使用下面的行明确定义密钥。对于3des,如果我没有错,密钥长度应该是24个字节。从文件生成指定数量的字节密钥

Dim Newkey() As Byte = Convert.FromBase64String("24 bytes enter here") 

Dim Newkey() As Byte = Convert.FromBase64String("c:\temp\mykey.pem") 
  1. 我如何确保该文件还给24个字节的数据,用于3DES加密?
  2. 什么/我如何生成这样的文件?

回答

1
  1. 只需检查使用NewKey.Length数组的长度进行解码
  2. 使用TripleDES.GenerateKey后,检索的关键属性。这将生成密钥,应该是与TripleDes的兼容,然后调用Converter.ToBase64String

请查找创建的教程/阅读文本文件的自己,用一个文件名FromBase64String将无法正常工作。

http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.generatekey.aspx#Y0

注意,16级字节的密钥可以被用于TripleDes的(ABA键)以及24字节(ABC)的键。还要注意,DES中存在弱密钥,并且密钥包括奇偶校验位。大多数实现都忽略奇偶校验位,但最好使用特殊的密钥生成函数来确保它们设置正确。

-2
using System; 
using System.Text; 
using System.Security.Cryptography; 

namespace Crypto 
{ 
    public class KeyCreator 
    { 
     public static void Main(String[] args) 
     {   
      String[] commandLineArgs = System.Environment.GetCommandLineArgs(); 
      string decryptionKey = CreateKey(System.Convert.ToInt32(commandLineArgs[1])); 
      string validationKey = CreateKey(System.Convert.ToInt32(commandLineArgs[2])); 

      Console.WriteLine("<machineKey validationKey=\"{0}\" decryptionKey=\"{1}\" validation=\"SHA1\"/>", validationKey, decryptionKey); 
     } 

     static String CreateKey(int numBytes) 
     { 
      RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); 
      byte[] buff = new byte[numBytes]; 

      rng.GetBytes(buff); 
      return BytesToHexString(buff); 
     } 

     static String BytesToHexString(byte[] bytes) 
     { 
      StringBuilder hexString = new StringBuilder(64); 

      for (int counter = 0; counter < bytes.Length; counter++) 
      { 
       hexString.Append(String.Format("{0:X2}", bytes[counter])); 
      } 
      return hexString.ToString(); 
     } 
    } 
} 
+0

我如何创建一个cer文件,以便它返回的原始字节是24?用作3des密码服务提供商的密钥。像TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider(); X509Certificate2 cert = new X509Certificate2(@“c:\ temp \ whatever.cer”); cryptoProvider.Key = cert.PublicKey.Key。 – Suresh 2011-12-17 04:27:45