खोज…


परिचय

यह एक साधारण पारी है मोनोफैबेटिक क्लासिकल सिफर जहां प्रत्येक अक्षर को लेटर 3 पोजीशन (वास्तविक सीज़र सिफर) से बदल दिया जाता है, जो Z के बाद सर्कुलर अल्फाबेटिक ऑर्डरिंग लेटर का उपयोग करके आगे होता है। इसलिए जब हम हेलो वर्ल्ड को एनकोड करते हैं, तो सिफर टेक्स्ट KHOORZRUOG हो जाता है।

परिचय

सीज़र सिफर एक क्लासिक एन्क्रिप्शन विधि है। यह एक निश्चित राशि से वर्णों को स्थानांतरित करके काम करता है। उदाहरण के लिए, यदि हम 3 की पारी चुनते हैं, तो A, D बन जाएगा और E, H बन जाएगा।

निम्नलिखित पाठ को 23 पारी का उपयोग करके एन्क्रिप्ट किया गया है।

THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD

पायथन कार्यान्वयन

ASCII रास्ता

यह वर्णों को बदल देता है लेकिन ध्यान नहीं देता कि नया वर्ण अक्षर नहीं है। यदि आप विराम चिह्न या विशेष वर्णों का उपयोग करना चाहते हैं तो यह अच्छा है, लेकिन यह आपको केवल एक आउटपुट के रूप में पत्र नहीं देगा। उदाहरण के लिए, "z" 3-शिफ्ट्स से "}"।

def ceasar(text, shift):
    output = ""
    for c in text:
        output += chr(ord(c) + shift)
    return output

ROT13

ROT13 सीज़र सिफर का एक विशेष मामला है, जिसमें 13 शिफ्ट है। केवल अक्षर बदले जाते हैं, और श्वेत-स्थान और विशेष वर्णों को छोड़ दिया जाता है जैसे वे हैं।

क्या दिलचस्प है कि ROT13 एक पारस्परिक सिफर है: ROT13 को दो बार लागू करने से आपको प्रारंभिक इनपुट मिलेगा। दरअसल, 2 * 13 = 26, वर्णमाला में अक्षरों की संख्या।


चूंकि ROT13 के पास इनपुट पैरामीटर के रूप में एक कुंजी नहीं है, इसलिए इसे अक्सर एन्कोडिंग एल्गोरिथ्म के रूप में अधिक देखा जाता है या, विशेष रूप से, सिफर के बजाय एक आक्षेप एल्गोरिथम

ROT13 सिर्फ संदेशों को सीधे पढ़ना मुश्किल बनाता है और इसलिए इसे अक्सर आक्रामक संदेशों या चुटकुलों के लिए इस्तेमाल किया जाता है। यह कोई कम्प्यूटेशनल सुरक्षा प्रदान नहीं करता है।

सीज़र सिफर के लिए एक जावा कार्यान्वयन

सीज़र सिफर का कार्यान्वयन।

  • यह कार्यान्वयन केवल ऊपरी और निचले मामले के अक्षर पर शिफ्ट ऑपरेशन करता है और अन्य पात्रों (जैसे कि स्पेस-जैसे) को बरकरार रखता है।
  • सीज़र सिफर वर्तमान मानकों के अनुसार सुरक्षित नहीं है।
  • नीचे उदाहरण केवल उदाहरण के लिए है!
  • संदर्भ: [ https://en.wikipedia.org/wiki/Caesar_cipherades(https://en.wikipedia.org/wiki/Caesar_cipher)

package com.example.so.cipher;

/**
 * Implementation of the Caesar cipher.
 * <p>
 * <ul>
 * <li>This implementation performs the shift operation only on upper and lower
 * case alphabets and retains the other characters (such as space as-is).</li>
 * <li>The Caesar cipher is not secure as per current standards.</li>
 * <li>Below example is for illustrative purposes only !</li>
 * <li>Reference: https://en.wikipedia.org/wiki/Caesar_cipher</li>
 * </ul>
 * </p>
 * 
 * @author Ravindra HV
 * @author Maarten Bodewes (beautification only)
 * @since 2016-11-21
 * @version 0.3
 *
 */
public class CaesarCipher {

    public static final char START_LOWER_CASE_ALPHABET = 'a'; // ASCII-97
    public static final char END_LOWER_CASE_ALPHABET = 'z';   // ASCII-122

    public static final char START_UPPER_CASE_ALPHABET = 'A'; // ASCII-65
    public static final char END_UPPER_CASE_ALPHABET = 'Z';   // ASCII-90
    
    public static final int ALPHABET_SIZE = 'Z' - 'A' + 1;    // 26 of course

    /**
     * Performs a single encrypt followed by a single decrypt of the Caesar
     * cipher, prints out the intermediate values and finally validates
     * that the decrypted plaintext is identical to the original plaintext.
     * 
     * <p>
     * This method outputs the following:
     * 
     * <pre>
     * Plaintext  : The quick brown fox jumps over the lazy dog
     * Ciphertext : Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald
     * Decrypted  : The quick brown fox jumps over the lazy dog
     * Successful decryption: true
     * </pre>
     * </p>
     * 
     * @param args (ignored)
     */
    public static void main(String[] args) {

        int shift = 23;
        String plainText = "The quick brown fox jumps over the lazy dog";
        
        System.out.println("Plaintext  : " + plainText);
 
        String ciphertext = caesarCipherEncrypt(plainText, shift);
        System.out.println("Ciphertext : " + ciphertext);
        
        String decrypted = caesarCipherDecrypt(ciphertext, shift);
        System.out.println("Decrypted  : " + decrypted);
        System.out.println("Successful decryption: "
                + decrypted.equals(plainText));
    }

    public static String caesarCipherEncrypt(String plaintext, int shift) {
        return caesarCipher(plaintext, shift, true);
    }

    public static String caesarCipherDecrypt(String ciphertext, int shift) {
        return caesarCipher(ciphertext, shift, false);
    }

    private static String caesarCipher(
            String input, int shift, boolean encrypt) {

        // create an output buffer of the same size as the input
        StringBuilder output = new StringBuilder(input.length());

        for (int i = 0; i < input.length(); i++) {
            
            // get the next character
            char inputChar = input.charAt(i);
            
            // calculate the shift depending on whether to encrypt or decrypt
            int calculatedShift = (encrypt) ? shift : (ALPHABET_SIZE - shift);

            char startOfAlphabet;
            if ((inputChar >= START_LOWER_CASE_ALPHABET)
                    && (inputChar <= END_LOWER_CASE_ALPHABET)) {
                
                // process lower case
                startOfAlphabet = START_LOWER_CASE_ALPHABET;
            } else if ((inputChar >= START_UPPER_CASE_ALPHABET)
                    && (inputChar <= END_UPPER_CASE_ALPHABET)) {
                
                // process upper case
                startOfAlphabet = START_UPPER_CASE_ALPHABET;
            } else {
                
                // retain all other characters
                output.append(inputChar);
                
                // and continue with the next character
                continue;
            }                
                
            // index the input character in the alphabet with 0 as base
            int inputCharIndex =
                    inputChar - startOfAlphabet;

            // cipher / decipher operation (rotation uses remainder operation)
            int outputCharIndex =
                    (inputCharIndex + calculatedShift) % ALPHABET_SIZE;

            // convert the new index in the alphabet to an output character
            char outputChar =
                    (char) (outputCharIndex + startOfAlphabet);

            // add character to temporary-storage
            output.append(outputChar);
        }

        return output.toString();
    }
}

कार्यक्रम का आउटपुट:

Plaintext  : The quick brown fox jumps over the lazy dog
Ciphertext : Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald
Decrypted  : The quick brown fox jumps over the lazy dog
Successful decryption: true

पायथन कार्यान्वयन

निम्न कोड उदाहरण सीज़र सिफर को लागू करता है और सिफर के गुणों को दर्शाता है।

यह अपरकेस और लोअर अल्फा-न्यूमेरिकल दोनों तरह के कैरेक्टर को हैंडल करता है, बाकी सभी कैरेक्टर्स को वैसा ही छोड़ता है जैसा वे थे।

सीज़र सिफर के निम्नलिखित गुण दिखाए गए हैं:

  • कमजोर चाबियाँ;
  • कम महत्वपूर्ण स्थान;
  • तथ्य यह है कि प्रत्येक कुंजी में एक पारस्परिक (उलटा) कुंजी होती है;
  • ROT13 के साथ संबंध;

यह निम्नलिखित भी दिखाता है - अधिक सामान्य - क्रिप्टोग्राफ़िक धारणाएँ:

  • कमजोर चाबियाँ;
  • ऑबफसिकेशन (एक कुंजी के बिना) और एन्क्रिप्शन के बीच का अंतर;
  • एक कुंजी मजबूर जानवर;
  • सिफरटेक्स्ट की अनुपलब्धता।

def caesarEncrypt(plaintext, shift):
    return caesarCipher(True, plaintext, shift)

def caesarDecrypt(ciphertext, shift):
    return caesarCipher(False, ciphertext, shift)

def caesarCipher(encrypt, text, shift):
    if not shift in range(0, 25):
        raise Exception('Key value out of range')

    output = ""
    for c in text:
        # only encrypt alphanumerical characters
        if c.isalpha():
            # we want to shift both upper- and lowercase characters
            ci = ord('A') if c.isupper() else ord('a')
            
            # if not encrypting, we're decrypting
            if encrypt:
                output += caesarEncryptCharacter(c, ci, shift)
            else:
                output += caesarDecryptCharacter(c, ci, shift)
        else:
            # leave other characters such as digits and spaces
            output += c
    return output

def caesarEncryptCharacter(plaintextCharacter, positionOfAlphabet, shift):
    # convert character to the (zero-based) index in the alphabet
    n = ord(plaintextCharacter) - positionOfAlphabet
    # perform the >positive< modular shift operation on the index
    # this always returns a value within the range [0, 25]
    # (note that 26 is the size of the western alphabet)
    x = (n + shift) % 26 # <- the magic happens here
    # convert the index back into a character
    ctc = chr(x + positionOfAlphabet)
    # return the result
    return ctc

def caesarDecryptCharacter(plaintextCharacter, positionOfAlphabet, shift):
    # convert character to the (zero-based) index in the alphabet 
    n = ord(plaintextCharacter) - positionOfAlphabet
    # perform the >negative< modular shift operation on the index
    x = (n - shift) % 26
    # convert the index back into a character
    ctc = chr(x + positionOfAlphabet)
    # return the result
    return ctc

def encryptDecrypt(): 
    print '--- Run normal encryption / decryption'
    plaintext = 'Hello world!'
    key = 3 # the original value for the Caesar cipher
    ciphertext = caesarEncrypt(plaintext, key)
    print ciphertext
    decryptedPlaintext = caesarDecrypt(ciphertext, key)
    print decryptedPlaintext
encryptDecrypt()

print '=== Now lets show some cryptographic properties of the Caesar cipher'

def withWeakKey():
    print '--- Encrypting plaintext with a weak key is not a good idea'
    plaintext = 'Hello world!'
    # This is the weakest key of all, it does nothing
    weakKey = 0
    ciphertext = caesarEncrypt(plaintext, weakKey)
    print ciphertext # just prints out the plaintext
withWeakKey();
    
def withoutDecrypt():
    print '--- Do we actually need caesarDecrypt at all?'
    plaintext = 'Hello world!'
    key = 3 # the original value for the Caesar cipher
    ciphertext = caesarEncrypt(plaintext, key)
    print ciphertext
    decryptionKey = 26 - key; # reciprocal value
    decryptedPlaintext = caesarEncrypt(ciphertext, decryptionKey)
    print decryptedPlaintext # performed decryption
withoutDecrypt()
    

def punnify():
    print '--- ROT 13 is the Caesar cipher with a given, reciprocal, weak key: 13'
    # The key is weak because double encryption will return the plaintext
    def rot13(pun):
        return caesarEncrypt(pun, 13)

    print 'Q: How many marketing people does it take to change a light bulb?' 
    obfuscated = 'N: V jvyy unir gb trg onpx gb lbh ba gung.'
    print obfuscated
    deobfuscated = rot13(obfuscated)
    print deobfuscated
    # We should not leak the pun, right? Lets obfuscate afterwards!
    obfuscatedAgain = rot13(deobfuscated)
    print obfuscatedAgain
punnify()

def bruteForceAndLength():
    print '--- Brute forcing is very easy as there are only 25 keys in the range [1..25]'
    # Note that AES-128 has 340,282,366,920,938,463,463,374,607,431,768,211,456 keys
    # and is therefore impossible to bruteforce (if the key is correctly generated)
    key = 10;
    plaintextToFind = 'Hello Maarten!'
    ciphertextToBruteForce = caesarEncrypt(plaintextToFind, key)
    for candidateKey in range(1, 25):
        bruteForcedPlaintext = caesarDecrypt(ciphertextToBruteForce, candidateKey)
        # lets assume the adversary knows 'Hello', but not the name
        if bruteForcedPlaintext.startswith('Hello'):
            print 'key value: ' + str(candidateKey) + ' gives : ' + bruteForcedPlaintext

    print '--- Length of plaintext usually not hidden'
    # Side channel attacks on ciphertext lengths are commonplace! Beware!
    if len(ciphertextToBruteForce) != len('Hello Stefan!'):
        print 'The name is not Stefan (but could be Stephan)'
bruteForceAndLength()

def manInTheMiddle():
    print '--- Ciphers are vulnerable to man-in-the-middle attacks'
    # Hint: do not directly use a cipher for transport security
    moneyTransfer = 'Give Maarten one euro'
    key = 1
    print moneyTransfer
    encryptedMoneyTransfer = caesarEncrypt(moneyTransfer, key)
    print encryptedMoneyTransfer
    # Man in the middle replaces third word with educated guess
    # (or tries different ciphertexts until success)
    encryptedMoneyTransferWords  = encryptedMoneyTransfer.split(' ');
    encryptedMoneyTransferWords[2] = 'ufo' # unidentified financial object
    modifiedEncryptedMoneyTransfer = ' '.join(encryptedMoneyTransferWords)
    print modifiedEncryptedMoneyTransfer
    decryptedMoneyTransfer = caesarDecrypt(modifiedEncryptedMoneyTransfer, key)
    print decryptedMoneyTransfer
manInTheMiddle()


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow