Datentypen Rechnen mit signed Byte-Array - unsinged byte

huckleberry

Bekanntes Mitglied
Hallo Forum,

ich habe ein irgendein Objekt. Diesen möchte ich gerne verschlüsseln. Dazu wandele ich das Objekt mit der Methode
Java:
.valueToBytes()
in ein Byte-Array (-128 bis +127). Wenn ich eine negative Zahl habe, verschlüssele und dann wieder entschlüssele komme ich folgerichtig nicht mehr auf die negative Zahl, klar.

unsigned byte gibt es ja auch nicht in Java. Habt ihr Tipps?

Java:
public class TestEnv {
	
	public static final byte zt = -84;
	
	public static byte [] plain = { -84, 116 ,117 ,100 ,46 ,99 ,97 ,114 ,50 ,99 ,97 ,114 ,46 ,112 ,114 ,105 ,118 ,97,46 ,109 ,101 ,46 ,84 ,97 ,103 ,10 ,83 ,116 ,97 ,109 ,112 ,76 ,0 ,19 ,115 ,101 ,110 ,100 ,101 ,114 ,115 ,75 ,-88 ,-80 ,94 ,-128 ,115 ,114 ,0 ,17 ,106 ,97 ,0 ,0 ,12 ,119 ,8 ,0 ,0 ,16 ,0 ,0 ,0 ,0 ,120 };
	
    public static byte modPow(byte param_base, int param_exp, int param_mod) {
        int result = 1;
        for(int e = 0; e < param_exp; e++) {
            result = ((result * param_base) % param_mod) % Byte.MAX_VALUE;
        }
        return (byte) result;
    }
	
	public String getBytesString(byte [] t){
		String s = "";
		for (int i = 0; i < t.length; i++) s += (int)(t[i])+" ,";
		return s;
	}
	
//	public static byte [] enCryptMUTV(byte [] input, int d) {
//		byte [] result = new byte[input.length];
//			for(int i = 0; i < input.length; i++) {
//				int cbyte = (int) input[i];
//				result [i] = modPow(cbyte, d, 82703);			
//			}
//		return result;
//	}
//	
//	public static byte [] deCryptMUTV(byte [] mutv){
//		return null;
//	}
	
	public static String getByteString(byte [] tt){
		String s = "";
		for (int i = 0; i < tt.length; i++) s += (int)(tt[i])+" ,";
		return s;
	}
	
	public static String getByteByte(byte [] tt){
		String s = "";
		for (int i = 0; i < tt.length; i++) s += (byte)(tt[i])+" ,";
		return s;
	}
	
	public static byte enCryptMUTV(byte input, int d) {
		return modPow(input, d, 82703);			
	}
	
	public static byte deCryptMUTV(byte mutv, int e){
		return (byte) modPow(mutv, e, 82703);		
	}
	
	public static void main(String[] args) {
		
		//  n: 82703 | e: 11 | d: 44771 | p1: 433 | p2: 191 | phi:82080        

		for (byte i = -128; i < 127; i++) {
			System.out.print("PlainText: " + i);
			byte tt = modPow(i, 44771, 82703);
			System.out.print(" CryptText: " + tt);
			tt = modPow(tt, 11, 82703);
			System.out.print(" EncryptText: " + tt);
			if(zt == tt) System.out.println(" OK!");
			else System.out.println(" ERROR!");
		}
		
//		System.out.println("PlainText: "+getByteString(plain));
//		System.out.println(" ZC ");
//		System.out.println("PlainText: "+getByteByte(plain));
//		byte [] r = enCryptMUTV(plain, 44771);
//		System.out.println("EncryptedText: "+getByteString(r));
//		r = deCryptMUTV(r);
//		System.out.println("DecryptedText: "+getByteString(r));
	}
}
 
Kannst ja damit mal versuchen, ein "(un)signed" byte zu bekommen (jz auf die schnelle):
Java:
public static int getUnsignedByte(byte b) 
	{
		return b & 0xff;
	}
	
	public static byte getSignedByte(int b)
	{
		return (byte) b;
	}
 
Gelöst: Einfach von und n Zweierkomplement umwandeln 🙂

Java:
	public static int getUnsignedByte(byte b){
		return (b < 0) ? (256+b) : b;
	}
	
	public static byte getSignedByte(int b){
		return (b > 127) ? (byte)(-256+b) : (byte)b;
	}
 

Zurück
Oben