Last active
October 27, 2022 06:26
-
-
Save ak-git/3aba70d31effe505b4af298e90cb262f to your computer and use it in GitHub Desktop.
Лекция по информационной безопасности и системам распределённого реестра
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ak.numbers; | |
import java.nio.charset.StandardCharsets; | |
import java.util.BitSet; | |
import java.util.stream.Collectors; | |
import javax.annotation.Nonnull; | |
import com.ak.util.Strings; | |
import org.assertj.core.api.Assertions; | |
import org.junit.jupiter.params.ParameterizedTest; | |
import org.junit.jupiter.params.provider.ValueSource; | |
class CRCTest { | |
@ParameterizedTest | |
@ValueSource(strings = {"0", "1", "2", "3", "fdlkjfdlfj7"}) | |
void test(@Nonnull String message) { | |
String binaryString = message.codePoints() | |
.mapToObj(symbol -> "%8s%s".formatted(Integer.toBinaryString(symbol), Strings.low(2)).replace(" ", "0")) | |
.collect(Collectors.joining(" | ")); | |
long ones = BitSet.valueOf(message.getBytes(StandardCharsets.UTF_8)).stream().count(); | |
System.out.printf("msg = '%s'; binary = [ %s ]; ones = %d; parityBit = %s %n", | |
message, binaryString, ones, | |
"%1s%s".formatted(Long.toBinaryString(ones & 0b0000_0001), Strings.low(2)).replace(" ", "0") | |
); | |
Assertions.assertThat(ones).isPositive(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ak.numbers; | |
import java.nio.charset.StandardCharsets; | |
import java.security.InvalidKeyException; | |
import java.security.Key; | |
import java.security.KeyPair; | |
import java.security.KeyPairGenerator; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.util.Arrays; | |
import java.util.Base64; | |
import java.util.logging.Logger; | |
import java.util.random.RandomGenerator; | |
import javax.annotation.Nonnull; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.spec.SecretKeySpec; | |
import org.assertj.core.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
class CryptoTest { | |
private static final RandomGenerator RANDOM = new SecureRandom(); | |
@Test | |
void testHash() throws NoSuchAlgorithmException { | |
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); | |
byte[] msg = "0".getBytes(StandardCharsets.UTF_8); | |
messageDigest.update(msg); | |
Logger.getAnonymousLogger().info(digestToString(messageDigest)); | |
Assertions.assertThat(messageDigest.getDigestLength()).isEqualTo(256 / 8); | |
} | |
private static String digestToString(@Nonnull MessageDigest messageDigest) { | |
byte[] digest = messageDigest.digest(); | |
StringBuilder sb = new StringBuilder(digest.length * 2); | |
for (byte b : digest) { | |
sb.append(String.format("%x", b)); | |
} | |
return sb.toString(); | |
} | |
@Test | |
void testAES() throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException { | |
String password = "password"; | |
byte[] key = Arrays.copyOf(MessageDigest.getInstance("SHA-256").digest(password.getBytes(StandardCharsets.UTF_8)), 16); | |
String strToEncrypt = String.valueOf("%c".formatted( | |
RANDOM.ints(1, 'a', 'z').findAny().orElseThrow()) | |
); | |
String msgA = encodeAES(strToEncrypt, key); | |
Logger.getAnonymousLogger().info(msgA); | |
String decodeAES = decodeAES(msgA, key); | |
Logger.getAnonymousLogger().info(decodeAES); | |
Assertions.assertThat(strToEncrypt).isNotEqualTo(msgA).isEqualTo(decodeAES); | |
} | |
@Nonnull | |
private static String encodeAES(@Nonnull String strToEncrypt, @Nonnull byte[] password) | |
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { | |
Key secretKey = new SecretKeySpec(password, "AES"); | |
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | |
cipher.init(Cipher.ENCRYPT_MODE, secretKey); | |
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8))); | |
} | |
@Nonnull | |
private static String decodeAES(@Nonnull String strToDecrypt, @Nonnull byte[] password) | |
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { | |
Key secretKey = new SecretKeySpec(password, "AES"); | |
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | |
cipher.init(Cipher.DECRYPT_MODE, secretKey); | |
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); | |
} | |
@Test | |
void testRSA() throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException { | |
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); | |
keyPairGenerator.initialize(1024, new SecureRandom()); | |
KeyPair keyPair = keyPairGenerator.generateKeyPair(); | |
String strToEncrypt = String.valueOf("%c".formatted( | |
RANDOM.ints(1, 'a', 'z').findAny().orElseThrow()) | |
); | |
String msgA = encodeRSA(strToEncrypt, keyPair.getPrivate()); | |
Logger.getAnonymousLogger().info(msgA); | |
String decodeRSA = decodeRSA(msgA, keyPair.getPublic()); | |
Logger.getAnonymousLogger().info(decodeRSA); | |
Assertions.assertThat(strToEncrypt).isNotEqualTo(msgA).isEqualTo(decodeRSA); | |
} | |
@Nonnull | |
private static String encodeRSA(@Nonnull String strToEncrypt, @Nonnull Key privateKey) throws NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException { | |
Cipher cipher = Cipher.getInstance("RSA"); | |
cipher.init(Cipher.ENCRYPT_MODE, privateKey); | |
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8))); | |
} | |
@Nonnull | |
private static String decodeRSA(@Nonnull String strToDecrypt, @Nonnull Key publicKey) | |
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { | |
Cipher cipher = Cipher.getInstance("RSA"); | |
cipher.init(Cipher.DECRYPT_MODE, publicKey); | |
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ak.numbers; | |
import java.nio.charset.StandardCharsets; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.logging.Logger; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import javax.annotation.Nonnull; | |
import com.ak.util.Strings; | |
import org.assertj.core.api.Assertions; | |
import org.junit.jupiter.params.ParameterizedTest; | |
import org.junit.jupiter.params.provider.ValueSource; | |
class SHA2Test { | |
private static final int ZEROS = 2; | |
@ParameterizedTest | |
@ValueSource(strings = "100$") | |
void test(@Nonnull String message) { | |
String append = IntStream.range(0, 1024 * 1024 * 1024).mapToObj(Integer::toString).parallel().filter(tail -> { | |
try { | |
byte[] digest = digest(message + tail); | |
return IntStream.range(0, ZEROS).map(operand -> digest[operand]).allMatch(value -> value == 0); | |
} | |
catch (NoSuchAlgorithmException e) { | |
return false; | |
} | |
}).findAny().orElseThrow(); | |
Logger.getLogger(getClass().getName()).info(append); | |
Assertions.assertThat(append).isNotBlank(); | |
} | |
private static byte[] digest(@Nonnull String message) throws NoSuchAlgorithmException { | |
MessageDigest md = MessageDigest.getInstance("SHA-256"); | |
md.update(message.getBytes(StandardCharsets.UTF_8)); | |
byte[] digest = md.digest(); | |
String firstBytes = IntStream.range(0, ZEROS).mapToObj(i -> toBinaryString(digest[i])).collect(Collectors.joining(" | ")); | |
System.out.printf("msg = '%s'; SHA-256 = %s; firstBytes = [ %s ]%n", message, digestToString(digest), firstBytes); | |
return digest; | |
} | |
private static String toBinaryString(int b) { | |
return "%8s%s".formatted(Integer.toBinaryString(b & 0xff), Strings.low(2)).replace(" ", "0"); | |
} | |
private static String digestToString(@Nonnull byte[] digest) { | |
StringBuilder sb = new StringBuilder(digest.length * 2); | |
for (byte b : digest) { | |
sb.append("%02x".formatted(b)); | |
} | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ak.numbers; | |
import java.nio.charset.StandardCharsets; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import javax.annotation.Nonnull; | |
import org.assertj.core.api.Assertions; | |
import org.junit.jupiter.params.ParameterizedTest; | |
import org.junit.jupiter.params.provider.ValueSource; | |
class SHATest { | |
@ParameterizedTest | |
@ValueSource(strings = {"100$65377774"}) | |
void test(@Nonnull String message) throws NoSuchAlgorithmException { | |
MessageDigest md = MessageDigest.getInstance("SHA-256"); | |
md.update(message.getBytes(StandardCharsets.UTF_8)); | |
System.out.printf("msg = '%s'; SHA-256 = %s%n", message, digestToString(md.digest())); | |
Assertions.assertThat(md.getDigestLength()).isEqualTo(256 / 8); | |
} | |
private static String digestToString(@Nonnull byte[] digest) { | |
StringBuilder sb = new StringBuilder(digest.length * 2); | |
for (byte b : digest) { | |
sb.append("%02x".formatted(b)); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment