加密两个给定字符串

huangapple go评论67阅读模式
英文:

Encrypt two given strings

问题

以下是翻译好的代码部分:

import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Main
{
    public static String StringToBinary(String str) 
    {
        StringBuilder result = new StringBuilder();
        char[] ch = str.toCharArray();
        for (char character : ch) 
        {
            result.append(String.format("%8s", Integer.toBinaryString(character)).replaceAll(" ", "0"));
        }
        return result.toString();
    }
    
    public static String XOR(String message, String password)
    {
        String tmp = "";
        for (int i = 0; i < message.length(); i++) 
        { 
            if (message.charAt(i) == password.charAt(i)) 
                tmp += "0"; 
            else
                tmp += "1"; 
        } 
        return tmp;
    }
    
    public static String convertBinaryToHexadecimal(String binaryStr) 
    {
        return new BigInteger(binaryStr, 2).toString(16);
    }
    
    public static void main(String[] args) 
    {
        int idx = 0;
        String msg = "poiuytrewq123";
        String pwd = "asdfghjkl";
        for (char m : msg.toCharArray())
        {
            idx = (idx < pwd.length()) ? idx : 0;
            char p = pwd.charAt(idx);
            idx++;
            String stringOfMessageCharArray = String.valueOf(m);
            String stringOfPasswordCharArray = String.valueOf(p);
            String messageBinaryString = StringToBinary(stringOfMessageCharArray);
            String passwordBinaryString = StringToBinary(stringOfPasswordCharArray);
            String XorString = XOR(messageBinaryString, passwordBinaryString);
            String cipherText = convertBinaryToHexadecimal(XorString);
            System.out.print(cipherText);
        }
    }
}
英文:

I'm trying to encrypt a message by the following code above.

  1. Compare 2 string length and refill the short one with cycle back.
  2. Convert that 2 string into a binary string.
  3. XOR that 2 binary string to get another binary string.
  4. convert the last binary string to hexadecimal.

Issue is on the last step. The output should be 111c0d131e1c180e1b10425655 instead of 111cd131e1c18e1b10425655.
Why my output missing two letters 0?
Can anyone help me to fix, please?

import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main
{
public static String StringToBinary(String str) 
{
StringBuilder result = new StringBuilder();
char[] ch = str.toCharArray();
for (char character : ch) 
{
result.append(String.format(&quot;%8s&quot;, Integer.toBinaryString(character)).replaceAll(&quot; &quot;, &quot;0&quot;));
}
return result.toString();
}
public static String XOR(String message, String password)
{
String tmp=&quot;&quot;;
for (int i = 0; i &lt; message.length(); i++) 
{ 
if (message.charAt(i) == password.charAt(i)) 
tmp += &quot;0&quot;; 
else
tmp += &quot;1&quot;; 
} 
return tmp;
}
public static String convertBinaryToHexadecimal(String binaryStr) 
{
return new BigInteger(binaryStr, 2).toString(16);
}
public static void main(String[] args) 
{
int idx=0;
String msg = &quot;poiuytrewq123&quot;;
String pwd = &quot;asdfghjkl&quot;;
for(char m:msg.toCharArray())
{
idx = (idx &lt; pwd.length()) ? idx : 0;
char p = pwd.charAt(idx);
idx++;
String stringOfMessageCharArray = String.valueOf(m);
String stringOfPasswordCharArray = String.valueOf(p);
String messageBinaryString = StringToBinary(stringOfMessageCharArray);
String passwordBinaryString = StringToBinary(stringOfPasswordCharArray);
String XorString = XOR(messageBinaryString,passwordBinaryString);
String cipherText = convertBinaryToHexadecimal(XorString);
System.out.print(cipherText);
}
}
}

答案1

得分: 0

问题在于你尝试将二进制转换为十六进制的数字太小,只需要2个字符,因此它只输出一个字符。

public static String convertBinaryToHexadecimal(String binaryStr)
{
    String hex = new BigInteger(binaryStr, 2).toString(16);
    return hex.length() < 2 ? "0" + hex : hex;
}

如果你将函数更改为这样,如果长度小于2,它将在十六进制前面添加一个0,并返回正确的十六进制值。

英文:

The problem is that the number you are trying to convert from binary to hex is too small to require 2 characters, so it outputs just one.

public static String convertBinaryToHexadecimal(String binaryStr)
{
    String hex = new BigInteger(binaryStr, 2).toString(16);
    return hex.length() &lt; 2 ? &quot;0&quot; + hex : hex;
}

If you change the function to this, it will put a 0 at the front of the hex if the length is less than 2, and return the right hex.

huangapple
  • 本文由 发表于 2020年9月28日 15:41:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64097889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定