Skip to content

Commit

Permalink
basic wallet class, xor encrytion with axample
Browse files Browse the repository at this point in the history
  • Loading branch information
saborsoft committed May 6, 2024
1 parent 3ab979d commit 776b47d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hu.saborsoft.blockchain.example.password;

import hu.saborsoft.blockchain.support.UtilityMethods;

public class XORExample {

public static void main(String[] args) {
String message = "At the most beautiful you."
+ "remember the most beautiful you.";
String password = "blockchains";
byte[] encry = UtilityMethods.encryptionByXOR(message.getBytes(), password);
// take a peak at the encrypted data.
System.out.println(new String(encry));
byte[] decrypted = UtilityMethods.decryptionByXOR(encry, password);
System.out.println("after proper decryption, the message is:\n");
System.out.println(new String(decrypted));
System.out.println("\nwith an incorrect password, "
+ "the decrypted message looks like:");
// let's try an incorrect password.
decrypted = UtilityMethods.decryptionByXOR(encry, "Block Chain");
// examine the wrongly decrypted message
System.out.println(new String(decrypted));
}

}
25 changes: 25 additions & 0 deletions src/main/java/hu/saborsoft/blockchain/support/UtilityMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,29 @@ public static void displayTransaction(Transaction t, PrintStream out, int level)
displayTab(out, level, "}");
}

public static byte[] encryptionByXOR(byte[] key, String password) {
int more = 100;
byte[] p = messageDigestSHA256ToBytes(password);
byte[] pwds = new byte[p.length * more];
for (int i = 0, z = 0; i < more; i++) {
for (int j = 0; j < p.length; j++, z++) {
pwds[z] = p[j];
}
}
byte[] result = new byte[key.length];
int i;
for (i = 0; i < key.length && i < pwds.length; i++) {
result[i] = (byte) ((key[i] ^ pwds[i]) & 0xFF);
}
while (i < key.length) {
result[i] = key[i];
i++;
}
return result;
}

public static byte[] decryptionByXOR(byte[] key, String password) {
return encryptionByXOR(key, password);
}

}
32 changes: 32 additions & 0 deletions src/main/java/hu/saborsoft/blockchain/wallet/Wallet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package hu.saborsoft.blockchain.wallet;

import hu.saborsoft.blockchain.support.UtilityMethods;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

public class Wallet {

private KeyPair keyPair;

private String walletName;

public Wallet(String walletName) {
keyPair = UtilityMethods.generateKeyPair(2048);
this.walletName = walletName;
}

public String getName() {
return walletName;
}

public PublicKey getPublicKey() {
return keyPair.getPublic();
}

public PrivateKey getPrivateKey() {
return keyPair.getPrivate();
}

}

0 comments on commit 776b47d

Please sign in to comment.