XOR加密包含空格的位字符串时出现问题

时间:2019-01-29 18:59:42

标签: java encryption bit-manipulation bit

我这里的密码是二进制字符串:

01110101 01101000 01010100 01000100 01000011 01000100 00110111 00111000 00110110 00110010 00110100 01100111 00100110 00110000 00111001 00111000 00111101 ...

还有一条消息作为二进制字符串:

01001000 01100001 01101100 01101100 01101111 01101100 01100101 01101100 01100101 01100001 01110011 01100100 01100110

我尝试了多种方法对这两个字符串进行XOR,这是我的最新尝试:

public static String encrypt(String str) {
        String pwBinary = getBinaryString(pw);
        String msgBinary = getBinaryString(str);
        StringBuilder temp = new StringBuilder();


        int count = 0;


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


            if(!(i%8==count)) {

                if(msgBinary.charAt(i) == pwBinary.charAt(i)) {

                    temp.append(0);
                }else {

                    temp.append(1);
                }


            }else if(i!=0) {
                temp.append(" ");
                count++;
                i++;
            }

        }
        return temp.toString();
    }

输出总是在中间的某个地方开始混乱...

0111101 0001001 0111000 0101000 0101100 0101000 1010010 1010100 10100110010100110010001110000000110010000000

我不知道如何正确处理空白处:( 在此先感谢:-)

2 个答案:

答案 0 :(得分:0)

我找到了一个简单的解决方案,尽管花了我一段时间才发现我的推理错误:

public static String encrypt(String str) {
        String pwBinary = getBinaryString(pw);
        String msgBinary = getBinaryString(str);
        StringBuilder temp = new StringBuilder();

        int x = 8;

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


            if(i == x) {
                temp.append(" ");
                x+=9;

            }else if(msgBinary.charAt(i) == pwBinary.charAt(i)) {

            temp.append(0);
            }else {

            temp.append(1);
            }


        }
        return temp.toString();
    }

答案 1 :(得分:0)

您的解决方案的工作版本(注释不合理的部分)为:

DECLARE @dtNow AS DATETIME = GetDate()
DECLARE @dtPast AS DATETIME = DATEADD(day,-1,GetDate())
DECLARE @dtFuture AS DATETIME = '22991231'
    SET NOCOUNT ON;

  -- Temp Table is JUST Updating Rows reflecting 
--Historical Marker on existing row No content change to row's columnar     content data 

IF OBJECT_ID('tempdb..#TheTempTableName') IS NOT NULL DROP TABLE     #TheTempTableName


CREATE TABLE #TheTempTableName 
(
    ABunchOfColumns
    RowCreatedDate datetime NULL,
    RowEffectiveDate datetime NULL,
    RowTerminationDate datetime NULL,
    RowIsCurrent bit NULL,
    RowHash varchar(max) NULL,
)

INSERT INTO #TheTempTableName
(
            ABunchOfColumns
                ,RowCreatedDate
                ,RowEffectiveDate
                ,RowTerminationDate
                ,RowIsCurrent
                ,RowHash 
)


SELECT
                ABunchOfColumns
                ,RowCreatedDate
                ,RowEffectiveDate
                ,RowTerminationDate
                ,RowIsCurrent
                ,RowHash 
FROM
    (
        MERGE tblDim WITH (HOLDLOCK) AS target
            USING 
            (
                SELECT          
                ABunchOfColumns
                ,RowCreatedDate
                ,RowEffectiveDate
                ,RowTerminationDate
                ,RowIsCurrent
                ,RowHash 

                FROM dbo.tblStaging
            )       
        AS source
        ON target.PKID  = source.PKID       
        WHEN MATCHED
            AND target.RowIsCurrent = 1
            AND target.RowHash != source.RowHash                    
------- PROCESS ONE -- UPDATE --- HISTORICALLY MARK EXISTING ROWS
            THEN UPDATE SET                     
                    RowEffectiveDate    =   @dtPast
                    ,RowTerminationDate =   @dtPast
                    ,RowIsCurrent   =   0                   
-----  PROCESS TWO -- INSERT ---INSERT NEW ROWS
            WHEN NOT     MATCHED                                                          
            THEN INSERT    ---   THIS INSERT Goes directly into Target ( DIM     ) Table (New Rows not matched with PK = PK ) 
                (
                ABunchOfColumns
                ,RowCreatedDate
                ,RowEffectiveDate
                ,RowTerminationDate
                ,RowIsCurrent
                ,RowHash 
                )
                VALUES
                (                   
                source.ABunchOfColumns
                    ,@dtNow                 --source.RowCreatedDate,
                    ,@dtFuture              ---source.RowEffectiveDate,
                    ,@dtFuture              ---source.RowTerminationDate,
                    ,1                      ---source.RowIsCurrent,
                    ,source.RowHash
                )
-------PROCESS THREE a  -- INSERT ---OUTPUT MATCHED ROWS FROM PROCESS ONE THAT CAUSED     HISTORICAL MARK (CHANGES) "INSERT"
        OUTPUT 
        $action Action_Out,

                ABunchOfColumns
                ,RowCreatedDate
                ,RowEffectiveDate
                ,RowTerminationDate
                ,RowIsCurrent
                ,RowHash 
    )       
    AS MERGE_OUT
WHERE MERGE_OUT.Action_Out = 'UPDATE';

----------PROCESS THREE b  -- INSERT FROM Temp Tbl to final
--Now we flush the data in the temp table into dim table

INSERT INTO tblDim
    (
                ABunchOfColumns
                ,RowCreatedDate
                ,RowEffectiveDate
                ,RowTerminationDate
                ,RowIsCurrent
                ,RowHash 
    )
SELECT
         ABunchOfColumns
        ,@dtNow AS RowCreatedDate
        ,@dtFuture AS RowEffectiveDate
        ,@dtFuture AS RowTerminationDate
        ,1 AS RowIsCurrent
        ,RowHash

FROM #TheTempTableName  
END

但是达到目标的更快(cpu工作量)的方法是:

public static String encrypt(String str) {
    String pwBinary = getBinaryString(pw);
    String msgBinary = getBinaryString(str);
    StringBuilder temp = new StringBuilder();
    //no idea what count was supposed to do!!!
    for(int i = 0; i < msgBinary.length(); i++) {
        if(((i+1)%9!=0)) {//the space occurs once in every 9 times
            if(msgBinary.charAt(i) == pwBinary.charAt(i)) {
                temp.append(0);
            }else {
                temp.append(1);
            }
        }else {
            temp.append(" ");
            //why should we increment i? the for loop does it automatically!
        }
    }
    return temp.toString();
}