private static byte[] scanKeyFile(CryptobyConsole console) { scanner = new Scanner(java.lang.System.@in); byte[] tempKey = null; do { Console.Out.WriteLine("\nAllowed Key Sizes 128,192 and 256 Bit."); Console.Out.WriteLine("Enter Path to Key File (Type '" + quit + "' to Escape):" ); if (scanner.hasNext(quit)) { aesCrypterFile(console); } keyPath = scanner.next(); try { tempKey = CryptobyFileManager.getKeyFromFile(keyPath); } catch (System.IO.IOException) { CryptobyHelper.printIOExp(); aesCrypterFile(console); } catch (FormatException) { Console.Out.WriteLine("Key File format is not correct!"); aesCrypterFile(console); } keySize = tempKey.Length * 4; }while (keySize != 128 && keySize != 192 && keySize != 256); return(tempKey); }
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); }
public virtual void testPutAndGetKey() { System.Console.Out.WriteLine("Put and Get Keys"); string publicKeyFilePath = "publicKey.pub"; string privateKeyFilePath = "privateKey.prv"; int keySize = 1024; CryptobyClient client = new CryptobyClient(); CryptobyCore core = new CryptobyCore(client); KeyGenRSA generator = new KeyGenRSA(core); generator.initGenerator(keySize); byte[] publicKeyByte = generator.getPublicKeyByte(); byte[] privateKeyByte = generator.getPrivateKeyByte(); string publicKey = generator.getPublicKey(); string privateKey = generator.getPrivateKey(); try { CryptobyFileManager.putKeyToFile(publicKeyFilePath, publicKey); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } try { CryptobyFileManager.putKeyToFile(privateKeyFilePath, privateKey); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } byte[] resultPublic = null; try { resultPublic = CryptobyFileManager.getKeyFromFile(publicKeyFilePath); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } byte[] resultPrivate = null; try { resultPrivate = CryptobyFileManager.getKeyFromFile(privateKeyFilePath); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } Assert.AreEqual(publicKeyByte, resultPublic); Assert.AreEqual(privateKeyByte, resultPrivate); Assert.AreEqual(publicKey, CryptobyHelper.bytesToHexString(resultPublic )); Assert.AreEqual(privateKey, CryptobyHelper.bytesToHexString(resultPrivate )); }