Main step of GOST 28147-89: Difference between revisions

Content deleted Content added
Aerobar (talk | contribs)
added RPL
PSNOW123 (talk | contribs)
m Formatting text due to line length exceeding page width.
 
(5 intermediate revisions by 3 users not shown)
Line 511:
// Display the encrypted text bytes and the encrypted text
List<Character> encryptedChars = displayBytesFromBinary("The encrypted text bytes are: ", encryptedBinary);
displayBytesFromBinary("The encrypted text bytes are: ", encryptedBinary);
String encryptedText = encryptedChars.stream().map(String::valueOf).collect(Collectors.joining());
System.out.println("The encrypted text is: \"" + encryptedText + "\"" + System.lineSeparator());
Line 519 ⟶ 520:
// Display the decrypted text bytes and the decrypted text
List<Character> decryptedChars = displayBytesFromBinary("The decrypted text bytes are: ", decryptedBinary);
displayBytesFromBinary("The decrypted text bytes are: ", decryptedBinary);
byte[] bytes = new byte[decryptedChars.size()];
IntStream.range(0, decryptedChars.size()).forEach( i -> bytes[i] = (byte) decryptedChars.get(i).charValue() );
.forEach( i -> bytes[i] = (byte) decryptedChars.get(i).charValue() );
System.out.println("The decrypted text is: \"" + new String(bytes) + "\"" + System.lineSeparator());
}
Line 740 ⟶ 743:
for (let сч = 0; сч < 4; сч++) {
const яч = (S >>> (сч << 3)) & 0xFF;
нов_S += ТЗ[сч * 2][яч & 0x0F] + (ТЗ[сч * 2 + 1][яч >>> 4] << 4) << (сч << 3);
нов_S +=
ТЗ[сч * 2][яч & 0x0F]
+ (ТЗ[сч * 2 + 1][яч >>> 4] << 4)
<< (сч << 3);
}
нов_S = (нов_S << 11) + (нов_S >>> 21) & 0xFFFFFFFF ^ N[1];
нов_S =
(нов_S << 11)
+ (нов_S >>> 21)
& 0xFFFFFFFF
^ N[1];
N[1] = N[0]; N[0] = нов_S;
return N;
Line 787 ⟶ 783:
end
 
bytes2int(arr) = arr[1] + reinterpret(UInt32(arr[2]), << 8) + (UInt32(arr[3]) << 16) + (UInt32(arr[4begin])) << 24
int2bytes(x) = [UInt8reinterpret(x&0xff), UInt8((x&0xff00)>>8), UInt8(([x&0xff0000])>>16), UInt8(x>>24)]
 
function mainstep(inputbytes, keybytes)
Line 1,307 ⟶ 1,303:
» '<span style="color:blue">KBOXINIT</span>' STO
« { x}
'''WHILE''' #0h ≠ '''REPEAT'''
« 'K87' x SRB SRB SRB #255d AND B→R 1 + GET SLB SLB SLB
'K65' x SRB SRB OVER #255d AND B→RSWAP 1OVER + GETSWAP SLB SLB ORSRB
'''NEXTEND''' SWAP DROP
'K43' x SRB #255d AND B→R 1 + GET SLB OR
» » '<span style="color:blue">FB→LIST</span>' STO
'K21' x #255d AND B→R 1 + GET OR
RLB RL RL RL
» » '<span style="color:blue">F</span>' STO
« { } « SWAP SLB OR » → input key result cast
« key REVLIST cast STREAM <span style="color:grey">@ convert key into a little-endian integer</span>
input 1 4 SUB REVLIST cast STREAM + <span style="color:blue">FB→LIST</span> <span style="color:grey">@ same for input part 1, then add and back to list format</span>
{ K21 K43 K65 K87 } SWAP B→R 1 ADD GET <span style="color:grey">@ replace bytes according to the table</span>
input 5 8 SUB REVLIST cast STREAM XOR
REVLIST cast STREAM <span style="color:grey">@ back to little-endian format</span>
1 4 '''START'''
RLB RL RL RL <span style="color:blue">B→LIST</span> <span style="color:grey">@ roll 11 bits, back to list format</span>
DUP #255d AND 'result' SWAP STO+
input 5 8 SUB XOR <span style="color:grey">@ add part 2</span>
SRB
input 1 4 SUB + <span style="color:grey">@ append part 1</span>
'''NEXT''' DROP
result input 1 4 SUB +
» » '<span style="color:blue">MAINSTEP</span>' STO
Line 1,334 ⟶ 1,327:
1: { # 1Fh # 88h # CFh # 7h # 21h # 4h # 3Bh # 4h }
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::convert::TryInto;