padding to get 128 bits in java - java

import javax.swing.JOptionPane;
public class Stack {
static int count = 0;
static String res[] = new String[16];
public static void main(String[] args) {
String str = JOptionPane.showInputDialog("enter text");
String abc = "";
char[] messChar = str.toCharArray();
for (int i = 0; i < messChar.length; i++)
abc += Integer.toBinaryString(messChar[i]);
System.out.println(abc.length());
int leg = (int) ((abc.length() / 128) + 1);
String[] spl = new String[leg];
int j = 0, i;
for (i = 0; i < abc.length(); i++) {
if (count == 128) {
if (i == 128)
spl[j] = abc.substring(0, 128);
else
spl[j] = abc.substring(i - 128, i);
count = 0;
j++;
}
count++;
}
spl[j] = abc.substring(i - count, i);
for (int k = 0; k < spl.length; k++)
System.out.println(spl[k].length());
for (i = 0; i < spl.length; i++) {
String binary = spl[i];
System.out.println(binary);
}
}
}
The input for the above code is text. If I give some text, it converts it into binary and then splits it into 128 bits. But at last some bits are remaining which are less than 128 bits. However, I need exactly 128 bits. So how can I do padding to get 128 bits in Java? I am implementing the AES Algorithm 128 bit key.

Related

I search to implement function get most occurence character in string and count it

public class GFG
{
static final int ASCII_SIZE = 256;
static char getMaxOccuringChar(String str)
{
int count[] = new int[ASCII_SIZE];
int len = str.length();
for (int i=0; i<len; i++)
count[str.charAt(i)]++;
int max = -1;
char result = ' ';
for (int i = 0; i < len; i++) {
if (max < count[str.charAt(i)]) {
max = count[str.charAt(i)];
result = str.charAt(i);
}
}
return result;
}
I stopped here and can't count the most repeated character.
The problem is that you should be iterating over the characters in the count array and not the string contents. You already counted them. Also the result will be the character represented by the value of i so you can just cast i to a char. Lastly unless you want to count spaces you should ignore character 32 (a space) and possibly other white-space characters or they may end up being the most common character. Here's a version that will count the most repeated character:
class GFG {
public static void main(String[] args) {
GFG s = new GFG();
char c = s.getMaxOccuringChar("People say there is nothing like a banana cream pie");
System.out.println("Max char is " + c);
}
static final int ASCII_SIZE = 256;
static char getMaxOccuringChar(String str) {
int count[] = new int[ASCII_SIZE];
int len = str.length();
for (int i = 0; i < len; i++)
count[str.charAt(i)]++;
int max = -1;
char result = ' ';
for (int i = 0; i < count.length; i++) {
if (max < count[i] && i != 32) {
max = count[i];
System.out.println("Max: "+max+" char: "+(char)i);
result = (char)i;
}
}
return result;
}
}
This will print out:
Max: 0 char:
Max: 1 char: P
Max: 6 char: a
Max: 7 char: e
Max char is e
In your second for loop you need
for (int i = 0; i < count.length; i++)
instead of
for (int i = 0; i < len; i++)
and then count[i]instead of count[str.charAt(i)]
and finally result = i instead of result = str.charAt(i)

Integer variable does not update when if condition is true

public class test
{
static Scanner store = new Scanner(System.in);
public static void main(String[] args)
{
String str1 = args[0];
String str2 = args[1];
System.out.printf("%nThere are %d dissimilar characters in the two strings.%n", CountNotSim(str1, str2));
}
public static int CountNotSim(String str1, String str2)
{
String s1 = str1.toLowerCase();
String s2 = str2.toLowerCase();
char[] a1 = new char[s1.length()];
char[] a2 = new char[s2.length()];
for (int g = 0; g < s1.length(); g++)
a1[g] = s1.charAt(g);
for (int h = 0; h < s2.length(); h++)
a2[h] = s2.charAt(h);
int check = 0, stored;
char[] array = new char[26];
int ctr = s1.length() + s2.length();
for (int i = 0; i < a1.length; i++)
{
check = 0;
stored = 0;
for (int j = 0; j < a2.length; j++)
{
if (a1[i] == a2[j])
{
check++;
for (int k = 0; k < 26; k++)
{
if (array[k] == ' ')
if (stored == 0)
array[k] = a1[i];
if (a1[i] == array[k])
{
stored = 1;
break;
}
}
System.out.print(stored + "/ ");
}
}
if (check > 0)
{
if (stored == 0)
ctr -= (check + 1);
else if (stored == 1)
ctr--;
}
System.out.print(ctr + " "); //checker
}
System.out.println();
return ctr;
}
}
The program checks for dissimilar letters in two strings inputted from the command line. Variable "stored" is supposed to change to 1 whenever there's a match to avoid extra deductions to variable "ctr". However, for some reason, not only does "stored's" value not change, the array "array" also doesn't update its elements whenever there's a match. I'm at a loss on how to fix it--nothing looks incorrect.
You wrote this:
char[] array = new char[26];
...
for (int k = 0; k < 26; k++)
{
if (array[k] == ' ') {
...
But you simply set the length of array not its content.
As a char array, it's filled with the default char value, which is not the character space but the value 0 (not the character 0, but the numeric value 0)
So array[k] == ' ' will never be true.
Try with that:
for (int k = 0; k < 26; k++)
{
if (array[k] == 0) {
...

8 bit full adder in Java

In 'eightbitfulladder' function,I am trying to complement the sumno which is the result of addition of a positive number and a negative number.I found out the number it is sending in the 'convertobyte' function is different from the sumno computed. This is wierd. Can someone explain me what is happening.
import static java.lang.Math.pow;
public class CAModifiedBoothsMultiplier {
public byte byteArrayTobyte(byte[] b) {
byte value = 0;
for (byte i = 0; i < 8; i++) {
value += (b[i] * (pow(2, i)));
}
return value;
}
public byte[] twosComplement(byte x) {
byte y = (byte) (~x + 1);
byte mask = 1;
mask = (byte) (mask << 7);
byte num[] = new byte[10];
byte i = 0, num1 = 0;
for (i = 0; i < 8; i++) {
if ((y & mask) == 0)
num1 = 0;
else
num1 = 1;
y = (byte) (y << 1);
num[i] = num1;
}
return num;
}
public byte[] saveByte(byte number) {
byte mask = 1;
mask = (byte) (mask << 7);
byte num[] = new byte[10];
byte num1 = 0;
byte i = 0;
for (i = 0; i < 8; i++) {
if ((number & mask) == 0)
num1 = 0;
else
num1 = 1;
number = (byte) (number << 1);
num[i] = num1;
}
return num;
}
public byte[] eightbitFullAdder(byte a, byte b, byte cin) {
byte sum = 0, temp1 = 0, cout = 0;
byte sumno[] = new byte[10];
byte ain[] = saveByte(a);
byte bin[] = new byte[10];
if (cin == 1) {
bin = twosComplement(b);
cin = 0;
} else {
bin = saveByte(b);
}
System.out.print("The number entered is, a :");
for (int i = 0; i < 8; i++) {
System.out.print(ain[i]);
}
System.out.println("\n");
System.out.print("The number entered is, b :");
for (int i = 0; i < 8; i++) {
System.out.print(bin[i]);
}
System.out.println("\n");
for (int i = 7; i >= 0; i--) {
temp1 = (byte) (ain[i] ^ bin[i]);
sum = (byte) (temp1 ^ cin);
sumno[i] = sum;
cout = (byte) ((ain[i] & bin[i]) | (cin & temp1));
if (i != 0)
cin = cout;
}
for (int i = 0; i < 8; i++) {
System.out.print(sumno[i]);
}
System.out.println("\n");
if (sumno[0] == 1) {
byte[] sumnocomp = new byte[10];
byte temp2 = 0;
temp2 = byteArrayTobyte(sumno);
System.out.print(temp2);
sumnocomp = twosComplement(temp2);
return sumnocomp;
} else {
return sumno;
}
}
public static void main(String args[]) {
CAModifiedBoothsMultiplier mbm = new CAModifiedBoothsMultiplier();
byte x = 5;
byte complementedno[];
complementedno = mbm.twosComplement(x);
for (int i = 1; i <= 8; i++)
System.out.print(complementedno[i]);
System.out.println("\n");
byte a = 6, b = 8, cin = 1;
byte fulladder[] = mbm.eightbitFullAdder(a, b, cin);
System.out.print("The sum of numbers entered is :");
for (int i = 0; i < 8; i++) {
System.out.print(fulladder[i]);
}
System.out.println("\n");
}
}

Brute force own encryption in Java

I am writing a program for a class to first encrypt a string with a predetermined key. That part is done. Next part is where i have a problem or not a problem per se. its a question of redundancy. After this I am supposed to do a KPA on the string and the encrypted string to find the key.
Which is working but i am using like 15 nested for loops for the brute force. Is there another way to do this? without doing it recursively!
static String Key = null;
public static void main(String[] args) {
long startTime = System.nanoTime();
long startTime1 = System.currentTimeMillis();
int cntr = 0;
String key = "AAAAAAAAAAADDDAM";
String plaintext = "Secretfoemotherd";
StringBuilder cipher = new StringBuilder();
StringBuilder brutus = new StringBuilder();
byte[] ciphertext = encrypt(byteT(key), byteT(plaintext));
for (int i = 0; i < ciphertext.length; i++) {
cipher.append(ciphertext[i]);
}
while (true) {
char[] nkey = new char[16];
for (int i1 = 65; i1 < 122; i1++) {
nkey[0] = (char) i1;
for (int i2 = 65; i2 < 122; i2++) {
nkey[1] = (char) i2;
for (int i3 = 65; i3 < 122; i3++) {
nkey[2] = (char) i3;
for (int i4 = 65; i4 < 122; i4++) {
nkey[3] = (char) i4;
for (int i5 = 65; i5 < 122; i5++) {
nkey[4] = (char) i5;
for (int i6 = 65; i6 < 122; i6++) {
nkey[5] = (char) i6;
for (int i7 = 65; i7 < 122; i7++) {
nkey[6] = (char) i7;
for (int i8 = 65; i8 < 122; i8++) {
nkey[7] = (char) i8;
for (int i9 = 65; i9 < 122; i9++) {
nkey[8] = (char) i9;
for (int i10 = 65; i10 < 122; i10++) {
nkey[9] = (char) i10;
for (int i11 = 65; i11 < 122; i11++) {
nkey[10] = (char) i11;
for (int i12 = 65; i12 < 122; i12++) {
nkey[11] = (char) i12;
for (int i13 = 65; i13 < 122; i13++) {
nkey[12] = (char) i13;
for (int i14 = 65; i14 < 122; i14++) {
nkey[13] = (char) i14;
for (int i15 = 65; i15 < 122; i15++) {
nkey[14] = (char) i15;
for (int i16 = 65; i16 < 122; i16++) {
nkey[15] = (char) i16;
cntr++;
byte[] brutusCipher = Crack(
byteC(nkey),
byteT(plaintext));
for (int k = 0; k < brutusCipher.length; k++) {
brutus.append(brutusCipher[k]);
}
if (brutus
.toString()
.equals(cipher
.toString())) {
System.out
.println("found it");
System.out
.println("Key: "
+ Key);
System.out
.println("Brutus: "
+ brutus);
System.out
.println("i ran: "
+ cntr
+ "times");
long endTime = System
.nanoTime();
System.out
.println("time:"
+ (endTime - startTime)
+ " ns");
long endTime1 = System
.currentTimeMillis();
System.out
.println("Took "
+ (endTime1 - startTime1)
+ " ms");
return;
}
brutus.setLength(0);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
public static byte[] byteT(String s) {
return s.getBytes();
}
public static byte[] byteC(char[] s) {
StringBuilder temp = new StringBuilder();
for (int i = 0; i < s.length; i++) {
temp.append(s[i]);
}
Key = temp.toString();
return temp.toString().getBytes();
}
public static byte[] encrypt(byte[] key, byte[] plaintext) {
byte[] d = new byte[key.length];
System.out.println(key.length);
for (int i = 0; i < key.length; i++) {
d[i] = (byte) (key[i] ^ plaintext[i]);
}
return d;
}
public static byte[] Crack(byte[] key, byte[] plaintext) {
byte[] n = new byte[key.length];
for (int i = 0; i < key.length; i++) {
n[i] = (byte) (key[i] ^ plaintext[i]);
}
return n;
}
}
Here is my suggestion on how you can improve your code:
char[] nkey = new char[16];
for (int i =0 ;i<16;++i) {
nkey[i] = 65;
}
while (true) {
//... do the stuff you do in the inner of the cycle
int index = 15;
nkey[index]++;
while (index >= 0 && nkey[index] >= 122) {
nkey[index] = 65;
index--;
if (index < 0) {
break;
}
nkey[index]++;
}
}
You can imagine what I do as representing what you iterate upon as a number in base 122-65 and adding one to it.
You could create a class like this (not tested):
class IncrementableCharArray {
private final char[] array;
IncrementableCharArray(int size) {
array = new char[size];
Arrays.fill(array, 'A');
}
boolean increment() {
//here logic to increment the array
int index = 0;
while(index < array.length && array[index] == 'z') index++;
if (index == array.length) return false;
array[index]++;
return true;
}
char[] get() { return array; }
}
The performance won't be better but it will be a little bit more readable. And you can use it like this:
IncrementableCharArray array = new IncrementableCharArray(16);
while(array.increment()) {
char[] nkey = array.get();
//your test here
}

Exception in thread main ArrayIndexOutOfBoundsException

Can someone maybe help me with the following error. Exception in thread main java.lang.arrayindexoutofboundsexception : 3 at RowTrans.encrypt(Rowtrans.java:33)
at RowTrans.main(Rowtrans.java :7)
In my program I want to get a text. Put it in a matrix with 5 columns and determine the rows according to the length of the text. Then i want to change the column and row position so that the row gets the columns position and the column the row. And when a row does not contain 5 values I want to add the character Z in the empty spaces. Can anyone assist me on this error please.
Here is my code
import java.util.Scanner;
public class ColTrans {
public static void main(String[] args)
{
String ori = "This is my horse";
String enc = encrypt(ori);
System.out.println(enc);
// String dec = decrypt(enc);
// System.out.println(dec);
}
static String encrypt(String text)
{
String result = "";
text = text.toUpperCase();
int length = text.length();
int rows = length / 5;
char[][] b = new char[rows][5];
char[][] c = new char[5][rows];
char[] d = new char[length];
if ((length % 5) != 0)
rows = rows + 1;
int k = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < 5; j++)
{
if (k > length)
b[i][j] = 'Z';
else
{
d[k] = text.charAt(k);
b[i][j] = d[k];
}
k++;
}
for (int i = 0; i < 5; i++)
for (int j = 0; j < rows; j++)
{
c[i][j] = b[j][i];
result = result + c[i][j];
}
return result;
}
}
Here is the cause:
You are increamenting row variable by one, once you have defined the array.
Move following line before line char [][] b =new char[rows][5];
if ((length % 5) != 0)
rows = rows + 1;
There are 2 issues in your code. First move the mod part before the matrix instantiation :
if ((length % 5) != 0)
rows = rows + 1;
char [][] b =new char[rows][5];
[...]
Then change if ( k > length ) to if ( k >= length )
just change your code as following:
if ((length % 5) != 0)
rows = rows + 1;
char[][] b = new char[rows][5];
char[][] c = new char[5][rows];
char[] d = new char[length];
According to your description:
text = text.toUpperCase();
char[] b = text.toCharArray();
char[][] c = new char[b.length][5];
int bLen = 0;
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < 5; j++) {
if(bLen < b.length)
c[i][j] = b[bLen++];
else
c[i][j] = 'Z';
}
}
//change the column and row position
char[][]d = new char[c[0].length][c.length];
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[0].length; j++) {
d[i][j] = c[j][i];
}
}
Output: TI EZZZZZZZZZZZZHSHZZZZZZZZZZZZZI OZZZZZZZZZZZZZSMRZZZZZZZZZZZZZ YSZZZZZZZZZZZZZ

Categories