コード例 #1
0
ファイル: CryptRSATest.cs プロジェクト: ResGear/CryptobySharp
        public virtual void testRSACrypt1024false()
        {
            System.Console.Out.WriteLine("RSACrypt1024false");
            int            keySize   = 1024;
            CryptobyClient client    = new CryptobyClient();
            CryptobyCore   core      = new CryptobyCore(client);
            KeyGenRSA      generator = new KeyGenRSA(core);

            generator.initGenerator(keySize);
            byte[] plainInput = Encoding.UTF8.GetBytes("Text to Test for Testing from Tester by Testcase"
                                                       );
            string publicKeyString = generator.getPublicKey();

            byte[] publicKey = CryptobyHelper.hexStringToBytes(publicKeyString);
            generator.initGenerator(keySize);
            string privateKeyString = generator.getPrivateKey();

            byte[]   privateKey = CryptobyHelper.hexStringToBytes(privateKeyString);
            CryptRSA rsa        = new CryptRSA();

            byte[] expResult = plainInput;
            byte[] result    = rsa.encrypt(plainInput, publicKey);
            result = rsa.decrypt(result, privateKey);
            NUnit.Framework.Assert.IsFalse(Arrays.equals(expResult, result));
        }
コード例 #2
0
        /// <summary>This method load a key file from disk to byte array.</summary>
        /// <remarks>
        /// This method load a key file from disk to byte array. They key in the file
        /// must be in Hex String Block format which will be merged and converted
        /// from hex String to byte array as output.
        /// </remarks>
        /// <param name="filePath">Path to key file which bytes will be loaded</param>
        /// <returns>Byte array of hex string in filePath</returns>
        /// <exception cref="System.IO.IOException">
        /// If file not found or other IO problems there will be
        /// throw an IOException
        /// </exception>
        public static byte[] getKeyFromFile(string filePath)
        {
            StreamReader streamReader = new StreamReader(filePath);
            string       sb           = streamReader.ReadToEnd();

            streamReader.Close();
            System.Console.WriteLine(CryptobyHelper.hexStringToBytes(sb));
            return(CryptobyHelper.hexStringToBytes(sb));
        }
コード例 #3
0
ファイル: RsaUI.cs プロジェクト: ResGear/CryptobySharp
 private static void rsaDecrypterText(CryptobyConsole console)
 {
     scanner = new Scanner(java.lang.System.@in);
     // Input encrypted Hex String Text to decrypt
     Console.Out.WriteLine("\nYour Text to decrypt (Type '" + quit + "' to Escape):"
                           );
     // Convert crypted HexString Block to one String
     try
     {
         string cryptText = string.Empty;
         while (!scanner.hasNext(CryptobyHelper.getEOBString()))
         {
             if (scanner.hasNext(quit))
             {
                 rsaCrypterText(console);
             }
             cryptText = cryptText + scanner.next();
         }
         cryptByte = CryptobyHelper.hexStringToBytes(cryptText);
     }
     catch (FormatException)
     {
         // Catch false format of Input
         Console.Out.WriteLine("\nNot allowed Crypted Text! Must be a Upper Hex String!"
                               );
         cryptByte = BigInteger.ZERO.toByteArray();
     }
     // Input the Private Key
     privateKeyByte = scanPrivateKey(console);
     // Initial RSA Crypt Object
     initRSAKeyGen(console);
     // Decrypt the String Text 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 String!!");
         plainByte = null;
         // Press Return for Continues
         CryptobyHelper.pressEnter();
         rsaCrypterText(console);
     }
     Console.Out.WriteLine("\nDecryption finished...");
     // Print decrypted Text
     Console.Out.WriteLine("\nRSA-" + keySize + " decrypted Text:");
     Console.Out.WriteLine(Encoding.UTF8.GetString(plainByte));
     // Reset RSA Crypt Object to release Memory
     initRSAKeyGen(console);
     // Back to Menu rsaCrypter with Enter (Return) Key
     Console.Out.WriteLine("\nGo back to RSA Text Crypter Menu: Press Enter");
     CryptobyHelper.pressEnter();
     rsaCrypterText(console);
 }
コード例 #4
0
ファイル: AesUI.cs プロジェクト: ResGear/CryptobySharp
 private static void aesDecrypterText(CryptobyConsole console)
 {
     scanner = new Scanner(java.lang.System.@in);
     // Input encrypted Hex String Text to decrypt
     Console.Out.WriteLine("\nYour Text to decrypt (Type '" + quit + "' to Escape):"
                           );
     // Convert crypted HexString Block to one String
     try
     {
         string cryptText = string.Empty;
         while (!scanner.hasNext(CryptobyHelper.getEOBString()))
         {
             if (scanner.hasNext(quit))
             {
                 aesCrypterText(console);
             }
             cryptText = cryptText + scanner.next();
         }
         cryptByte = CryptobyHelper.hexStringToBytes(cryptText);
     }
     catch (FormatException)
     {
         // Catch false format of Input
         Console.Out.WriteLine("\nNot allowed Crypted Text! Must be a Upper Hex String!"
                               );
         cryptByte = BigInteger.ZERO.toByteArray();
     }
     // Input your Key for encryption
     key = scanKeyText(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();
         aesCrypterText(console);
     }
     // Print decrypted Text
     Console.Out.WriteLine("\nDecryption finished...");
     Console.Out.WriteLine("\nAES-" + keySize + " decrypted Text:");
     Console.Out.WriteLine(Encoding.UTF8.GetString(plainByte));
     // Back to Text Crypter Menu
     Console.Out.WriteLine("\nGo back to AES Text Crypter Menu: Press Enter");
     CryptobyHelper.pressEnter();
     aesCrypterText(console);
 }
コード例 #5
0
        public virtual void testEncryptDecrypt256()
        {
            System.Console.Out.WriteLine("encrypt and decrypt testphrase");
            byte[] plainInput = Encoding.UTF8.GetBytes("Text to Test for Testing from Tester by Testcase"
                                                       );
            KeyGenSHA3 keyGen = new KeyGenSHA3();
            string     hexKey = keyGen.generateKey(256, "password");

            byte[]   bKey     = CryptobyHelper.hexStringToBytes(hexKey);
            CryptAES instance = new CryptAES();

            byte[] expResult = plainInput;
            byte[] result    = instance.encrypt(plainInput, bKey);
            result = instance.decrypt(result, bKey);
            Assert.AreEqual(expResult, result);
        }
コード例 #6
0
        public virtual void testEncryptDecrypt256_false()
        {
            System.Console.Out.WriteLine("crypt almost false key");
            byte[] plainInput = Encoding.UTF8.GetBytes("Text to Test for Testing from Tester by Testcase"
                                                       );
            string hexKey = "13A9489AF957FF7B5E8E712737D0B4A0C92AE8EBAE9DD11E9C11B8CB79707017";

            byte[]   bKey     = CryptobyHelper.hexStringToBytes(hexKey);
            CryptAES instance = new CryptAES();

            byte[] expResult = plainInput;
            byte[] result    = instance.encrypt(plainInput, bKey);
            hexKey = "13A9489AF957FF7B5E8E712737D0B4A0C92AE8EBAE9DD11E9C11B8CB79707011";
            bKey   = CryptobyHelper.hexStringToBytes(hexKey);
            result = instance.decrypt(result, bKey);
            Assert.IsFalse(Encoding.UTF8.GetString(expResult).Equals
                               (Encoding.UTF8.GetString(result)));
        }
コード例 #7
0
        public virtual void testEncryptDecrypt256_HugeData()
        {
            System.Console.Out.WriteLine("encrypt and decrypt huge Data");
            byte[]     plainInput = new byte[1000000];
            KeyGenSHA3 keyGen     = new KeyGenSHA3();
            string     hexKey     = keyGen.generateKey(256, "password");

            byte[]   bKey     = CryptobyHelper.hexStringToBytes(hexKey);
            CryptAES instance = new CryptAES();

            byte[] expResult = plainInput;
            byte[] result    = instance.encrypt(plainInput, bKey);
            result = instance.decrypt(result, bKey);
            for (int i = 0; i < 10; i++)
            {
                result = instance.encrypt(plainInput, bKey);
                result = instance.decrypt(result, bKey);
            }
            Assert.AreEqual(expResult, result);
        }
コード例 #8
0
ファイル: RsaUI.cs プロジェクト: ResGear/CryptobySharp
 // Help Functions
 private static byte[] scanPrivateKey(CryptobyConsole console)
 {
     byte[] retKey = null;
     do
     {
         scanner = new Scanner(java.lang.System.@in);
         string keyText = string.Empty;
         // Input Private Key for decryption
         Console.Out.WriteLine("\nEnter the private Key (Type '" + quit + "' to Escape):"
                               );
         try
         {
             while (!scanner.hasNext(CryptobyHelper.getEOBString()))
             {
                 if (scanner.hasNext(quit))
                 {
                     rsaCrypterText(console);
                 }
                 keyText = keyText + scanner.next();
             }
             retKey  = CryptobyHelper.hexStringToBytes(keyText);
             keySize = retKey.Length * 4;
         }
         catch (FormatException)
         {
             // Catch false format of Input
             Console.Out.WriteLine("Not allowed Characters in Private Key! Just lower alphanumeric Characters!"
                                   );
             retKey  = BigInteger.ZERO.toByteArray();
             keySize = 0;
         }
         catch (ArgumentNullException)
         {
             Console.Out.WriteLine("NullPointerException catched! Try again!");
             retKey  = BigInteger.ZERO.toByteArray();
             keySize = 0;
         }
     }while (keySize != 1024 && keySize != 2048 && keySize != 4096);
     return(retKey);
 }
コード例 #9
0
        public virtual void testEncryptDecrypt256_CBC()
        {
            System.Console.Out.WriteLine("encrypt and decrypt recurring words");
            byte[] plainInput = Encoding.UTF8.GetBytes("TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest"
                                                       );
            KeyGenSHA3 keyGen = new KeyGenSHA3();
            string     hexKey = keyGen.generateKey(256, "password");

            byte[]   bKey     = CryptobyHelper.hexStringToBytes(hexKey);
            CryptAES instance = new CryptAES();

            byte[] expResult = plainInput;
            byte[] result    = instance.encrypt(plainInput, bKey);
            string resString = CryptobyHelper.bytesToHexStringUpper(result);

            for (int i = 0; i < resString.Length - 32; i += 32)
            {
                Assert.IsFalse(resString.Substring(i, 32).Equals
                                   (resString.Substring(i + 32, 32)));
            }
            result = instance.decrypt(result, bKey);
            Assert.AreEqual(expResult, result);
        }