在java中使用String和Hex进行XOR操作

时间:2016-10-19 08:05:25

标签: java string encryption hex xor

我是cobol开发人员,但我必须在java中开发一些代码。我正在加密/解密cobol中的字符串,将十六进制值作为加密密钥和XOR操作。那些加密数据存储在DB上,然后我必须在java中读取和解密它们。我在java中使用^(xor)用于从0x00到0x7F的密钥加密的数据没有问题,问题是当密钥是从0x80到0xFF(扩展ASCII)时。我已经阅读了一些类似问题的帖子(post1post2),但我仍然遇到同样的问题。

在我的代码中,我试图使用密钥0x85(ASCII中的字符“...”)解码加密数据,它应该返回数据“desde cobol”。使用sql我在字符串中获取了encypted值,但其十六进制值是0xE1,0xE0,0xF6,0xE1,0xE0,0xA5,0xE6,0xEA,0xE7,0xEA,0xE9。 (我想省略sql代码,但我不知道如何将这些十六进制转换为字符串)。 我怎么能得到解密值'desde cobol'?

import java.io.IOException;
import java.lang.Math;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.xml.bind.DatatypeConverter;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.util.Base64;

public class Decode02 {
 public static void main(String args[]) {
    //
    Connection c = null;
    Statement stmt = null;
    String data = null;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:test2.db");
      c.setAutoCommit(false);
      System.out.println("Opened database successfully");

      stmt = c.createStatement();
      ResultSet rs = stmt.executeQuery( "SELECT * FROM tabla where key = '002';" );
      while ( rs.next() ) {
         String  key = rs.getString("key");

         data = rs.getString("data");


         System.out.println( "CLAVE = " + key );
         System.out.println( "DATOS = " + data );

         System.out.println();
      }
      rs.close();
      stmt.close();
      c.close();
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }
    System.out.println("Operation done successfully");


    String shex = "85";
    byte[] keyHex = DatatypeConverter.parseHexBinary(shex);
    System.out.println("Byte: " + keyHex[0]);

    byte[] bhex = new byte[0x85];
    String keyStr = DatatypeConverter.printHexBinary(bhex);
    System.out.println("String: " + keyStr);

    System.out.println("");

    String salida = new String(encode(data,shex));
    System.out.println("Salida: " + salida);

    String salida2 = new String(encode(data,keyStr));
    System.out.println("Salida2: " + salida2);

    System.out.println("");



 }

public static String encode(String s, String key) {
    return base64Encode(xorWithKey(s.getBytes(), key.getBytes()));
}

public static String decode(String s, String key) {
    return new String(xorWithKey(base64Decode(s), key.getBytes()));
}

private static byte[] xorWithKey(byte[] a, byte[] key) {
    byte[] out = new byte[a.length];
    for (int i = 0; i < a.length; i++) {
        out[i] = (byte) (a[i] ^ key[i%key.length]);
    }
    return out;
}

private static byte[] base64Decode(String s) {
    try {
        BASE64Decoder d = new BASE64Decoder();
        return d.decodeBuffer(s);
    } catch (IOException e) {throw new RuntimeException(e);}
}

private static String base64Encode(byte[] bytes) {
    BASE64Encoder enc = new BASE64Encoder();
    return enc.encode(bytes).replaceAll("\\s", "");

 }
}

输出:

Opened database successfully
CLAVE = 002
DATOS = ??????????

Operation done successfully
cipher: 
Byte: -123
String: 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


Decoded to be... 
Salida: BwoHCgcKBwoHCg==
Salida2: Dw8PDw8PDw8PDw==

0 个答案:

没有答案