public void testRun() { int rounds = 50; Console.WriteLine("Test Performance Cryptoby FileManager"); Console.WriteLine("Rounds: " + rounds); Console.WriteLine("Cycles per Round: " + cycles); Console.WriteLine("\nWrite 1MB to File"); file1 = new byte[mb1]; PerfMeter.run(new Action(testWrite1ToFile), rounds); Console.WriteLine("\nRead 1MB from File"); PerfMeter.run(new Action(testRead1FromFile), rounds); file1 = null; Console.WriteLine("\nWrite 10MB to File"); file10 = new byte[mb10]; PerfMeter.run(new Action(testWrite10ToFile), rounds); Console.WriteLine("\nRead 10MB from File"); PerfMeter.run(new Action(testRead10FromFile), rounds); file10 = null; Console.WriteLine("\nWrite 100MB to File"); file100 = new byte[mb100]; PerfMeter.run(new Action(testWrite100ToFile), rounds); Console.WriteLine("\nRead 100MB from File"); PerfMeter.run(new Action(testRead100FromFile), rounds); file100 = null; CryptobyFileManager.putBytesToFile(filePath, new byte[0]); }
public void testWrite100ToFile() { for (int i = 0; i < cycles; i++) { CryptobyFileManager.putBytesToFile(filePath, file100); } }
private static void aesEncrypterFile(CryptobyConsole console) { // Input Path to File for encryption scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter Path to File for encryption (Type '" + quit + "' to Escape):"); scanner.useDelimiter("\n"); if (scanner.hasNext(quit)) { aesCrypterFile(console); } plainFilePath = scanner.next(); // Get Bytes from PlainFile try { plainByte = CryptobyFileManager.getBytesFromFile(plainFilePath); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); aesCrypterFile(console); } // Input Path saving Path scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter Path to save encrypted File (Type '" + quit + "' to Escape):"); scanner.useDelimiter("\n"); if (scanner.hasNext(quit)) { aesCrypterFile(console); } cryptFilePath = scanner.next(); // Input Key File for encryption key = scanKeyFile(console); // Initial AES Crypt Object initAESKeyGen(console); // Encrypt the String Text with given Key Console.Out.WriteLine("\nEncrypting in progress..."); cryptByte = console.getCore().getCryptSym().encrypt(plainByte, key); Console.Out.WriteLine("\nEncryption successfull. Saving File now..."); //Put encrypted Bytes to File try { CryptobyFileManager.putBytesToFile(cryptFilePath, cryptByte); } catch (System.IO.IOException ex) { Logger.getLogger(typeof(RsaUI).FullName).log(Level.SEVERE, null, ex); } Console.Out.WriteLine("\nEncrypted File saved to this Path:"); Console.Out.WriteLine(cryptFilePath); // Reset Variables initAESKeyGen(console); cryptByte = null; plainByte = null; key = null; // Back to File Crypter Menu Console.Out.WriteLine("\nGo back to AES File Crypter Menu: Press Enter"); CryptobyHelper.pressEnter(); aesCrypterFile(console); }
private static void aesDecrypterFile(CryptobyConsole console) { // Input Path to File for decryption scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter Path to decrypt a File (Type '" + quit + "' to Escape):" ); scanner.useDelimiter("\n"); if (scanner.hasNext(quit)) { aesCrypterFile(console); } cryptFilePath = scanner.next(); try { // Get Bytes from PlainFile cryptByte = CryptobyFileManager.getBytesFromFile(cryptFilePath); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); aesCrypterFile(console); } // Input Path saving Path scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter Path to save decrypted File (Type '" + quit + "' to Escape):"); scanner.useDelimiter("\n"); if (scanner.hasNext(quit)) { aesCrypterFile(console); } plainFilePath = scanner.next(); // Input your Key for encryption key = scanKeyFile(console); // Initial AES Crypt Object initAESKeyGen(console); // Decrypt the String Text with given Key Console.Out.WriteLine("\nDecrypting in progress..."); try { plainByte = console.getCore().getCryptSym().decrypt(cryptByte, key); } catch (Exception) { Console.Out.WriteLine("\nUnable to decrypt this String!!"); // Enter for Continues CryptobyHelper.pressEnter(); aesCrypterFile(console); } Console.Out.WriteLine("\nDecryption finished. Saving File now..."); try { //Put encrypted Bytes to File CryptobyFileManager.putBytesToFile(plainFilePath, plainByte); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); aesCrypterFile(console); } Console.Out.WriteLine("\nDecrypted File saved to this Path:"); Console.Out.WriteLine(plainFilePath); // Reset Variables initAESKeyGen(console); cryptByte = null; plainByte = null; key = null; // Back to File Crypter Menu Console.Out.WriteLine("\nGo back to AES File Crypter Menu: Press Enter"); CryptobyHelper.pressEnter(); aesCrypterFile(console); }
private static void rsaDecrypterFile(CryptobyConsole console) { // Input Path to File for decryption scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter Path to File for Decryption (Type '" + quit + "' to Escape):"); scanner.useDelimiter("\n"); if (scanner.hasNext(quit)) { rsaCrypterFile(console); } cryptFilePath = scanner.next(); // Get Bytes from PlainFile try { cryptByte = CryptobyFileManager.getBytesFromFile(cryptFilePath); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); rsaCrypterFile(console); } // Input Path saving Path scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter saving Path for decrypted File (Type '" + quit + "' to Escape):"); scanner.useDelimiter("\n"); if (scanner.hasNext(quit)) { rsaCrypterFile(console); } plainFilePath = scanner.next(); // Input Path to Key File for decryption scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter Path to Private Key File (Type '" + quit + "' to Escape):" ); scanner.useDelimiter("\n"); if (scanner.hasNext(quit)) { rsaCrypterFile(console); } privateKeyPath = scanner.next(); // Get Bytes from Private Key File try { privateKeyByte = CryptobyFileManager.getKeyFromFile(privateKeyPath); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); rsaCrypterFile(console); } catch (FormatException) { Console.Out.WriteLine("Key File format is not correct!"); rsaCrypterFile(console); } // Initial RSA Crypt Object initRSAKeyGen(console); // Encrypt the File with given Key Console.Out.WriteLine("\nDecryption in progress..."); try { plainByte = console.getCore().getCryptAsym().decrypt(cryptByte, privateKeyByte); } catch (Exception) { Console.Out.WriteLine("\nUnable to decrypt this File!!"); plainByte = null; // Press Return for Continues CryptobyHelper.pressEnter(); rsaCrypterFile(console); } Console.Out.WriteLine("\nDecryption finished. Saving File now..."); try { //Put encrypted Bytes to File CryptobyFileManager.putBytesToFile(plainFilePath, plainByte); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); rsaCrypterFile(console); } Console.Out.WriteLine("\nDecrypted File saved to this Path:"); Console.Out.WriteLine(plainFilePath); // Reset Variables initRSAKeyGen(console); cryptByte = null; plainByte = null; publicKeyByte = null; // Back to Menu rsaCrypter with Enter (Return) Key Console.Out.WriteLine("\nGo back to RSA File Crypter Menu: Press Enter"); CryptobyHelper.pressEnter(); rsaCrypterFile(console); }
private static void rsaEncGenKeysFile(CryptobyConsole console) { scanner = new Scanner(java.lang.System.@in); // Set Default Key Size keySize = 1024; do { Console.Out.WriteLine("\n"); Console.Out.WriteLine("Choose Key Size in Bit"); Console.Out.WriteLine("----------------------\n"); Console.Out.WriteLine("1 - 1024"); Console.Out.WriteLine("2 - 2048"); Console.Out.WriteLine("3 - 4096"); Console.Out.WriteLine("4 - Back"); Console.Out.Write("Enter Number: "); while (!scanner.hasNextInt()) { Console.Out.Write("That's not a number! Enter 1,2,3 or 4: "); scanner.next(); } choice = scanner.nextInt(); }while (choice < 1 || choice > 4); switch (choice) { case 1: { keySize = 1024; break; } case 2: { keySize = 2048; break; } case 3: { keySize = 4096; break; } case 4: { rsaCrypterFile(console); break; } default: { rsaEncGenKeysFile(console); break; } } // Input Path to File for encryption scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter Path to File for encryption (Type '" + quit + "' to Escape):"); scanner.useDelimiter("\n"); if (scanner.hasNext(quit)) { rsaCrypterFile(console); } plainFilePath = scanner.next(); // Input Path to File for encryption scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter Path to saving encrypted File (Type '" + quit + "' to Escape):"); scanner.useDelimiter("\n"); if (scanner.hasNext(quit)) { rsaCrypterFile(console); } cryptFilePath = scanner.next(); // Input Path for saving Private Key scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter Path to saving Private Key (Type '" + quit + "' to Escape):"); scanner.useDelimiter("\n"); if (scanner.hasNext(quit)) { rsaCrypterFile(console); } privateKeyPath = scanner.next(); // Input Path for saving Public Key scanner = new Scanner(java.lang.System.@in); Console.Out.WriteLine("Enter Path to saving Public Key (Type '" + quit + "' to Escape):" ); scanner.useDelimiter("\n"); publicKeyPath = scanner.next(); // Get Bytes from PlainFile try { plainByte = CryptobyFileManager.getBytesFromFile(plainFilePath); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); rsaCrypterFile(console); } // Initial RSA Crypt Object initRSAKeyGen(console); // Get Public Key in Bytecode Console.Out.WriteLine("\nGenerate Private and Public RSA Keys..."); console.getCore().getKeyGenAsym().initGenerator(keySize); publicKeyByte = console.getCore().getKeyGenAsym().getPublicKeyByte(); // Get Public and Private Key as String string publicKey = console.getCore().getKeyGenAsym().getPublicKey(); string privateKey = console.getCore().getKeyGenAsym().getPrivateKey(); // Encrypt the File with given Key Console.Out.WriteLine("\nEncryption in progress..."); cryptByte = console.getCore().getCryptAsym().encrypt(plainByte, publicKeyByte); Console.Out.WriteLine("\nEncryption successfull. Saving File now..."); //Put encrypted Bytes to File try { CryptobyFileManager.putBytesToFile(cryptFilePath, cryptByte); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); rsaCrypterFile(console); } Console.Out.WriteLine("\nEncrypted File saved to this Path:"); Console.Out.WriteLine(cryptFilePath); //Put private Key to File try { CryptobyFileManager.putKeyToFile(privateKeyPath, privateKey); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); rsaCrypterFile(console); } Console.Out.WriteLine("\nPrivate Key File saved to this Path:"); Console.Out.WriteLine(privateKeyPath); //Put public Key to File try { CryptobyFileManager.putKeyToFile(publicKeyPath, publicKey); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); rsaCrypterFile(console); } Console.Out.WriteLine("\nPublic Key File saved to this Path:"); Console.Out.WriteLine(publicKeyPath); // Reset Variables initRSAKeyGen(console); cryptByte = null; plainByte = null; publicKeyByte = null; // Back to Menu rsaCrypter with Enter (Return) Key Console.Out.WriteLine("\nGo back to RSA File Crypter Menu: Press Enter"); CryptobyHelper.pressEnter(); rsaCrypterFile(console); }
public virtual void testGetAndPutByteFiles() { System.Console.Out.WriteLine("Put Plaintext, get Plainfile, encrypt and decrypt Byte Files"); for (int i = 1; i < 100; i += 3) { string filePathPlain = "test.txt"; string filePathEnc = "test.cty"; string filePathDec = "test2.txt"; int keySize = 1024; byte[] testBytes = new byte[i * 100 + i]; new Random().nextBytes(testBytes); CryptobyClient client = new CryptobyClient(); CryptobyCore core = new CryptobyCore(client); CryptRSA rsa = new CryptRSA(); KeyGenRSA generator = new KeyGenRSA(core); try { CryptobyFileManager.putBytesToFile(filePathPlain, testBytes); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } generator.initGenerator(keySize); byte[] publicKey = generator.getPublicKeyByte(); byte[] privateKey = generator.getPrivateKeyByte(); byte[] plainInput = null; try { plainInput = CryptobyFileManager.getBytesFromFile(filePathPlain); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } Assert.AreEqual(testBytes, plainInput); byte[] encrypt = rsa.encrypt(plainInput, publicKey); // Put encrypted Bytes from File try { CryptobyFileManager.putBytesToFile(filePathEnc, encrypt); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } // Get encrypted Bytes from File byte[] fileEncrypt = null; try { fileEncrypt = CryptobyFileManager.getBytesFromFile(filePathEnc); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } Assert.AreEqual(fileEncrypt, encrypt); byte[] decrypt = rsa.decrypt(fileEncrypt, privateKey); Assert.AreEqual(testBytes, decrypt); try { CryptobyFileManager.putBytesToFile(filePathDec, decrypt); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } } }