String key="this is stack Overflow i am implementing aes algorithm"; //plaintext
String binary=stringToBinary(key); //convertion from plain text to binarydigits
String res[]=split_at(binary,8); //split binary digits into 8 bits
int c=0,r=0;
for(int i=0;i<res.length;i++) //convert each 8 bits into hexadecimal
{
userKey[r][c]=binaryToHex(res[i]);
c++;
if(c==4)
{
c=0;
r++;
if(r==4)
break;
}
}
public static String[][] split_at(String str,int no)//method
{
int i=0;
int x=0;
int l=str.length();
String res[]=new String[(l/no)+1];
int f=0;
while(no<1){
res[f]=str.substring(i,no);
i=i+x;
no=no+x;
f++;
}
if(i<1){
res[f]=str.substring(i,1);
}
return res;
}
我试过这个。但我不知道它是否正确。我是不是这个正确的过程?如果没有,那么建议我如何将明文转换为十六进制。
答案 0 :(得分:1)
将字符串的每个字符转换为int
:
int character = (int) str.charAt(i);
然后获得int
:
String hexString = Integer.toHexString(character);
答案 1 :(得分:0)
获取每个字符,解析为整数并将其转换为十六进制
public static void main(String args[]) throws InterruptedException {
String key = "Hello world";// plaintext
byte[] x = key.getBytes();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < x.length; i++) {
int b = (int) x[i];
sb.append(Integer.toHexString(b));
}
System.out.println(sb.toString());
}