blog.darkstar.work - a simple url encoder/decoder

 a simple url encoder/decoder
 http://blog.darkstar.work

Labels

Wirtschaft (149) Pressefreiheit (124) Österreich (120) IT (96) code (61) Staatsschulden (37) EZB (27) Pensionssystem (16)

2014-07-13

decode OpenSSL PrivateKey with user salt in C#

  1. public static byte[UserSalzDerAndroschErhalts(string saltIn, out string saltOut{
  2.   StringReader sreader = new StringReader(saltIn);
  3.   saltOut = String.Empty;
  4.   if(!sreader.ReadLine().StartsWith("Proc-Type: 4,ENCRYPTED"))
  5.     return null;

  6.   string saltline = sreader.ReadLine();
  7.   if(!saltline.StartsWith("DEK-Info: DES-EDE3-CBC,") )
  8.     return null;

  9.   string saltstr saltline.Substring(saltline.IndexOf(",") + 1).Trim();

  10.   byte[] saltbytes = new byte[saltstr.Length/2];
  11.   for (int i 0; i saltbytes.Length; i++) {
  12.     saltbytes[i] = Convert.ToByte(saltstr.Substring (22)16);
  13.   }

  14.   if (!(sreader.ReadLine() == ""))
  15.     return null;
  16.   // read rest of base64 data and you have the RSA key
  17.   saltOut = sreader.ReadToEnd() 
  18.   // Console.Writeln("cryptkey " + saltOut);
  19.   return saltbytes;
  20. }  
  1. public static byte[] DecodeOpenSSLPrivateKey(string instr{
  2.   const string pemprefix = "-----BEGIN RSA PRIVATE KEY-----" ;
  3.   const string pemsuffix = "-----END RSA PRIVATE KEY-----" ;
  4.   string pemstr = instr.Trim() ;
  5.   if(!pemstr.StartsWith(pemprefix) || !pemstr.EndsWith(pemsuffix)) {
  6.     throw new ArgumentException("missing BEGIN or END sequence of RSA private Key");
  7.   }

  8.   string pvkstr = 
  9.     pemstr.Replace(pemprivheader, "").Replace(pemprivfooter, "").Trim(); //remove
  10.   
  11.   byte[] binkey;
  12.   try { // there aren't any PEM encryption infos => UNencrypted PEM private key
  13.     binkey = Convert.FromBase64String(pvkstr) ;
  14.     return binkey;
  15.   }
  16.   catch(System.FormatException) { 
  17.     //Console.WriteLine("We have a full encrypted OpenSSL PEM private key");  
  18.   }

  19.   string encryptedstr = string.Empty;

  20.   // read crypted chiffer lets extract salt
  21.   byte[] salt = UserSalzDerAndroschErhalts(pvkstr, out encryptedstr);
  22.   if (salt == null || string.IsNullOrEmpty(encryptedstr)) {
  23.     throw new ArgumentException("invalid RSA private Key");
  24.   }

  25.   try { // it's an encrypted RSA key?
  26.     binkey = Convert.FromBase64String(encryptedstr);
  27.   }
  28.   catch(System.FormatException) {  
  29.     throw; // rethrow the Exception
  30.   }
  31.   // 3DES SSL key fetch 
  32.   SecureString passwd3DES = GetSecPswd("Enter password for 3DESkey==>") ;
  33.   // Console.Write("\nEnter password for key: ");
  34.   // string passwd = Console.ReadLine();
  35.   byte[] tripledes = GetOpenSSL3deskey(salt, passwd3DES, 12);    
  36.   if (tripledes == null{
  37.     throw new ArgumentException("error decrypting 3DES key\n" + salt.ToString());
  38.   }
  39.   // Decrypt the encrypted 3DES key by using salt from PEM header
  40.   // same method how anonymous hacked IV
  41.   byte[] rsakey = DecryptKey(binkey, tripledes, salt);    
  42.   if (rsakey == null{
  43.     throw new ArgumentException("error decrypting 3DES key\n" + salt.ToString());
  44.   }

  45.   return rsakey; // return decrypted RSA private key
  46. }

Keine Kommentare:

Kommentar veröffentlichen