Module: EncryptDecrypt
- Defined in:
- lib/accu-encrypt.rb
Overview
The primary module which packages the classes and global methods to be used for encryption.
Defined Under Namespace
Classes: EncryptedClient, EncryptedServer
Class Method Summary collapse
-
.decrypt(secret, plainText, *args) ⇒ Object
Decrypts based on a continuing password offset algorithm.
-
.decrypt_file(filename, pass, *args) ⇒ Object
Decrypts a file.
-
.encrypt(secret, source, *args) ⇒ Object
Encrypts based on a continuing password offset algorithm.
-
.encrypt_file(filename, pass, *args) ⇒ Object
Encrypts a file.
-
.get_mode ⇒ Object
Returns current encryption mode.
-
.get_password(prompt = "Password: ") ⇒ Object
Reads a password using the highline gem.
-
.interactive ⇒ Object
Chooses either interactive mode based on operating system.
-
.interactive_help ⇒ Object
Returns interactive help in a string.
-
.interactive_terminal ⇒ Object
Opens an interactive terminal session.
-
.interactive_windows ⇒ Object
Opens an interactive session with the WindowTerminal library.
-
.set_mode(arg) ⇒ Object
Set current encryption mode.
-
.windows(manager) ⇒ Object
Declares the default windows on a manager object.
Class Method Details
.decrypt(secret, plainText, *args) ⇒ Object
Decrypts based on a continuing password offset algorithm.
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/accu-encrypt.rb', line 367 def self.decrypt(secret,plainText,*args) currentKey = secret.clone output = "" plainText.each_byte { |plain| secondbyte = currentKey[0].ord out = plain - secondbyte while out < 0 do out = out + 255 end output[output.length] = out.chr currentKey[currentKey.length] = out.chr currentKey.slice! 0 } if args[0] == :continuing then return secret,output,currentKey elsif args[0] == :double then return self.decrypt(secret,output) else return secret,output end end |
.decrypt_file(filename, pass, *args) ⇒ Object
Decrypts a file.
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/accu-encrypt.rb', line 410 def self.decrypt_file(filename,pass,*args) if File.exists? filename then if args[0] != :double then file = File.open filename pass,output = *self.decrypt(pass,file.read) file.close File.delete filename File.open(filename,"w") do |filer| filer << output end else self.decrypt_file(filename,pass) self.decrypt_file(filename,pass) end else puts "ERROR" end end |
.encrypt(secret, source, *args) ⇒ Object
Encrypts based on a continuing password offset algorithm.
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/accu-encrypt.rb', line 343 def self.encrypt(secret,source,*args) currentKey = secret.clone output = "" source.each_byte { |orig| secondbyte = currentKey[0].ord out = orig + secondbyte while out > 255 do out = out - 255 end output[output.length] = out.chr currentKey[currentKey.length] = orig.chr currentKey.slice! 0 } if args[0] == :continuing then return secret,output,currentKey elsif args[0] == :double then return self.encrypt(secret,output) else return secret,output end end |
.encrypt_file(filename, pass, *args) ⇒ Object
Encrypts a file.
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/accu-encrypt.rb', line 390 def self.encrypt_file(filename,pass,*args) if File.exists? filename then if args[0] != :double then file = File.open filename pass,output = *self.encrypt(pass,file.read) file.close File.delete filename File.open(filename,"w") do |filer| filer << output end else self.encrypt_file(filename,pass) self.encrypt_file(filename,pass) end else puts "ERROR" end end |
.get_mode ⇒ Object
Returns current encryption mode.
811 812 813 |
# File 'lib/accu-encrypt.rb', line 811 def self.get_mode() @@mode.dup end |
.get_password(prompt = "Password: ") ⇒ Object
Reads a password using the highline gem.
824 825 826 |
# File 'lib/accu-encrypt.rb', line 824 def self.get_password(prompt="Password: ") ask(prompt) {|q| q.echo = false} end |
.interactive ⇒ Object
Chooses either interactive mode based on operating system.
801 802 803 804 805 806 807 |
# File 'lib/accu-encrypt.rb', line 801 def self.interactive() if defined? WindowTerminal and WindowTerminal.os == :linux then self.interactive_windows else self.interactive_terminal end end |
.interactive_help ⇒ Object
Returns interactive help in a string.
337 338 339 |
# File 'lib/accu-encrypt.rb', line 337 def self.interactive_help() "---------\nThis program encrypts and decrypts files by offsetting the binary representation of the current character by the binary representation of the first character in an ongoing password. The ongoing password is made up of the password appended to the beginning of the original plaintext. Thus, the pattern is theoretically unpredictable as it is impossible to exactly where the original password starts and the obviously unpredictable plaintext encryption begins. However, this algorithm's security is heavily correlated to the length and complexity of the opening password. A complex but memorable password is recommended for secure encryption.\n---------\n" end |
.interactive_terminal ⇒ Object
Opens an interactive terminal session.
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/accu-encrypt.rb', line 430 def self.interactive_terminal() print "Welcome to interactive encode/decode. " selection = "" @@mode = :normal verbose = true while selection != "q" do puts "What would you like to do? |E D I M N Q|" selection = gets.downcase selection.slice!(-1) if selection == "e" then file = "" ready = false while not ready do file = FileLibrary.select_file if verbose then puts "---------\nWARNING!!!!!\n---------\nEncoding a file could un-retrievably destroy the contents of this file. Are you sure you would like to use this file? |Y N Q|" selection2 = gets.downcase selection2.slice!(-1) while (selection2 != "y" and selection2 != "n" and selection2 != "q") do puts "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|" selection2 = gets.downcase selection2.slice!(-1) end if selection2 == "q" then ready = true file = "" elsif selection2 == "y" then ready = true end else ready = true end end if File.exists? file then password = EncryptDecrypt.get_password if verbose then puts "Finally, are you sure you would like to encrypt this file using this password? |Y N|" selection2 = gets.downcase selection2.slice!(-1) if selection2 == "y" then self.encrypt_file(file,password,@@mode) puts "Successfully encrypted file." end else self.encrypt_file(file,password,@@mode) puts "Successfully encrypted file." end end elsif selection == "d" then file = "" ready = false while not ready do file = FileLibrary.select_file if verbose then puts "---------\nWARNING!!!!!\n---------\nDecoding a file which has not been encrypted will destroy the contents.\nDecoding a file with the incorrect password will likely also destroy the file.\n---------\nAre you sure you would like to use this file? |Y N Q|" selection2 = gets.downcase selection2.slice!(-1) while (selection2 != "y" and selection2 != "n" and selection2 != "q") do puts "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|" selection2 = gets.downcase selection2.slice!(-1) end if selection2 == "q" then ready = true file = "" elsif selection2 == "y" then ready = true end else ready = true end end if File.exists? file then password = EncryptDecrypt.get_password if verbose then puts "Finally, are you sure you would like to decrypt this file using this password? |Y N|" selection2 = gets.downcase selection2.slice!(-1) if selection2 == "y" then self.decrypt_file(file,password,@@mode) puts "Successfully decrypted file." end else self.decrypt_file(file,password,@@mode) puts "Successfully decrypted file." end end elsif selection == "i" then puts EncryptDecrypt.interactive_help elsif selection == "m" then puts "Select mode: Double - D Normal - N Verbose - V Unverbose - U" selection2 = "" selection2 = gets.downcase selection2.slice!(-1) while (selection2 != "d" and selection2 != "n" and selection2 != "v" and selection2 != "u") do puts "Invalid: |" + selection2 + "|" selection2 = gets.downcase selection2.slice!(-1) end if selection2 == "d" then @@mode = :double elsif selection2 == "v" then verbose = true elsif selection2 == "u" then verbose = false else @@mode = :normal end elsif selection == "n" then puts "Port number: " selection2 = "" selection2 = gets.downcase selection2.slice!(-1) while (selection2.to_i == 0 or selection2.to_i == nil) do puts "Invalid: |" + selection2 + "|" selection2 = gets.downcase selection2.slice!(-1) end port = selection2.to_i puts "Would you like to open a client or a server? |C S|" selection2 = "" selection2 = gets.downcase selection2.slice!(-1) while (selection2 != "c" and selection2 != "s") do puts "Invalid: |" + selection2 + "|" selection2 = gets.downcase selection2.slice!(-1) end if selection2 == "c" then puts "IP: " ip = "" ip = gets.downcase ip.slice!(-1) begin server = TCPSocket.open( ip, port ) EncryptedClient.new( server ) server.close rescue puts "ERROR\nConnection probably refused." end else server = TCPServer.open port EncryptedServer.new( server.accept ) server.close end puts "Returning to normal encryption tasks." elsif selection != "q" then puts "Incorrect selection: |" + selection + "|" end end end |
.interactive_windows ⇒ Object
Opens an interactive session with the WindowTerminal library.
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 |
# File 'lib/accu-encrypt.rb', line 600 def self.interactive_windows() raise "WindowTerminal library missing" if not defined? WindowTerminal manager = WindowTerminal::WindowManager.new text = self.windows(manager) selection = "" @@mode = :normal verbose = true while selection != "q" do text.set_text "What would you like to do? |E D I M N Q|" manager.show(1) manager.render() selection = WindowTerminal.getchr().downcase if selection == "e" then file = "" ready = false while not ready do file = FileLibrary.select_file_with_windows(manager) manager.show if verbose then text.set_text "---------\nWARNING!!!!!\n---------\nEncoding a file could un-retrievably destroy the contents of this file. Are you sure you would like to use this file? |Y N Q|" manager.render() selection2 = WindowTerminal.getchr().downcase while (selection2 != "y" and selection2 != "n" and selection2 != "q") do text.set_text "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|" manager.render() selection2 = WindowTerminal.getchr().downcase end if selection2 == "q" then ready = true file = "" elsif selection2 == "y" then ready = true end else ready = true end end if File.exists? file then text.set_text "Password: " total = 0 manager.render() password = WindowTerminal.getchrs{ |char,full| if char.ord != 127 then text.set_text(text.text + "*") total += 1 elsif total > 0 then text.set_text(text.text[0..(text.text.length-1)]) total -= 1 end manager.render() } if verbose then text.set_text "Finally, are you sure you would like to encrypt this file using this password? |Y N|" manager.render() selection2 = WindowTerminal.getchr().downcase if selection2 == "y" then self.encrypt_file(file,password,@@mode) text.set_text "Successfully encrypted file." manager.render() end else self.encrypt_file(file,password,@@mode) text.set_text "Successfully encrypted file." manager.render() end WindowTerminal.getchr() end elsif selection == "d" then file = "" ready = false while not ready do file = FileLibrary.select_file_with_windows(manager) manager.show if verbose then text.set_text "---------\nWARNING!!!!!\n---------\nDecoding a file which has not been encrypted will destroy the contents.\nDecoding a file with the incorrect password will likely also destroy the file.\n---------\nAre you sure you would like to use this file? |Y N Q|" manager.render() selection2 = WindowTerminal.getchr().downcase while (selection2 != "y" and selection2 != "n" and selection2 != "q") do text.set_text "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|" manager.render() selection2 = WindowTerminal.getchr().downcase end if selection2 == "q" then ready = true file = "" elsif selection2 == "y" then ready = true end else ready = true end end if File.exists? file then text.set_text "Password: " total = 0 manager.render() password = WindowTerminal.getchrs{ |char,full| if char.ord != 127 then text.set_text(text.text + "*") total += 1 elsif total > 0 then text.set_text(text.text[0..(text.text.length-1)]) total -= 1 end manager.render() } if verbose then text.set_text "Finally, are you sure you would like to decrypt this file using this password? |Y N|" manager.render() selection2 = WindowTerminal.getchr().downcase if selection2 == "y" then self.decrypt_file(file,password,@@mode) text.set_text "Successfully decrypted file." manager.render() end else self.decrypt_file(file,password,@@mode) text.set_text "Successfully decrypted file." manager.render() end WindowTerminal.getchr() end elsif selection == "i" then manager.show(2) manager.render() WindowTerminal.getchr() manager.show(1) manager.render() elsif selection == "m" then text.set_text "Select mode: Double - D Normal - N Verbose - V Unverbose - U" manager.render() selection2 = "" selection2 = WindowTerminal.getchr().downcase while (selection2 != "d" and selection2 != "n" and selection2 != "v" and selection2 != "u") do text.set_text "Invalid: |" + selection2 + "|" manager.render() selection2 = WindowTerminal.getchr().downcase end if selection2 == "d" then @@mode = :double elsif selection2 == "v" then verbose = true elsif selection2 == "u" then verbose = false else @@mode = :normal end elsif selection == "n" then if WindowTerminal.os == :linux then print `clear` end puts "Dropping to command line for network communications.\n\n" puts "Port number: " selection2 = "" selection2 = gets.downcase selection2.slice!(-1) while (selection2.to_i == 0 or selection2.to_i == nil) do puts "Invalid: |" + selection2 + "|" selection2 = gets.downcase selection2.slice!(-1) end port = selection2.to_i puts "Would you like to open a client or a server? |C S|" selection2 = "" selection2 = gets.downcase selection2.slice!(-1) while (selection2 != "c" and selection2 != "s") do puts "Invalid: |" + selection2 + "|" selection2 = gets.downcase selection2.slice!(-1) end if selection2 == "c" then puts "IP: " ip = "" ip = gets.downcase ip.slice!(-1) begin server = TCPSocket.open( ip, port ) EncryptedClient.new( server ) server.close rescue puts "ERROR\nConnection probably refused." end else server = TCPServer.open port EncryptedServer.new( server.accept ) server.close end puts "Returning to normal encryption tasks." elsif selection != "q" then text.set_text "Incorrect selection: |" + selection + "|" manager.render() WindowTerminal.getchr() end end end |
.set_mode(arg) ⇒ Object
Set current encryption mode.
817 818 819 820 821 |
# File 'lib/accu-encrypt.rb', line 817 def self.set_mode(arg) if arg then @@mode = arg end end |
.windows(manager) ⇒ Object
Declares the default windows on a manager object.
583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
# File 'lib/accu-encrypt.rb', line 583 def self.windows(manager) #-- # Page 1 - Simple window. page1_win1 = WindowTerminal::Templates.simple()[0] page1_win1.set_name "Lobby" manager.new_page(page1_win1) # Page 2 - Help page2_win1 = WindowTerminal::Window.new(WindowTerminal::Orientation.new,"Help") page2_text1 = WindowTerminal::ColoredWrappedText.new(WindowTerminal::Orientation.new,self.interactive_help.gsub(/-/,""),6) page2_win1.add_objects(page2_text1) manager.new_page(page2_win1) return page1_win1.objects[2] #++ end |