public static string DESEncryption(string plain, string key) { BCEngine bcEngine = new BCEngine(new DesEngine(), Global.UTF8); Pkcs7Padding padding = new Pkcs7Padding(); padding.Init(new SecureRandom()); bcEngine.SetPadding(padding); return bcEngine.Encrypt(plain, CheckKeyLength(key, 8)); }
public static string AESDecryption(string cipher, string key) { BCEngine bcEngine = new BCEngine(new AesEngine(), Global.UTF8); Pkcs7Padding padding = new Pkcs7Padding(); padding.Init(new SecureRandom()); bcEngine.SetPadding(padding); return bcEngine.Decrypt(cipher, CheckKeyLength(key, 32)); }
public static AesEncyrption getInstance() { if (instance == null) { try { if (string.IsNullOrEmpty(_encryptionKey)) { Initalize(); } AesEncyrption bcEngine = new AesEncyrption(); KeyParameter keyParam = new KeyParameter(Encoding.UTF8.GetBytes(_encryptionKey)); ICipherParameters param = new ParametersWithIV(keyParam, iv); //create decrypt/encryptor cipher IBlockCipherPadding padding = new Pkcs7Padding(); BufferedBlockCipher decrypt = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), padding); decrypt.Reset(); decrypt.Init(false, param); bcEngine.setDecryptCipher(decrypt); BufferedBlockCipher encrypt = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), padding); encrypt.Reset(); encrypt.Init(true, param); bcEngine.setEncryptCipher(encrypt); instance = bcEngine; } catch (Exception) { throw; } } return instance; }
public MainWindow() { InitializeComponent(); transactionTypeCollection.Add("CREDIT_CARD"); transactionTypeCollection.Add("DEBIT_CARD"); transactionTypeCollection.Add("ACH"); transactionTypeCollection.Add("INTERAC"); creditEntryModeCollection.Add("EMV"); creditEntryModeCollection.Add("HID"); creditEntryModeCollection.Add("KEYED"); debitEntryModeCollection.Add("EMV"); debitEntryModeCollection.Add("HID"); achEntryModeCollection.Add("KEYED"); creditChargeTypeCollection.Add("SALE"); creditChargeTypeCollection.Add("CREDIT"); creditChargeTypeCollection.Add("VOID"); creditChargeTypeCollection.Add("FORCE_SALE"); creditChargeTypeCollection.Add("AUTH"); creditChargeTypeCollection.Add("CAPTURE"); creditChargeTypeCollection.Add("ADJUSTMENT"); creditChargeTypeCollection.Add("SIGNATURE"); debitChargeTypeCollection.Add("PURCHASE"); debitChargeTypeCollection.Add("REFUND"); achChargeTypeCollection.Add("DEBIT"); achChargeTypeCollection.Add("CREDIT"); accountTypeCollection.Add("DEFAULT"); accountTypeCollection.Add("CASH_BENEFIT"); accountTypeCollection.Add("FOOD_STAMP"); creditTypeCollection.Add("INDEPENDENT"); creditTypeCollection.Add("DEPENDENT"); tccCollection.Add("PPD"); tccCollection.Add("CCD"); tccCollection.Add("WEB"); tccCollection.Add("TEL"); this.transactionTypeCombo.ItemsSource = transactionTypeCollection; _encoding = Encoding.ASCII; Pkcs7Padding pkcs = new Pkcs7Padding(); _padding = pkcs; var settingsPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.dat").ToString(); //string data = File.ReadAllText(settingsPath); string decryptedData = AESDecryption(File.ReadAllText(settingsPath), VariableHandler.CryptoKey, true); var parts = decryptedData.Split(','); VariableHandler.AccountToken = parts[0]; customParamText.Text = parts[1]; accountTokenText.Text = VariableHandler.AccountToken; }
public static IBufferedCipher GetCipher( string algorithm) { if (algorithm == null) throw new ArgumentNullException("algorithm"); algorithm = Platform.ToUpperInvariant(algorithm); { string aliased = (string) algorithms[algorithm]; if (aliased != null) algorithm = aliased; } IBasicAgreement iesAgreement = null; if (algorithm == "IES") { iesAgreement = new DHBasicAgreement(); } else if (algorithm == "ECIES") { iesAgreement = new ECDHBasicAgreement(); } if (iesAgreement != null) { return new BufferedIesCipher( new IesEngine( iesAgreement, new Kdf2BytesGenerator( new Sha1Digest()), new HMac( new Sha1Digest()))); } if (algorithm.StartsWith("PBE")) { if (algorithm.EndsWith("-CBC")) { if (algorithm == "PBEWITHSHA1ANDDES-CBC") { return new PaddedBufferedBlockCipher( new CbcBlockCipher(new DesEngine())); } else if (algorithm == "PBEWITHSHA1ANDRC2-CBC") { return new PaddedBufferedBlockCipher( new CbcBlockCipher(new RC2Engine())); } else if (Strings.IsOneOf(algorithm, "PBEWITHSHAAND2-KEYTRIPLEDES-CBC", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC")) { return new PaddedBufferedBlockCipher( new CbcBlockCipher(new DesEdeEngine())); } else if (Strings.IsOneOf(algorithm, "PBEWITHSHAAND128BITRC2-CBC", "PBEWITHSHAAND40BITRC2-CBC")) { return new PaddedBufferedBlockCipher( new CbcBlockCipher(new RC2Engine())); } } else if (algorithm.EndsWith("-BC") || algorithm.EndsWith("-OPENSSL")) { if (Strings.IsOneOf(algorithm, "PBEWITHSHAAND128BITAES-CBC-BC", "PBEWITHSHAAND192BITAES-CBC-BC", "PBEWITHSHAAND256BITAES-CBC-BC", "PBEWITHSHA256AND128BITAES-CBC-BC", "PBEWITHSHA256AND192BITAES-CBC-BC", "PBEWITHSHA256AND256BITAES-CBC-BC", "PBEWITHMD5AND128BITAES-CBC-OPENSSL", "PBEWITHMD5AND192BITAES-CBC-OPENSSL", "PBEWITHMD5AND256BITAES-CBC-OPENSSL")) { return new PaddedBufferedBlockCipher( new CbcBlockCipher(new AesFastEngine())); } } } string[] parts = algorithm.Split('/'); IBlockCipher blockCipher = null; IAsymmetricBlockCipher asymBlockCipher = null; IStreamCipher streamCipher = null; string algorithmName = parts[0]; { string aliased = (string)algorithms[algorithmName]; if (aliased != null) algorithmName = aliased; } CipherAlgorithm cipherAlgorithm; try { cipherAlgorithm = (CipherAlgorithm)Enums.GetEnumValue(typeof(CipherAlgorithm), algorithmName); } catch (ArgumentException) { throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); } switch (cipherAlgorithm) { case CipherAlgorithm.AES: blockCipher = new AesFastEngine(); break; case CipherAlgorithm.ARC4: streamCipher = new RC4Engine(); break; case CipherAlgorithm.BLOWFISH: blockCipher = new BlowfishEngine(); break; case CipherAlgorithm.CAMELLIA: blockCipher = new CamelliaEngine(); break; case CipherAlgorithm.CAST5: blockCipher = new Cast5Engine(); break; case CipherAlgorithm.CAST6: blockCipher = new Cast6Engine(); break; case CipherAlgorithm.DES: blockCipher = new DesEngine(); break; case CipherAlgorithm.DESEDE: blockCipher = new DesEdeEngine(); break; case CipherAlgorithm.ELGAMAL: asymBlockCipher = new ElGamalEngine(); break; case CipherAlgorithm.GOST28147: blockCipher = new Gost28147Engine(); break; case CipherAlgorithm.HC128: streamCipher = new HC128Engine(); break; case CipherAlgorithm.HC256: streamCipher = new HC256Engine(); break; case CipherAlgorithm.IDEA: blockCipher = new IdeaEngine(); break; case CipherAlgorithm.NOEKEON: blockCipher = new NoekeonEngine(); break; case CipherAlgorithm.PBEWITHSHAAND128BITRC4: case CipherAlgorithm.PBEWITHSHAAND40BITRC4: streamCipher = new RC4Engine(); break; case CipherAlgorithm.RC2: blockCipher = new RC2Engine(); break; case CipherAlgorithm.RC5: blockCipher = new RC532Engine(); break; case CipherAlgorithm.RC5_64: blockCipher = new RC564Engine(); break; case CipherAlgorithm.RC6: blockCipher = new RC6Engine(); break; case CipherAlgorithm.RIJNDAEL: blockCipher = new RijndaelEngine(); break; case CipherAlgorithm.RSA: asymBlockCipher = new RsaBlindedEngine(); break; case CipherAlgorithm.SALSA20: streamCipher = new Salsa20Engine(); break; case CipherAlgorithm.SEED: blockCipher = new SeedEngine(); break; case CipherAlgorithm.SERPENT: blockCipher = new SerpentEngine(); break; case CipherAlgorithm.SKIPJACK: blockCipher = new SkipjackEngine(); break; case CipherAlgorithm.TEA: blockCipher = new TeaEngine(); break; case CipherAlgorithm.TWOFISH: blockCipher = new TwofishEngine(); break; case CipherAlgorithm.VMPC: streamCipher = new VmpcEngine(); break; case CipherAlgorithm.VMPC_KSA3: streamCipher = new VmpcKsa3Engine(); break; case CipherAlgorithm.XTEA: blockCipher = new XteaEngine(); break; default: throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); } if (streamCipher != null) { if (parts.Length > 1) throw new ArgumentException("Modes and paddings not used for stream ciphers"); return new BufferedStreamCipher(streamCipher); } bool cts = false; bool padded = true; IBlockCipherPadding padding = null; IAeadBlockCipher aeadBlockCipher = null; if (parts.Length > 2) { if (streamCipher != null) throw new ArgumentException("Paddings not used for stream ciphers"); string paddingName = parts[2]; CipherPadding cipherPadding; if (paddingName == "") { cipherPadding = CipherPadding.RAW; } else if (paddingName == "X9.23PADDING") { cipherPadding = CipherPadding.X923PADDING; } else { try { cipherPadding = (CipherPadding)Enums.GetEnumValue(typeof(CipherPadding), paddingName); } catch (ArgumentException) { throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); } } switch (cipherPadding) { case CipherPadding.NOPADDING: padded = false; break; case CipherPadding.RAW: break; case CipherPadding.ISO10126PADDING: case CipherPadding.ISO10126D2PADDING: case CipherPadding.ISO10126_2PADDING: padding = new ISO10126d2Padding(); break; case CipherPadding.ISO7816_4PADDING: case CipherPadding.ISO9797_1PADDING: padding = new ISO7816d4Padding(); break; case CipherPadding.ISO9796_1: case CipherPadding.ISO9796_1PADDING: asymBlockCipher = new ISO9796d1Encoding(asymBlockCipher); break; case CipherPadding.OAEP: case CipherPadding.OAEPPADDING: asymBlockCipher = new OaepEncoding(asymBlockCipher); break; case CipherPadding.OAEPWITHMD5ANDMGF1PADDING: asymBlockCipher = new OaepEncoding(asymBlockCipher, new MD5Digest()); break; case CipherPadding.OAEPWITHSHA1ANDMGF1PADDING: case CipherPadding.OAEPWITHSHA_1ANDMGF1PADDING: asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha1Digest()); break; case CipherPadding.OAEPWITHSHA224ANDMGF1PADDING: case CipherPadding.OAEPWITHSHA_224ANDMGF1PADDING: asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha224Digest()); break; case CipherPadding.OAEPWITHSHA256ANDMGF1PADDING: case CipherPadding.OAEPWITHSHA_256ANDMGF1PADDING: asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha256Digest()); break; case CipherPadding.OAEPWITHSHA384ANDMGF1PADDING: case CipherPadding.OAEPWITHSHA_384ANDMGF1PADDING: asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha384Digest()); break; case CipherPadding.OAEPWITHSHA512ANDMGF1PADDING: case CipherPadding.OAEPWITHSHA_512ANDMGF1PADDING: asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha512Digest()); break; case CipherPadding.PKCS1: case CipherPadding.PKCS1PADDING: asymBlockCipher = new Pkcs1Encoding(asymBlockCipher); break; case CipherPadding.PKCS5: case CipherPadding.PKCS5PADDING: case CipherPadding.PKCS7: case CipherPadding.PKCS7PADDING: padding = new Pkcs7Padding(); break; case CipherPadding.TBCPADDING: padding = new TbcPadding(); break; case CipherPadding.WITHCTS: cts = true; break; case CipherPadding.X923PADDING: padding = new X923Padding(); break; case CipherPadding.ZEROBYTEPADDING: padding = new ZeroBytePadding(); break; default: throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); } } string mode = ""; if (parts.Length > 1) { mode = parts[1]; int di = GetDigitIndex(mode); string modeName = di >= 0 ? mode.Substring(0, di) : mode; try { CipherMode cipherMode = modeName == "" ? CipherMode.NONE : (CipherMode)Enums.GetEnumValue(typeof(CipherMode), modeName); switch (cipherMode) { case CipherMode.ECB: case CipherMode.NONE: break; case CipherMode.CBC: blockCipher = new CbcBlockCipher(blockCipher); break; case CipherMode.CCM: aeadBlockCipher = new CcmBlockCipher(blockCipher); break; case CipherMode.CFB: { int bits = (di < 0) ? 8 * blockCipher.GetBlockSize() : int.Parse(mode.Substring(di)); blockCipher = new CfbBlockCipher(blockCipher, bits); break; } case CipherMode.CTR: blockCipher = new SicBlockCipher(blockCipher); break; case CipherMode.CTS: cts = true; blockCipher = new CbcBlockCipher(blockCipher); break; case CipherMode.EAX: aeadBlockCipher = new EaxBlockCipher(blockCipher); break; case CipherMode.GCM: aeadBlockCipher = new GcmBlockCipher(blockCipher); break; case CipherMode.GOFB: blockCipher = new GOfbBlockCipher(blockCipher); break; case CipherMode.OCB: aeadBlockCipher = new OcbBlockCipher(blockCipher, CreateBlockCipher(cipherAlgorithm)); break; case CipherMode.OFB: { int bits = (di < 0) ? 8 * blockCipher.GetBlockSize() : int.Parse(mode.Substring(di)); blockCipher = new OfbBlockCipher(blockCipher, bits); break; } case CipherMode.OPENPGPCFB: blockCipher = new OpenPgpCfbBlockCipher(blockCipher); break; case CipherMode.SIC: if (blockCipher.GetBlockSize() < 16) { throw new ArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)"); } blockCipher = new SicBlockCipher(blockCipher); break; default: throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); } } catch (ArgumentException) { throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); } } if (aeadBlockCipher != null) { if (cts) throw new SecurityUtilityException("CTS mode not valid for AEAD ciphers."); if (padded && parts.Length > 2 && parts[2] != "") throw new SecurityUtilityException("Bad padding specified for AEAD cipher."); return new BufferedAeadBlockCipher(aeadBlockCipher); } if (blockCipher != null) { if (cts) { return new CtsBlockCipher(blockCipher); } if (padding != null) { return new PaddedBufferedBlockCipher(blockCipher, padding); } if (!padded || blockCipher.IsPartialBlockOkay) { return new BufferedBlockCipher(blockCipher); } return new PaddedBufferedBlockCipher(blockCipher); } if (asymBlockCipher != null) { return new BufferedAsymmetricBlockCipher(asymBlockCipher); } throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); }
/// <summary> /// Encapsulates the specified links into a CCF container. /// </summary> /// <param name="name">The name of the package.</param> /// <param name="links">The links.</param> /// <returns> /// CCF container. /// </returns> public static byte[] CreateCCF(string name, string[] links) { var sb = new StringBuilder(); sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sb.Append("<CryptLoad>"); sb.Append("<Package service=\"\" name=\"" + name + "\" url=\"Directlinks\">"); foreach (var link in links) { sb.Append("<Download Url=\"" + link + "\">"); sb.Append("<Url>" + link + "</Url>"); //sb.Append("<FileName></FileName>"); //sb.Append("<FileSize></FileSize>"); sb.Append("</Download>"); } sb.Append("</Package>"); sb.Append("</CryptLoad>"); var aes = new AesEngine(); var cbc = new CbcBlockCipher(aes); var pk7 = new Pkcs7Padding(); var pad = new PaddedBufferedBlockCipher(cbc, pk7); pad.Init(true, new ParametersWithIV(new KeyParameter(CCFKey), CCFIV)); return pad.DoFinal(Encoding.UTF8.GetBytes(sb.ToString())); }
public static IBufferedCipher GetCipher( string algorithm) { if (algorithm == null) throw new ArgumentNullException("algorithm"); algorithm = algorithm.ToUpper(CultureInfo.InvariantCulture); string aliased = (string) algorithms[algorithm]; if (aliased != null) algorithm = aliased; IBasicAgreement iesAgreement = null; if (algorithm == "IES") { iesAgreement = new DHBasicAgreement(); } else if (algorithm == "ECIES") { iesAgreement = new ECDHBasicAgreement(); } if (iesAgreement != null) { return new BufferedIesCipher( new IesEngine( iesAgreement, new Kdf2BytesGenerator( new Sha1Digest()), new HMac( new Sha1Digest()))); } if (algorithm.StartsWith("PBE")) { switch (algorithm) { case "PBEWITHSHAAND2-KEYTRIPLEDES-CBC": case "PBEWITHSHAAND3-KEYTRIPLEDES-CBC": return new PaddedBufferedBlockCipher( new CbcBlockCipher(new DesEdeEngine())); case "PBEWITHSHAAND128BITRC2-CBC": case "PBEWITHSHAAND40BITRC2-CBC": return new PaddedBufferedBlockCipher( new CbcBlockCipher(new RC2Engine())); case "PBEWITHSHAAND128BITAES-CBC-BC": case "PBEWITHSHAAND192BITAES-CBC-BC": case "PBEWITHSHAAND256BITAES-CBC-BC": case "PBEWITHSHA256AND128BITAES-CBC-BC": case "PBEWITHSHA256AND192BITAES-CBC-BC": case "PBEWITHSHA256AND256BITAES-CBC-BC": case "PBEWITHMD5AND128BITAES-CBC-OPENSSL": case "PBEWITHMD5AND192BITAES-CBC-OPENSSL": case "PBEWITHMD5AND256BITAES-CBC-OPENSSL": return new PaddedBufferedBlockCipher( new CbcBlockCipher(new AesFastEngine())); case "PBEWITHSHA1ANDDES-CBC": return new PaddedBufferedBlockCipher( new CbcBlockCipher(new DesEngine())); case "PBEWITHSHA1ANDRC2-CBC": return new PaddedBufferedBlockCipher( new CbcBlockCipher(new RC2Engine())); } } string[] parts = algorithm.Split('/'); IBlockCipher blockCipher = null; IAsymmetricBlockCipher asymBlockCipher = null; IStreamCipher streamCipher = null; switch (parts[0]) { case "AES": blockCipher = new AesFastEngine(); break; case "ARC4": streamCipher = new RC4Engine(); break; case "BLOWFISH": blockCipher = new BlowfishEngine(); break; case "CAMELLIA": blockCipher = new CamelliaEngine(); break; case "CAST5": blockCipher = new Cast5Engine(); break; case "CAST6": blockCipher = new Cast6Engine(); break; case "DES": blockCipher = new DesEngine(); break; case "DESEDE": blockCipher = new DesEdeEngine(); break; case "ELGAMAL": asymBlockCipher = new ElGamalEngine(); break; case "GOST28147": blockCipher = new Gost28147Engine(); break; case "HC128": streamCipher = new HC128Engine(); break; case "HC256": streamCipher = new HC256Engine(); break; #if INCLUDE_IDEA case "IDEA": blockCipher = new IdeaEngine(); break; #endif case "NOEKEON": blockCipher = new NoekeonEngine(); break; case "PBEWITHSHAAND128BITRC4": case "PBEWITHSHAAND40BITRC4": streamCipher = new RC4Engine(); break; case "RC2": blockCipher = new RC2Engine(); break; case "RC5": blockCipher = new RC532Engine(); break; case "RC5-64": blockCipher = new RC564Engine(); break; case "RC6": blockCipher = new RC6Engine(); break; case "RIJNDAEL": blockCipher = new RijndaelEngine(); break; case "RSA": asymBlockCipher = new RsaBlindedEngine(); break; case "SALSA20": streamCipher = new Salsa20Engine(); break; case "SEED": blockCipher = new SeedEngine(); break; case "SERPENT": blockCipher = new SerpentEngine(); break; case "SKIPJACK": blockCipher = new SkipjackEngine(); break; case "TEA": blockCipher = new TeaEngine(); break; case "TWOFISH": blockCipher = new TwofishEngine(); break; case "VMPC": streamCipher = new VmpcEngine(); break; case "VMPC-KSA3": streamCipher = new VmpcKsa3Engine(); break; case "XTEA": blockCipher = new XteaEngine(); break; default: throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); } if (streamCipher != null) { if (parts.Length > 1) throw new ArgumentException("Modes and paddings not used for stream ciphers"); return new BufferedStreamCipher(streamCipher); } bool cts = false; bool padded = true; IBlockCipherPadding padding = null; IAeadBlockCipher aeadBlockCipher = null; if (parts.Length > 2) { if (streamCipher != null) throw new ArgumentException("Paddings not used for stream ciphers"); switch (parts[2]) { case "NOPADDING": padded = false; break; case "": case "RAW": break; case "ISO10126PADDING": case "ISO10126D2PADDING": case "ISO10126-2PADDING": padding = new ISO10126d2Padding(); break; case "ISO7816-4PADDING": case "ISO9797-1PADDING": padding = new ISO7816d4Padding(); break; case "ISO9796-1": case "ISO9796-1PADDING": asymBlockCipher = new ISO9796d1Encoding(asymBlockCipher); break; case "OAEP": case "OAEPPADDING": asymBlockCipher = new OaepEncoding(asymBlockCipher); break; case "OAEPWITHMD5ANDMGF1PADDING": asymBlockCipher = new OaepEncoding(asymBlockCipher, new MD5Digest()); break; case "OAEPWITHSHA1ANDMGF1PADDING": case "OAEPWITHSHA-1ANDMGF1PADDING": asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha1Digest()); break; case "OAEPWITHSHA224ANDMGF1PADDING": case "OAEPWITHSHA-224ANDMGF1PADDING": asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha224Digest()); break; case "OAEPWITHSHA256ANDMGF1PADDING": case "OAEPWITHSHA-256ANDMGF1PADDING": asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha256Digest()); break; case "OAEPWITHSHA384ANDMGF1PADDING": case "OAEPWITHSHA-384ANDMGF1PADDING": asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha384Digest()); break; case "OAEPWITHSHA512ANDMGF1PADDING": case "OAEPWITHSHA-512ANDMGF1PADDING": asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha512Digest()); break; case "PKCS1": case "PKCS1PADDING": asymBlockCipher = new Pkcs1Encoding(asymBlockCipher); break; case "PKCS5": case "PKCS5PADDING": case "PKCS7": case "PKCS7PADDING": padding = new Pkcs7Padding(); break; case "TBCPADDING": padding = new TbcPadding(); break; case "WITHCTS": cts = true; break; case "X9.23PADDING": case "X923PADDING": padding = new X923Padding(); break; case "ZEROBYTEPADDING": padding = new ZeroBytePadding(); break; default: throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); } } string mode = ""; if (parts.Length > 1) { mode = parts[1]; int di = GetDigitIndex(mode); string modeName = di >= 0 ? mode.Substring(0, di) : mode; switch (modeName) { case "": case "ECB": case "NONE": break; case "CBC": blockCipher = new CbcBlockCipher(blockCipher); break; case "CCM": aeadBlockCipher = new CcmBlockCipher(blockCipher); break; case "CFB": { int bits = (di < 0) ? 8 * blockCipher.GetBlockSize() : int.Parse(mode.Substring(di)); blockCipher = new CfbBlockCipher(blockCipher, bits); break; } case "CTR": blockCipher = new SicBlockCipher(blockCipher); break; case "CTS": cts = true; blockCipher = new CbcBlockCipher(blockCipher); break; case "EAX": aeadBlockCipher = new EaxBlockCipher(blockCipher); break; case "GCM": aeadBlockCipher = new GcmBlockCipher(blockCipher); break; case "GOFB": blockCipher = new GOfbBlockCipher(blockCipher); break; case "OFB": { int bits = (di < 0) ? 8 * blockCipher.GetBlockSize() : int.Parse(mode.Substring(di)); blockCipher = new OfbBlockCipher(blockCipher, bits); break; } case "OPENPGPCFB": blockCipher = new OpenPgpCfbBlockCipher(blockCipher); break; case "SIC": if (blockCipher.GetBlockSize() < 16) { throw new ArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)"); } blockCipher = new SicBlockCipher(blockCipher); break; default: throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); } } if (aeadBlockCipher != null) { if (cts) throw new SecurityUtilityException("CTS mode not valid for AEAD ciphers."); if (padded && parts.Length > 2 && parts[2] != "") throw new SecurityUtilityException("Bad padding specified for AEAD cipher."); return new BufferedAeadBlockCipher(aeadBlockCipher); } if (blockCipher != null) { if (cts) { return new CtsBlockCipher(blockCipher); } if (padding != null) { return new PaddedBufferedBlockCipher(blockCipher, padding); } if (!padded || blockCipher.IsPartialBlockOkay) { return new BufferedBlockCipher(blockCipher); } return new PaddedBufferedBlockCipher(blockCipher); } if (asymBlockCipher != null) { return new BufferedAsymmetricBlockCipher(asymBlockCipher); } throw new SecurityUtilityException("Cipher " + algorithm + " not recognised."); }
public override void PerformTest() { SecureRandom rand = new SecureRandom(new byte[20]); rand.SetSeed(DateTime.Now.Ticks); doTestPadding(new Pkcs7Padding(), rand, Hex.Decode("ffffff0505050505"), Hex.Decode("0000000004040404")); Pkcs7Padding padder = new Pkcs7Padding(); try { padder.PadCount(new byte[8]); Fail("invalid padding not detected"); } catch (InvalidCipherTextException e) { if (!"pad block corrupted".Equals(e.Message)) { Fail("wrong exception for corrupt padding: " + e); } } doTestPadding(new ISO10126d2Padding(), rand, null, null); doTestPadding(new X923Padding(), rand, null, null); doTestPadding(new TbcPadding(), rand, Hex.Decode("ffffff0000000000"), Hex.Decode("00000000ffffffff")); doTestPadding(new ZeroBytePadding(), rand, Hex.Decode("ffffff0000000000"), null); doTestPadding(new ISO7816d4Padding(), rand, Hex.Decode("ffffff8000000000"), Hex.Decode("0000000080000000")); }
public override void ChangePads(string pads) { IBlockCipherPadding padding = null; switch (pads) { case "ISO10126d2": padding = new ISO10126d2Padding(); break; case "ISO7816d4": padding = new ISO7816d4Padding(); break; case "Pkcs7": padding = new Pkcs7Padding(); break; case "Tbc": padding = new TbcPadding(); break; case "X923": padding = new X923Padding(); break; case "ZeroByte": padding = new ZeroBytePadding(); break; } _Cipher = new PaddedBufferedBlockCipher(_Engine, padding); }
/// <summary> /// Encapsulates the specified links into a DLC container. /// </summary> /// <param name="name">The name of the package.</param> /// <param name="links">The links.</param> /// <returns> /// Base-64-encoded DLC container. /// </returns> public static string CreateDLC(string name, string[] links) { var sb = new StringBuilder(); sb.Append("<dlc>"); sb.Append("<header>"); sb.Append("<generator>"); sb.Append("<app>" + Convert.ToBase64String(Encoding.UTF8.GetBytes("RS TV Show Tracker")) + "</app>"); sb.Append("<version>" + Convert.ToBase64String(Encoding.UTF8.GetBytes(Signature.Version)) + "</version>"); sb.Append("<url>" + Convert.ToBase64String(Encoding.UTF8.GetBytes("http://lab.rolisoft.net/")) + "</url>"); sb.Append("</generator>"); sb.Append("<dlcxmlversion>" + Convert.ToBase64String(Encoding.UTF8.GetBytes("20_02_2008")) + "</dlcxmlversion>"); sb.Append("</header>"); sb.Append("<content>"); sb.Append("<package name=\"" + Convert.ToBase64String(Encoding.UTF8.GetBytes(name)) + "\">"); foreach (var link in links) { sb.Append("<file>"); sb.Append("<url>" + Convert.ToBase64String(Encoding.UTF8.GetBytes(link)) + "</url>"); //sb.Append("<filename></filename>"); //sb.Append("<size></size>"); sb.Append("</file>"); } sb.Append("</package>"); sb.Append("</content>"); sb.Append("</dlc>"); var xml = Convert.ToBase64String(Encoding.UTF8.GetBytes(sb.ToString())); var key = BitConverter.ToString(new SHA256CryptoServiceProvider().ComputeHash(BitConverter.GetBytes(DateTime.Now.ToBinary()))).Replace("-", string.Empty).Substring(0, 16); var srv = Utils.GetURL(DLCCrypt, "&data=" + key + "&lid=" + Convert.ToBase64String(Encoding.UTF8.GetBytes("JDOWNLOADER.ORG_" + DLCCrypt + "_3600"))); var rcr = Regex.Match(srv, @"<rc>(.+)</rc>"); if (!rcr.Groups[1].Success) { throw new Exception("The jDownloader DLC encryption service did not return an encryption key."); } var enc = rcr.Groups[1].Value; var aes = new AesEngine(); var cbc = new CbcBlockCipher(aes); var pk7 = new Pkcs7Padding(); var pad = new PaddedBufferedBlockCipher(cbc, pk7); pad.Init(true, new ParametersWithIV(new KeyParameter(Encoding.ASCII.GetBytes(key)), Encoding.ASCII.GetBytes(key))); var xm2 = Convert.ToBase64String(pad.DoFinal(Encoding.ASCII.GetBytes(xml))); return xm2 + enc; }
private BufferedBlockCipher CreateScryptoEngine() { IBlockCipher engine; switch (_MSec.Algorithm) { case ESec.SCRYPTO_AES: engine = new AesEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_AESFAST: engine = new AesFastEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_AESLIGHT: engine = new AesLightEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_BLOWFISH: engine = new BlowfishEngine(); _MSec.KeySize = 56; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAMELLIA: engine = new CamelliaEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAMELLIALIGHT: engine = new CamelliaLightEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAST5: engine = new Cast5Engine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAST6: engine = new Cast6Engine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_DES: engine = new DesEngine(); _MSec.KeySize = 8; _MSec.IVSize = 0; break; case ESec.SCRYPTO_DESEDE: engine = new DesEdeEngine(); _MSec.KeySize = 24; _MSec.IVSize = 0; break; case ESec.SCRYPTO_GOST28147: engine = new Gost28147Engine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_NOEKEON: engine = new NoekeonEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_NULL: engine = new NullEngine(); _MSec.KeySize = 32; _MSec.IVSize = 16; break; case ESec.SCRYPTO_RC2: engine = new RC2Engine(); _MSec.KeySize = 128; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RC532: engine = new RC532Engine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RC564: engine = new RC564Engine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RC6: engine = new RC6Engine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RIJNDAEL: engine = new RijndaelEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_SEED: engine = new SeedEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_SERPENT: engine = new SerpentEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_SKIPJACK: engine = new SkipjackEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_TEA: engine = new TeaEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_TWOFISH: engine = new TwofishEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_XTEA: engine = new XteaEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; default: engine = new AesEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; } switch (_MSec.Mode) { case ESec.MODE_CBC: engine = new CbcBlockCipher(engine); break; case ESec.MODE_CFB: engine = new CfbBlockCipher(engine, 8); break; case ESec.MODE_GOFB: engine = new GOfbBlockCipher(engine); break; case ESec.MODE_OFB: engine = new OfbBlockCipher(engine, 8); break; case ESec.MODE_OPENPGPCFB: engine = new OpenPgpCfbBlockCipher(engine); break; case ESec.MODE_SIC: engine = new SicBlockCipher(engine); break; default: engine = new CbcBlockCipher(engine); break; } IBlockCipherPadding padding = null; switch (_MSec.Padding) { case ESec.PADDING_ISO10126d2: padding = new ISO10126d2Padding(); break; case ESec.PADDING_ISO7816d4: padding = new ISO7816d4Padding(); break; case ESec.PADDING_PKCS7: padding = new Pkcs7Padding(); break; case ESec.PADDING_TBC: padding = new TbcPadding(); break; case ESec.PADDING_X923: padding = new X923Padding(); break; case ESec.PADDING_ZEROBYTE: padding = new ZeroBytePadding(); break; default: padding = new Pkcs7Padding(); break; } return new PaddedBufferedBlockCipher(engine, padding); }
private static void en_de_crypt(en_de_cryption_mode en_de_cryption_mode_current, string file_path_from, string file_path_to, bool delete_file_path_from, bool interactive) { Form form = new Form(); Label password_1_label = new Label(), password_2_label = new Label(); int label_width = 150, label_height = 20, textbox_width = 200, textbox_height = 20, button_width = 80, button_height = 40; Color label_forecolor = Color.LightGreen, password_backcolor = Color.Black, password_forecolor = Color.LightGreen, proceed_backcolor = Color.Black, proceed_forecolor = Color.LightGreen, cancel_backcolor = Color.Black, cancel_forecolor = Color.LightGreen, error_forecolor = Color.Red; ContentAlignment label_text_alignment = ContentAlignment.MiddleRight, button_text_alignment = ContentAlignment.MiddleCenter; TextBox password_1 = new TextBox(), password_2 = new TextBox(); char password_character = '*'; BorderStyle password_border_style = BorderStyle.Fixed3D; Font label_font = new Font(FontFamily.GenericMonospace, (float)10.0), password_font = new Font(FontFamily.GenericMonospace, (float)10.0), button_font = new Font(FontFamily.GenericMonospace, (float)10.0); Button proceed = new Button(), cancel = new Button(); form.StartPosition = FormStartPosition.CenterParent; form.Width = 400; form.Height = 180; form.BackColor = Color.Black; form.Text = "Please, provide the password for the " + (en_de_cryption_mode_current == en_de_cryption_mode.encrypt ? "encryption" : "decryption") + "."; password_1_label.Bounds = new Rectangle(10, 20, label_width, label_height); password_1_label.Font = label_font; password_1_label.ForeColor = label_forecolor; password_1_label.TextAlign = label_text_alignment; password_1_label.Text = "Password:"******"Confirm password:"******"OnClick", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(cancel, new object[] { null }); if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt) { if (!string.IsNullOrEmpty(password_1.Text)) { password_2.Enabled = true; } else { proceed.Enabled = false; password_2.Text = ""; if (password_2.Enabled) password_2.Enabled = false; } } else { if (!string.IsNullOrEmpty(password_1.Text)) proceed.Enabled = true; else proceed.Enabled = false; } if ((Keys)e.KeyValue == Keys.Enter && !string.IsNullOrEmpty(password_1.Text)) { if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt) password_2.Focus(); else proceed.GetType() .GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(proceed, new object[] { new KeyEventArgs(Keys.Enter) }); } }; password_2.Bounds = new Rectangle(160, 50, textbox_width, textbox_height); password_2.BackColor = password_backcolor; password_2.ForeColor = password_forecolor; password_2.BorderStyle = password_border_style; password_2.PasswordChar = password_character; password_2.Font = password_font; password_2.Multiline = false; password_2.Enabled = false; if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt) password_2.KeyUp += (sender, e) => { if ((Keys)e.KeyValue == Keys.Escape) cancel.GetType() .GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(cancel, new object[] { null }); if (string.IsNullOrEmpty(password_2.Text) || password_1.Text.Equals(password_2.Text)) { if (!string.IsNullOrEmpty(password_2.Text)) proceed.Enabled = true; password_2.ForeColor = password_forecolor; if ((Keys)e.KeyValue == Keys.Enter && !string.IsNullOrEmpty(password_2.Text)) proceed.GetType() .GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(proceed, new object[] { new KeyEventArgs(Keys.Enter) }); } else { proceed.Enabled = false; password_2.ForeColor = error_forecolor; } }; proceed.Bounds = new Rectangle(290, 100, button_width, button_height); proceed.BackColor = proceed_backcolor; proceed.ForeColor = proceed_forecolor; proceed.Font = button_font; proceed.TextAlign = button_text_alignment; proceed.Text = "Proceed"; proceed.Enabled = false; proceed.Click += (sender, e) => { form.Close(); byte[] key_string_bytes = Encoding.UTF8.GetBytes(password_1.Text), key = new byte[64]; password_1.Text = ""; password_2.Text = ""; FileStream file_from = null, file_to = null; try { WhirlpoolDigest whirlpool_digest = new WhirlpoolDigest(); whirlpool_digest.BlockUpdate(key_string_bytes, 0, key_string_bytes.Length); whirlpool_digest.DoFinal(key, 0); SerpentEngine serpent_engine = new SerpentEngine(); Pkcs7Padding pkcs7_padding = new Pkcs7Padding(); pkcs7_padding.Init(new SecureRandom(key)); PaddedBufferedBlockCipher padded_buffered_block_cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(serpent_engine), pkcs7_padding); byte[] iv = new byte[padded_buffered_block_cipher.GetBlockSize()]; Array.Copy(key, key.Length - iv.Length, iv, 0, iv.Length); padded_buffered_block_cipher.Init((en_de_cryption_mode_current == en_de_cryption_mode.encrypt), new ParametersWithIV(new KeyParameter(key, 31, 32), iv)); int plaintext_buffer_size = 1024 * padded_buffered_block_cipher.GetBlockSize(), ciphertext_buffer_size = padded_buffered_block_cipher.GetBlockSize() + plaintext_buffer_size, bytes_read_count, bytes_en_de_crypted_count; byte[] buffer_from, buffer_to; if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt) { buffer_from = new byte[plaintext_buffer_size]; buffer_to = new byte[ciphertext_buffer_size]; } else { buffer_from = new byte[ciphertext_buffer_size]; buffer_to = new byte[plaintext_buffer_size]; } file_from = new FileStream(file_path_from, FileMode.Open); file_to = new FileStream(file_path_to, FileMode.Create); while (true) { bytes_read_count = file_from.Read(buffer_from, 0, buffer_from.Length); if (bytes_read_count == 0) { break; } bytes_en_de_crypted_count = padded_buffered_block_cipher.DoFinal(buffer_from, 0, bytes_read_count, buffer_to, 0); file_to.Write(buffer_to, 0, bytes_en_de_crypted_count); } file_from.Close(); file_to.Close(); if (delete_file_path_from) File.Delete(file_path_from); for (int i = 0; i < key_string_bytes.Length; i += 1) key_string_bytes[i] = 0; for (int i = 0; i < key.Length; i += 1) key[i] = 0; whirlpool_digest.Reset(); serpent_engine.Reset(); pkcs7_padding = null; padded_buffered_block_cipher.Reset(); for (int i = 0; i < iv.Length; i += 1) iv[i] = 0; for (int i = 0; i < buffer_from.Length; i += 1) buffer_from[i] = 0; for (int i = 0; i < buffer_to.Length; i += 1) buffer_to[i] = 0; } catch(Exception exception) { string message; if (exception.Message.Contains("pad block corrupted")) { if (file_from != null) { file_from.Close(); } if (file_to != null) { file_to.Close(); } try { File.Delete(file_path_to); } catch(Exception) {} message = "A wrong decryption key has been provided."; } else { message = exception.Message; } alert(message, interactive); } }; cancel.Bounds = new Rectangle(200, 100, button_width, button_height); cancel.BackColor = cancel_backcolor; cancel.ForeColor = cancel_forecolor; cancel.Font = button_font; cancel.TextAlign = button_text_alignment; cancel.Text = "Cancel"; cancel.Click += (sender, e) => { form.Close(); password_1.Text = ""; password_2.Text = ""; }; form.Controls.Add(password_1_label); form.Controls.Add(password_2_label); form.Controls.Add(password_1); form.Controls.Add(password_2); form.Controls.Add(proceed); form.Controls.Add(cancel); form.ShowDialog(); }