From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Ian Dunn Newsgroups: gmane.emacs.devel Subject: Re: [ELPA] New package: vigenere Date: Mon, 28 Aug 2017 20:02:50 -0400 Message-ID: <87o9qz2qc5.fsf@escafil> References: <87o9r1dnir.fsf@escafil> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1503965056 14291 195.159.176.226 (29 Aug 2017 00:04:16 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 29 Aug 2017 00:04:16 +0000 (UTC) User-Agent: mu4e 0.9.19; emacs 26.0.50 Cc: emacs-devel@gnu.org To: rms@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Aug 29 02:04:12 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dmU0i-00034N-Nr for ged-emacs-devel@m.gmane.org; Tue, 29 Aug 2017 02:04:04 +0200 Original-Received: from localhost ([::1]:41854 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dmU0p-0007OL-IM for ged-emacs-devel@m.gmane.org; Mon, 28 Aug 2017 20:04:11 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:45831) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dmU0E-0007My-33 for emacs-devel@gnu.org; Mon, 28 Aug 2017 20:03:34 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dmU0D-0000IZ-FC for emacs-devel@gnu.org; Mon, 28 Aug 2017 20:03:34 -0400 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:54857) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dmU0D-0000IU-B0 for emacs-devel@gnu.org; Mon, 28 Aug 2017 20:03:33 -0400 Original-Received: from [2604:6000:1010:176:da4d:3352:bae5:f50e] (port=43186 helo=escafil) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1dmU06-0001oW-11; Mon, 28 Aug 2017 20:03:26 -0400 In-reply-to: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:4830:134:3::e X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:217870 Archived-At: --=-=-= Content-Type: text/plain RMS> Please add text to the doc string saying, RMS> This is for historical interest, not a serious system for encryption. RMS> You might think everyone knows that, but most people are not curious RMS> about ciphers. Done. I often find packages through their functions, so I added a note about it to the function doc strings and the package commentary, for good measure. --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=vigenere.el Content-Transfer-Encoding: quoted-printable ;;; vigenere.el --- Run a vigenere cipher on a block of text ; -*- lexical-= binding: t; -*- ;; Author: Ian Dunn ;; Version: 1.0 ;;; Commentary: ;; A vigenere cipher is similar to a series of shift ciphers. A key is rep= eated ;; through the plain text, then a shift cipher is used for each letter depe= nding ;; on the letter in the key at that position. ;; Example: ;; The following uses the key EMACS (because what else would my example key= be?). ;; Plain text: This is some plain text ;; First, we repeat the key through the plain text ;; This is some plain text ;; EMAC SE MACS EMACS EMAC ;; Then each letter in the plain text is shifted based on the key character= at ;; that point: ;; This is some plain text ;; EMAC SE MACS EMACS EMAC ;; Xtiu aw eoow txakf xqxv ;; The specific shift amount for each character is the 0-based offset from = A. ;; Therefore, A is 0, B is 1, etc. ;; The key in our case is case-insensitive. EMACS is Emacs is emacs is EmA= Cs. ;; One more note, is that this package only operates on letters, and the ca= se of ;; the original text is preserved. ;; A vigenere cipher should not be considered cryptographically secure. Th= is ;; package is for recreational use only, not for securing sensitive informa= tion. ;;; Code: (defun vigenere-process-region (key begin end enc-or-dec) "Process the region from BEGIN to END in the current buffer using KEY. ENC-OR-DEC is either 'encrypt or 'decrypt, which determines the mode of operation. KEY is case-insensitive, and case is preserved in the original text." (save-excursion (goto-char begin) (let ((key-pos 0) (key-len (length key)) (op (if (eq enc-or-dec 'encrypt) '+ '-))) ;; Only operate on letters. (while (re-search-forward "[[:alpha:]]" end t) (let* ((c (- (string-to-char (downcase (match-string-no-properties = 0))) ?a)) (k (- (string-to-char (downcase (substring key key-pos))) ?a= )) (d (char-to-string (+ (mod (funcall op c k) 26) ?a))) (case-fold-search nil)) (replace-match d)) ;; Update position in key (setq key-pos (mod (1+ key-pos) key-len)))))) (defun vigenere-decrypt-region (key begin end) "Decrypt region from BEGIN to END with a Vigenere cipher using KEY. Note: A Vigenere cipher should not be considered cryptographically secure." (interactive "sKey:\nr") (vigenere-process-region key begin end 'decrypt)) (defun vigenere-encrypt-region (key begin end) "Encrypt region from BEGIN to END with a Vigenere cipher using KEY. Note: A Vigenere cipher should not be considered cryptographically secure." (interactive "sKey:\nr") (vigenere-process-region key begin end 'encrypt)) (defun vigenere-decrypt-buffer (key) "Decrypt the current buffer with a Vigenere cipher using KEY. Note: A Vigenere cipher should not be considered cryptographically secure." (interactive "sKey: ") (vigenere-decrypt-region key (point-min) (point-max))) (defun vigenere-encrypt-buffer (key) "Encrypt the current buffer with a Vigenere cipher using KEY. Note: A Vigenere cipher should not be considered cryptographically secure." (interactive "sKey: ") (vigenere-encrypt-region key (point-min) (point-max))) (defun vigenere-decrypt-string (key string) "Decrypt STRING with a Vigenere cipher using KEY. Note: A Vigenere cipher should not be considered cryptographically secure." (with-temp-buffer (insert string) (vigenere-decrypt-buffer key) (buffer-string))) (defun vigenere-encrypt-string (key string) "Encrypt STRING with a Vigenere cipher using KEY. Note: A Vigenere cipher should not be considered cryptographically secure." (with-temp-buffer (insert string) (vigenere-encrypt-buffer key) (buffer-string))) (provide 'vigenere) ;;; vigenere.el ends here --=-=-= Content-Type: text/plain -- Ian Dunn --=-=-=--