如何从字符串中删除重复的字母?凯撒密码

时间:2016-04-25 14:27:55

标签: java string duplicates caesar-cipher

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

/**
   This class encrypts files using the Caesar cipher.
   For decryption, use an encryptor whose key is the 
   negative of the encryption key.
*/
public class CaesarCipher
{
   private String keyWordString;
   private String removeDuplicates(String word){

    for (int i = 0; i < word.length(); i++) {
        if(!keyWordString.contains(String.valueOf(word.charAt(i)))) {
            keyWordString += String.valueOf(word.charAt(i));
        }
    }
    return(keyWordString);
}

   /**
      Constructs a cipher object with a given key word.
      @param aKey the encryption key
   */
   public CaesarCipher(String aKeyWord)
   {  
      keyWordString = removeDuplicates(keyWordString);
      keyWordString = aKeyWord;
      //create the mapping string
      //keyWordString = removeDuplicates(keyWordString);
      int moreChar = 26 - keyWordString.length();
      char ch = 'Z';
      for(int j=0; j<moreChar; j++)
      {
         keyWordString += ch;
         ch -= 1;
      }
      System.out.println("The mapping string is: " + keyWordString);
   }

   /**
      Encrypts the contents of a stream.
      @param in the input stream
      @param out the output stream
   */      
   public void encryptStream(InputStream in, OutputStream out)
         throws IOException
   {
      boolean done = false;
      while (!done)
      {
         int next = in.read();
         if (next == -1) 
         { 
            done = true; 
         }
         else
         {
            //int encrypted = encrypt(next);
            int encrypted = encryptWordKey(next);
            System.out.println((char)next + " is encrypted to " + (char)encrypted);
            out.write(encrypted);
         }
      }
   }

   /**
      Encrypts a value.
      @param b the value to encrypt (between 0 and 255)
      @return the encrypted value
   */
   public int encryptWordKey(int b)
   {
      int pos = b % 65;
      return keyWordString.charAt(pos);
   }
}

当我要运行此代码时,它会给我一个运行时错误,上面写着:

Exception in thread "main" java.lang.NullPointerException
    at CaesarCipher.removeDuplicates(CaesarCipher.java:15)
    at CaesarCipher.<init>(CaesarCipher.java:29)
    at CaesarEncryptor.main(CaesarEncryptor.java:29)

假设我输入JJJJJJJavvvvvaaaaaaa,我想让它产生Java并给我加密代码。为了区别于已经问过的其他问题,我需要执行removeDuplicates方法并让输出打印JavaZYWW .....等。任何帮助或建议?非常感谢我的帮助

1 个答案:

答案 0 :(得分:0)

您忘了启动变量<img src="your-image-path" class="img-responsive btn-block>

例如在类的静态字段(或构造函数)中执行此操作。

function decrypt_google_winning_price($value, $ekey, $ikey, &$reason = '') {
if (strlen($value) != 38)
{
    $reason = "Wrong encrypted value length";
    return false;
}

$ekey = base64_decode($ekey);
$ikey = base64_decode($ikey);
$value = strtr($value, '-_,', '+/=') . "==";
$enc_value = base64_decode($value); //Gets a 28 byte encrypted string.
if (strlen($enc_value) != 28)
{
    $reason = "Wrong encrypted value length after base64_decode()";
    return false;
}

$iv = substr($enc_value, 0, 16);// initialization vector (16 bytes - unique to the impression)
$p = substr($enc_value, 16, 8); // encryption key (32 bytes - provided at account set up)
$sig = substr($enc_value, 24, 4);// integrity signature (4 bytes)
$price_pad = hash_hmac("sha1", $iv, $ekey, true);
$price = $p ^ $price_pad;// XOR

$conf_sig = substr(hex2bin(hash_hmac("sha1", $price . $iv, $ikey)), 0, 4);

if ($sig !== $conf_sig)
{
    $reason = "Signature is not valid";
    return false;
}

return  hexdec(bin2hex($price)); //This is 8 byte binary. Dumps some binary on screen. Result should be a 8 byte number
}


$value = "[VALUE]"; //38 character base64
$ekey = "[ENCRYPTIONKEY]"; //64 byte hex encoded key . 32 byte key
$ikey "[INTEGRITYKEY]"; //64 byte hex encoded key . 32 byte key

var_dump(decrypt_google_winning_price($value, $ekey, $ikey));