当前位置:网站首页>Software College of Shandong University Project Training - Innovation Training - network security range experimental platform (XVII)

Software College of Shandong University Project Training - Innovation Training - network security range experimental platform (XVII)

2022-06-10 16:17:00 Scrambled eggs with tomatoes without eggs!

Catalog

One 、Rot13 password

1.1 brief introduction

1.2 Code implementation

Two 、RC4 password

2.1 brief introduction

2.2 Code implementation

3、 ... and 、 Show the effect

3.1 Rot13 encryption

3.2 RC4 Password encryption and decryption


Preface : This blog is mainly recorded in the security tools Rot13 encryption 、RC4 Implementation of encryption and decryption .

One 、Rot13 password

1.1 brief introduction

ROT13( Turn around 13 position ) Is a simple replacement cipher algorithm . It's a way to hide gossip in English online forums 、 Witticism 、 Puzzles, answers and tools for swearing , The purpose is to escape a glimpse of the moderator or .ROT13 It is also a variant of the Caesar code developed in ancient Rome .ROT13 It is the reverse of itself , namely : To restore to the original text, just use the same algorithm , Therefore, the same operation can be used for encryption and decryption . This algorithm does not provide real security in cryptography , Therefore, it should not be used for purposes requiring preservation . It is often used as a typical example of weak encryption .

application ROT13 To a piece of text just check the alphabetical order and replace it in 13 The corresponding letter after bit , If there is a need to exceed it, go back to 26 Just start with an English letter .A Switch to N、B Switch to O、 And so on to M Switch to Z, Then the serial inversion :N Switch to A、O Switch to B、 Last Z Switch to M( As shown in the figure ). Only those characters that appear in English letters are affected ; Numbers 、 Symbol 、 White space characters and all other characters remain the same . The case of the replaced letters remains unchanged .

1.2 Code implementation

<template>
  <br>
  <br>
  <el-card shadow="always" style="text-align: center; margin-right: 100px;margin-left: 100px;height: 700px">
    <!--     title -->
    <el-row>
      <el-col :span="8">
      </el-col>
      <el-col :span="8">
        <div style="font-size: x-large;background-color: darkgreen;color:white;margin: 5px;">
          Rot13 Online encryption and decryption 
        </div>
        <el-button style="text-align: right" size="small" icon="el-icon-thumb">
          <el-link href="https://blog.csdn.net/qq_17046291/article/details/80306580?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165459051316782184695260%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165459051316782184695260&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-80306580-null-null.142^v11^pc_search_result_control_group,157^v13^new_style1&utm_term=Rot13&spm=1018.2226.3001.4187"
                   target="_blank"
                   style="font-size: 20px;color: darkgreen"

          > What is? Rot13?
          </el-link>
        </el-button>
      </el-col>
      <el-col :span="8">
      </el-col>
    </el-row>

    <!--     Input box -->
    <div class="el-common-layout">
      <div class="text-area" style="margin: 10px">
        <!--    <textarea v-model="textdata" placeholder=" Please enter the encoded string or the string to be decoded ">-->
        <!--    </textarea>-->
        <el-input
            v-model="textdata"
            type="textarea"
            placeholder=" Please enter the content to be encrypted and decrypted "
            style="width: 600px;font-size: 20px;margin: 10px;"
            :rows=5
        />
      </div>
    </div>

    <!--     Encryption / decryption button -->
    <el-button @click="encode(textdata)" type="info" style="margin: 20px;margin-bottom: 0px;">EnCrypto</el-button>
    <el-button @click="decode(textdata)" type="info">DeCrypto</el-button>

    <!--     Result area -->
    <el-card style="width: 40%;height: 20%;margin-left: 30%;margin-top: 3%">
      <h2 style="text-align: left"> encryption / Decryption result :</h2>
      <el-input
          v-model="myresult"
          type="textarea"
          placeholder=""
          style="font-size: 20px"
          disabled
          :rows=5
      />
      <!--      <p>{
   { myresult }}</p>-->
    </el-card>

  </el-card>

</template>

 

    // Rot13 encryption 
    @GetMapping("/encrypto")
    public Result encrypto(@RequestParam() String Text) throws UnsupportedEncodingException {

        System.out.println(" Plaintext :"+Text);

        Rot13 rot13=new Rot13();
        String encryStr = rot13.cipher(Text);
        System.out.println(" Encrypted string :"+encryStr);
        return Result.success(encryStr);
    }

    // Rot13 Decrypt 
    @GetMapping("/decrypto")
    public Result decrypto(@RequestParam() String Text) throws UnsupportedEncodingException {

        System.out.println(" Ciphertext :"+Text);

        Rot13 rot13=new Rot13();
        String decryStr = rot13.cipher(Text);
        System.out.println(" Decrypted string :"+decryStr);
        return Result.success(decryStr);
    }

    static {
        final char[] lookup1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();

        final char[] lookup2 = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm".toCharArray();

        Map m = new HashMap();

        for (int i = 0; i < lookup1.length; i++) {

            m.put(lookup1[i], lookup2[i]);
        }
        map = Collections.unmodifiableMap(m);

    }

    /**
     * ROT-13  Algorithm .
     *
     * @param inStr
     * @return
     */

    public static String cipher(final String inStr) {

        char[] arr = inStr.toCharArray();
        StringBuilder sb = new StringBuilder(arr.length);

        for (char c : arr) {
            Character out = (Character) map.get(c);
            if (out == null) {
                sb.append(c);

            } else {

                sb.append(out);

            }

        }
        return sb.toString();

    }

Two 、RC4 password

2.1 brief introduction

RC4( come from Rivest Cipher 4 Abbreviation ) It's a stream encryption algorithm , Key length is variable . It uses the same key for encryption and decryption , Therefore, it also belongs to symmetric encryption algorithm .RC4 Is Wired Equivalent encryption (WEP) The encryption algorithm used in , It used to be TLS One of the available algorithms .

RC4 Algorithm characteristics :

(1)、 The algorithm is simple and easy to implement by software , Fast encryption , High security ;

(2)、 Key length is variable , It's usually used 256 Bytes .

Introducing RC4 Before the principle of algorithm , Let's first look at some key variables in the algorithm :

● Key stream :RC4 The key of the algorithm is to generate the corresponding key stream according to plaintext and key , The length of the key stream corresponds to the length of the plaintext , That is to say, the length of expository text is 500 byte , So the key stream is also 500 byte . Of course , The ciphertext generated by encryption is also 500 byte , Because ciphertext No i byte = In the first place i byte ^ Key stream number i byte .
● State vector S: The length is 256,S[0],S[1]…S[255]. Each unit is a byte , Any time the algorithm runs ,S It all includes 0-255 Of 8 Permutation and combination of bits , It's just that the position of the value changes ;
● Temporary vector T: The length is also 256, Each unit is also a byte . If the length of the key is 256 byte , Just assign the value of the key to T, otherwise , Assign each byte of the key to in turn T;
● secret key K: The length is 1-256 byte , Note the length of the key keylen And plaintext length 、 The length of the key stream does not necessarily matter , Usually the length of the key is interesting 16 byte (128 The bit )

2.2 Code implementation

<!--RC4 encryption -->
<template>
  <br>
  <br>
  <el-card shadow="always" style="text-align: center; margin-right: 100px;margin-left: 100px;height: 700px">
    <!--     title -->
    <el-row>
      <el-col :span="8">
      </el-col>
      <el-col :span="8">
        <div style="font-size: x-large;background-color: darkgreen;color:white;margin: 5px;">
          RC4 Online encoding and decoding 
        </div>
        <el-button style="text-align: right" size="small" icon="el-icon-thumb">
          <el-link href="https://blog.csdn.net/wangningzk123/article/details/108294288?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165451738016782395393911%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165451738016782395393911&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-2-108294288-null-null.142^v11^pc_search_result_control_group,157^v13^control&utm_term=RC4&spm=1018.2226.3001.4187"
                   target="_blank"
                   style="font-size: 20px;color: darkgreen"

          > What is? RC4?
          </el-link>
        </el-button>
      </el-col>
      <el-col :span="8">
      </el-col>
    </el-row>

    <!--     Input box -->
    <div class="el-common-layout">
      <div class="text-area" style="margin: 10px">
        <!--    <textarea v-model="textdata" placeholder=" Please enter the encoded string or the string to be decoded ">-->
        <!--    </textarea>-->
        <el-input
            v-model="textdata"
            type="textarea"
            placeholder=" Please enter the content to be encrypted and decrypted "
            style="width: 600px;font-size: 20px;margin: 10px;"
            :rows=5
        />
        <el-input
            v-model="key"
            type="textarea"
            placeholder=" Please enter the key "
            style="width: 600px;font-size: 20px;margin: 10px;"
            :rows=5
        />
      </div>
    </div>

    <!--     Encryption / decryption button -->
    <el-button @click="encode(textdata,key)" type="info" style="margin: 20px;margin-bottom: 0px;">EnCrypto</el-button>
    <el-button @click="decode(textdata,key)" type="info">DeCrypto</el-button>

    <!--     Result area -->
    <el-card style="width: 40%;height: 20%;margin-left: 30%;margin-top: 3%">
      <h2 style="text-align: left"> encryption / Decryption result :</h2>
      <el-input
          v-model="myresult"
          type="textarea"
          placeholder=""
          style="font-size: 20px"
          disabled
          :rows=5
      />
      <!--      <p>{
   { myresult }}</p>-->
    </el-card>

  </el-card>

</template>

 

    // RC4 encryption 
    @GetMapping("/encrypto")
    public Result encrypto(@RequestParam() String clearText, @RequestParam() String Key) throws UnsupportedEncodingException {

        System.out.println(" Plaintext :"+clearText);
        RC4 rc4=new RC4();
        String encryStr = rc4.encryRC4String(clearText, Key,"UTF-8");
        System.out.println(" Encrypted string :"+encryStr);
        return Result.success(encryStr);
    }

    // RC4 Decrypt 
    @GetMapping("/decrypto")
    public Result decrypto(@RequestParam() String cipherText,@RequestParam() String Key) throws UnsupportedEncodingException {

        System.out.println(" Ciphertext :"+cipherText);
        RC4 rc4=new RC4();
        String decryStr = rc4.decryRC4(cipherText, Key,"UTF-8");
        System.out.println(" Decrypted string :"+decryStr);
        return Result.success(decryStr);
    }


    /**
     * RC4 encryption , Hash the encrypted data 
     * @param data  Data that needs to be encrypted 
     * @param key  Encryption key 
     * @param chartSet  Encoding mode 
     * @return  Return encrypted data 
     * @throws UnsupportedEncodingException
     */
    public static String encryRC4String(String data, String key, String chartSet) throws UnsupportedEncodingException {
        if (data == null || key == null) {
            return null;
        }
        return bytesToHex(encryRC4Byte(data, key, chartSet));
    }

    /**
     * RC4 encryption , The encrypted byte data 
     * @param data  Data that needs to be encrypted 
     * @param key  Encryption key 
     * @param chartSet  Encoding mode 
     * @return  Return encrypted data 
     * @throws UnsupportedEncodingException
     */
    public static byte[] encryRC4Byte(String data, String key, String chartSet) throws UnsupportedEncodingException {
        if (data == null || key == null) {
            return null;
        }
        if (chartSet == null || chartSet.isEmpty()) {
            byte bData[] = data.getBytes();
            return RC4Base(bData, key);
        } else {
            byte bData[] = data.getBytes(chartSet);
            return RC4Base(bData, key);
        }
    }
    /**
     * RC4 Decrypt 
     * @param data  Data that needs to be decrypted 
     * @param key  Encryption key 
     * @param chartSet  Encoding mode 
     * @return  Return decrypted data 
     * @throws UnsupportedEncodingException
     */
    public static String decryRC4(String data, String key,String chartSet) throws UnsupportedEncodingException {
        if (data == null || key == null) {
            return null;
        }
        return new String(RC4Base(hexToByte(data), key),chartSet);
    }

    /**
     * RC4 Encryption initialization key 
     * @param aKey
     * @return
     */
    private static byte[] initKey(String aKey) {
        byte[] bkey = aKey.getBytes();
        byte state[] = new byte[256];

        for (int i = 0; i < 256; i++) {
            state[i] = (byte) i;
        }
        int index1 = 0;
        int index2 = 0;
        if (bkey.length == 0) {
            return null;
        }
        for (int i = 0; i < 256; i++) {
            index2 = ((bkey[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
            byte tmp = state[i];
            state[i] = state[index2];
            state[index2] = tmp;
            index1 = (index1 + 1) % bkey.length;
        }
        return state;
    }

 

3、 ... and 、 Show the effect

3.1 Rot13 encryption

3.2 RC4 Password encryption and decryption

 

 

原网站

版权声明
本文为[Scrambled eggs with tomatoes without eggs!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101009229526.html