I am reversing an old game for translate it, here is the compression algorithm , I want to know what algorithm this is.
the following is the python pseudocode
while True:
byte = rawdata[praw]
low_byte = byte & 0xF
high_byte = byte >> 4
for i in range(low_byte):
buffer[pbuffer] = rawdata[praw]
pbuffer += 1
praw += 1
for i in range(high_byte):
low_byte2 = rawdata[praw] & 0xF
high_byte2 = rawdata[praw] >> 4
p = pbuffer - low_byte - 1
for i in range(high_byte2):
buffer[pbuffer] = buffer[p]
pbuffer += 1
praw += 1
if low_byte or high_byte is 0, will call this function
def func(data):
# do...while...
while data is even number:
data = (data << 7) | rwadata[praw]
p += 1
if (data & 1) != 0:
break
return (data >> 1)
Related
I am very difficult to display all the output results.
this code.
DEF VAR INPUTAN AS INTEGER.
DEF VAR i AS INTEGER.
DEF VAR j AS INTEGER.
DEF VAR a AS INTEGER.
DEF VAR rows AS INT.
DEF VAR pascal AS CHAR FORMAT "x(25)".
SET INPUTAN.
a = 1.
REPEAT i = 0 TO INPUTAN:
rows = i.
DISPLAY rows.
REPEAT j = 0 TO i :
IF j = 0 OR j = i THEN DO:
a = 1.
END.
ELSE
a = a * (i + 1 - j) / j.
pascal = STRING(a).
display a.
END.
END.
DEF VAR INPUTAN AS INTEGER.
DEF VAR i AS INTEGER.
DEF VAR j AS INTEGER.
DEF VAR a AS INTEGER.
DEF VAR rows AS INT.
DEF VAR pascal AS CHAR.
SET INPUTAN.
a = 1.
REPEAT i = 0 TO INPUTAN:
rows = i.
/*DISPLAY rows. */
REPEAT j = 0 TO i :
IF j = 0 OR j = i THEN DO:
a = 1.
END.
ELSE
a = a * (i + 1 - j) / j.
IF j = 0 THEN
pascal = pascal + FILL(" ", INPUTAN - i).
pascal = pascal + STRING(a) + " ".
IF j = i THEN
pascal = pascal + CHR(13).
/* display a.*/
END.
END.
MESSAGE pascal
VIEW-AS ALERT-BOX INFO BUTTONS OK.
I found this example which sorta works... but it's not perfect. For starters it plots 10 at once but that's an easy fix. Problem is that it does not dynamically get line endings and instead relies on byte count.
pkg load instrument-control
s1 = serialport("/dev/ttyUSB0", 9600)
flush(s1);
y_temp = cell(10,1)
y = 0
while true
for i = 1:10
y_serial = str2num(char(fread(s1,8)))
y_temp {i,1} = y_serial
endfor
y = cat(1, y, y_temp{1:10})
plot(y)
pause(1)
endwhile
srl_close(s1)
This works... so long as the number of bytes per string is 8. How can I make this dynamic and split by line endings?
All I need to do is plot incrementing by 1 x_value and a float y_value from serial.
https://www.edn.com/read-serial-data-directly-into-octave/
Solution found.
Their is no inbuilt solution but you can make your own function like:
function [char_array] = ReadToTermination (srl_handle, term_char)
% parameter term_char is optional, if not specified
% then CR = 'r' = 13dec is the default.
if (nargin == 1)
term_char = 13;
end
not_terminated = true;
i = 1;
int_array = uint8(1);
while not_terminated
val = fread(srl_handle, 1);
if(val == term_char)
not_terminated = false;
end
% Add char received to array
int_array(i) = val;
i = i + 1;
end
% Change int array to a char array and return a string array
char_array = char(int_array);
endfunction
This does not actually work straight off and I had to add \n to my Arduino script.
disp('load instrument control:');
pkg load instrument-control
disp('SerialPort:');
serialportlist
cpx = serialport ("COM6", 57600);
disp(cpx);
disp(sprintf('InBuffer:%3d', cpx.NumBytesAvailable));
if (cpx.NumBytesAvailable > 0)
zx = read(cpx, cpx.NumBytesAvailable);
disp(sprintf("IN:\r\n%s", zx));
endif;
Recently, I have been trying to educate myself on how to encrypt and decrypt using the Vigenere Cipher.
I have successfully encrypted the message and these are the steps I undertook to achieve encryption:
Encryption Key: Set
Message: Top secret
Step 1: Numerical representation of key is 18, 4, 19 (Using the table below)
Working Out:
Reminder:
P is the set of plaintext units
C is the set of ciphertext units
K is the set of keys
E: P x K -> C is the encryption function
D: C x K -> P is the decryption function
Plaintext: top secret
Ciphertext: ISIKIVJIM
Although I have managed to encrypt the message "top secret" I am struggling to decrypt messages using the Vigenere Cipher method using the numerical technique I used above. Can someone explain to me how I can decrypt lets say: ISIKIVJIM (the ciphertext from above) to its original plain text message which is "top secret".
Thanks.
As pointed out in the comments the decryption formula is : p = c - k mod 26, also note that we have to perform modular arithmetic so our answer for any input should belong in the range of 0 - 25, i.e if we get a negative number we can add 26(i.e the number we are taking modulo by) until we are within this range you can read more about this here :
https://en.wikipedia.org/wiki/Modular_arithmetic
So the decryption will be like :
L = 11 - 18 = -7 mod 26 = -7 + 26 = 19 = T
S = 18 - 4 = 14 mod 26 = 14 = O
I = 8 - 19 = -11 mod 26 = -11 + 26 = 15 = P
ans so on...
I have also written a c++ code : http://ideone.com/M3BAmq
I recently wrote a java program that encrypts and decrypts in Vigenere using bytes. You need to convert the plain text/ crypt text into byte array and pass it in.
public static byte [] encryptVigenere (byte [] pt, String key)
{
byte [] c_text = new byte [pt.length];
byte [] key_text = key.getBytes();
byte tmp;
int shift;
for (int i = 0, j = 0; i < pt.length; i++)
{
if (j >= key_text.length)
j = 0;
shift = key_text[j] - 65; //index of alphabet
tmp = (byte) (pt[i] + shift);
if (tmp > 'Z')
tmp = (byte) (pt[i] - (26-shift));
c_text[i] = tmp;
j++;
}
return c_text;
}
public static byte [] decryptVigenere (byte [] ct, String key)
{
byte [] p_text = new byte [ct.length];
byte [] key_text = key.getBytes();
byte tmp;
int shift;
for (int i = 0, j = 0; i < ct.length; i++)
{
if (j >= key_text.length)
j = 0;
shift = key_text[j] - 65; //index of alphabet
tmp = (byte) (ct[i] - shift);
if (tmp < 'A')
tmp = (byte) (ct[i] + (26-shift));
p_text[i] = tmp;
j++;
}
return p_text;
}
I want to convert decimal numbers (price values to be exact), which may:
a) contain 1 OR 2 decimals
b) have either a . or , as decimal separator
to an integer value in cents.
So:
3,5 becomes 350
3,50 becomes 350
3.5 becomes 350
3.50 becomes 350
1,000.34 becomes 100034
1.000,34 becomes 100034
Without building a function that does all these checks is there a way in ASP.NET to do this more quickly?
**UPDATE **
Thanks to Nicholas:
I now have this in VB.NET
Private Shared Function ConvertToPriceInCents(s As String) As Integer
If s Is Nothing Then
Throw New ArgumentNullException("s")
End If
If s = "" Then
Throw New ArgumentOutOfRangeException("s", "s must not be empty.")
End If
Dim priceInCents As Integer = 0
Dim scale As Integer = 1
Dim i As Integer = s.Length
' collect the fractional part; identify the decimal separator
While System.Threading.Interlocked.Decrement(i) >= 0
Dim n As Integer = Asc(s(i)) - Asc("0"c)
If n < 0 OrElse n > 9 Then
Exit While
End If
' bail out, we found the decimal separator
priceInCents += n * scale
scale *= 10
End While
Dim decimalSeparator As Char = s(i)
Dim groupSeparator As Char = If(decimalSeparator = "."c, ","c, "."c)
If scale <> 10 AndAlso scale <> 100 Then
Throw New FormatException("value must have 1 or 2 digits to the right of the decimal separator")
End If
If decimalSeparator <> ","c AndAlso decimalSeparator <> "."c Then
Throw New FormatException("Invalid decimal separator")
End If
' if we only found one digit to the right of the decimal separator,
' we need to normalize and scale up by a factor of 10 (so something like 3.5 represents 350 cents)
If scale = 10 Then
scale *= 10
priceInCents *= 10
End If
' get the integer portion of value
' we're being a little lax here and ignoring group separators regardless of position.
' It's a hard thing to do, especially when you consider that
' - group sizes vary across cultures, and
' - aren't necessarily uniform in size.
While System.Threading.Interlocked.Decrement(i) >= 0
Dim c As Char = s(i)
If c = groupSeparator Then
Continue While
End If
Dim n As Integer = Asc(s(i)) - Asc("0"c)
If n < 0 OrElse n > 9 Then
Throw New FormatException("invalid group separator")
End If
priceInCents += n * scale
scale *= 10
End While
' If we haven't thrown an exception yet,
' we have the value in cents: return it.
Return priceInCents
End Function
You write a method that looks something like this:
static int ConvertToPriceInCents( string s )
{
if ( s == null ) throw new ArgumentNullException("s") ;
if ( s == "" ) throw new ArgumentOutOfRangeException("s","s must not be empty." ) ;
int priceInCents = 0 ;
int scale = 1 ;
int i = s.Length ;
// collect the fractional part; identify the decimal separator
while ( --i >= 0 )
{
int n = s[i] - '0' ;
if ( n < 0 || n > 9 ) break ; // bail out, we found the decimal separator
priceInCents += n*scale ;
scale *= 10 ;
}
char decimalSeparator = s[i] ;
char groupSeparator = decimalSeparator == '.' ? ',' : '.' ;
if ( scale != 10 && scale != 100 ) throw new FormatException("value must have 1 or 2 digits to the right of the decimal separator") ;
if ( decimalSeparator != ',' && decimalSeparator != '.' ) throw new FormatException("Invalid decimal separator") ;
// if we only found one digit to the right of the decimal separator,
// we need to normalize and scale up by a factor of 10 (so something like 3.5 represents 350 cents)
if ( scale == 10 )
{
scale *= 10 ;
priceInCents *= 10 ;
}
// get the integer portion of value
// we're being a little lax here and ignoring group separators regardless of position.
// It's a hard thing to do, especially when you consider that
// - group sizes vary across cultures, and
// - aren't necessarily uniform in size.
while ( --i >= 0 )
{
char c = s[i] ;
if ( c == groupSeparator ) continue ;
int n = s[i] - '0' ;
if ( n < 0 || n > 9 ) throw new FormatException("invalid group separator") ;
priceInCents += n*scale ;
scale *= 10 ;
}
// If we haven't thrown an exception yet,
// we have the value in cents: return it.
return priceInCents ;
}
HI I am tiring to implement a CFB with DES. I think i am able to encrypt using with CFB but how can I decrypt?? My main issue is CFB code for encrypting using CFB correct ??. Due to the restriction I have, I am unable to use other library.
for (int i = 0; i < VecMSG.size(); i++) {
DESEncrypt(IV, Key);
stringstream str;
str << bitset < 32 > (V[0]); //First 32 bits convert to string
str << bitset < 32 > (V[1]); //Second 32 bits covert to string and join with the first
VText2 = VText = str.str(); //Store in 2 different strings
VText = VText.substr(0, 5); //Take the most significant first 5 bits in the form of
str.str("");
bitset < 2 > mybits(VText); //covert to bits
bitset < 2 > mybits2(VecMSG[i]); //covert plaintext bits from string to bits
str << (mybits ^= mybits2); //XOR with and convert to string
VecCipher.push_back(str.str()); //Store in a different vector
str.str("");
VText2 = VText2.substr(5) + VecCipher[i]; //Remove the first 5 bits and join ciphertext to the end
V[0] = (unsigned int)VText2.substr(0,32).c_str();
V[1] = (unsigned int)VText2.substr(32).c_str();
}