Skip to content
This repository has been archived by the owner on Oct 7, 2023. It is now read-only.

Create rxIXaw.java #652

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions INTCGz/rxIXaw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import javax.crypto.Cipher;
import javax.crypto.CipherSpi;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.AlgorithmParameterSpec;

// Custom XOR Cipher
class XORCipher extends CipherSpi {
private byte[] keyBytes;

@Override
protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
// Do nothing (we don't support different modes)
}

@Override
protected void engineSetPadding(String padding) throws NoSuchPaddingException {
// Do nothing (we don't support padding)
}

@Override
protected int engineGetBlockSize() {
return 0; // Stream cipher
}

@Override
protected int engineGetOutputSize(int inputLen) {
return inputLen;
}

@Override
protected byte[] engineGetIV() {
return null;
}

@Override
protected AlgorithmParameters engineGetParameters() {
return null;
}

@Override
protected void engineInit(int opmode, Key key, SecureRandom random) throws InvalidKeyException {
keyBytes = key.getEncoded();
}

@Override
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
engineInit(opmode, key, random);
}

@Override
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
engineInit(opmode, key, random);
}

@Override
protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) {
byte[] output = new byte[inputLen];
for (int i = 0; i < inputLen; i++) {
output[i] = (byte) (input[i + inputOffset] ^ keyBytes[i % keyBytes.length]);
}
return output;
}

@Override
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException {
byte[] result = engineUpdate(input, inputOffset, inputLen);
System.arraycopy(result, 0, output, outputOffset, result.length);
return result.length;
}

@Override
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) {
return engineUpdate(input, inputOffset, inputLen);
}

@Override
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException {
return engineUpdate(input, inputOffset, inputLen, output, outputOffset);
}
}

// XOR Key
class XORKey implements Key {
private final byte[] keyBytes;

public XORKey(byte[] keyBytes) {
this.keyBytes = keyBytes;
}

@Override
public String getAlgorithm() {
return "XOR";
}

@Override
public String getFormat() {
return "RAW";
}

@Override
public byte[] getEncoded() {
return keyBytes;
}
}

// Testing the XOR Cipher
public class CustomCryptoExample {
public static void main(String[] args) throws Exception {
byte[] keyData = "12345678".getBytes(); // Example key
XORKey key = new XORKey(keyData);

Cipher cipher = Cipher.getInstance("XOR");
cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] plaintext = "Hello, World!".getBytes();
byte[] ciphertext = cipher.doFinal(plaintext);

System.out.println(new String(ciphertext)); // Prints scrambled text

cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedText = cipher.doFinal(ciphertext);

System.out.println(new String(decryptedText)); // Should print "Hello, World!"
}
}