javaandroidencryptionsha

SPECK Algorithm encryption


I Want to Encrypte a String with SPECK Algorithm.we have a password and we get its hash get a 128 bit key from that hash - hash algorithm is SHA256. in decryption i get invallid String - block size : 128 and key size 128 , round 32

public static void SpeckEnc(byte[] sha,String message){
byte[] data=message.getBytes();
byte[] newdata;
byte[] lastblock=new byte[16];
byte[] key=new byte[16];
int tool=data.length/16;
final int baghi=data.length%16;

for(int I = 0; I < 16; I++)
    key[I] = (byte) (sha[I] ^ sha[I + 16]);

if(baghi!=0){
    int toollast=data.length - tool*16;
    for(int ba=0;ba<16;ba++){
        if(ba<toollast)
            lastblock[ba]=data[tool*16+ba];
        else
            lastblock[ba]=0;
        }
    tool++;
    newdata=new byte[tool*16];
    System.arraycopy(data,0,newdata,0,(tool-1)*16);
    System.arraycopy(lastblock,0,newdata,(tool-1)*16,16);
}else
    newdata=data;
byte[] c=new byte[newdata.length];
byte[] k1 = Arrays.copyOfRange(key, 0, 8);
byte[] k2 = Arrays.copyOfRange(key, 8, 16);
byte[] pofd; 
for(int i=0;i<tool;i++){
    pofd=Arrays.copyOfRange(newdata,i*16,i*16+16);
    System.out.print(pofd[0]+" ");
    pofd=enc(pofd,k1,k2);
    System.out.print(pofd[0]+" |\n");
    System.arraycopy( pofd,0,c,i*16,16);
}
String encoded = Base64.getEncoder().encodeToString(c);}

main encryption func:

public static byte[] enc(byte[] piecedata,byte[] k1,byte[] k2){
byte[] result=new byte[16];
byte[] pt1=new byte[8];
byte[] pt2=new byte[8];
for(int si = 0; si < 32; si++) {
     k1=generateKey(k1,k2,si);
     k2=generatekey2(k1,k2);
    pt1=circularBitShiftingRight(pt1,8);
    pt1=plus(pt1,pt2);
    for(int i=0;i<8;i++)
        pt1[i]=(byte) (pt1[i]^k2[i]);
    pt2=circularBitShiftingLeft(pt2,3);
    for(int i=0;i<8;i++)
        pt2[i]=(byte) (pt1[i]^pt2[i]);
   }    
System.arraycopy(pt1,0,result,0,8);
System.arraycopy(pt2,0,result,8,8);
return result;
} 

decryption section :

public static void SpeckDec(byte[] sha,String base64message){
byte[] decodedString = Base64.getDecoder().decode(base64message.getBytes());
byte[] data=new byte[decodedString.length];
byte[] keys=new byte[256];
byte[] key=new byte[16];
byte[] pofd,p;

final int tool=decodedString.length/16;
for(int I = 0; I < 16; I++)
    key[I] = (byte) (sha[I] ^ sha[I + 16]);
byte[] k1 = Arrays.copyOfRange(key, 0, 8);
byte[] k2 = Arrays.copyOfRange(key, 8, 16);
for (int i=0;i<32;i++)
{
    k1=generateKey(k1,k2,i);
    k2=generatekey2(k1,k2);
    System.arraycopy(k2,0,keys,i*8,8);
}    
for(int i=0;i<tool ;i++){
    pofd=Arrays.copyOfRange(decodedString,i*16,i*16+16);
    p=dec(pofd,keys);
    System.arraycopy(p, 0, data, i*16, 16);
}}

main decryption func:

public static byte[] dec(byte[] pieceofdata,byte[] keys){
byte[] result=new byte[16];
byte[] key;
byte[] ct1=new byte[8];
byte[] ct2=new byte[8];
int m;
for(int i=0;i<32;i++){
    m=256-((i+1)*8);
      key=Arrays.copyOfRange(keys,m,m+8);
    for(int j=0;j<8;j++)
        ct2[j]=(byte) (ct1[j]^ct2[j]);
    ct2=circularBitShiftingRight(ct2,3);
    for(int j=0;j<8;j++)
        ct1[j]=(byte) (ct1[j]^key[j]);
    for(int j=0;j<8;j++)
        ct1[j]=(byte) (ct1[j]-ct2[j]);
    ct1=circularBitShiftingLeft(ct1,8); 
}   
System.arraycopy(ct1, 0, result, 0, 8);
System.arraycopy(ct2, 0, result, 8, 8);
return result;}

generate key func:

public static byte[] generateKey(byte[] k1,byte[] k2,int x){
byte[] a=circularBitShiftingRight(k1,8);
a=plus(a,k2);
for(int i=0;i<8;i++)
    a[i]=(byte) (a[i]^x);
return a;}


public static byte[] generatekey2(byte[] k1,byte[] k2){
 byte[] t =circularBitShiftingLeft(k2,3);
  for(int i=0;i<8;i++)
    t[i]=(byte) (t[i]^k1[i]);
  return t;}

plus func :

public static byte[] plus(byte[] a,byte[] b){
    byte[] s=new byte[8];
    for(int i=0;i<8;i++){
        s[i]=(byte) (a[i]+b[i]);
    }
return s;}

you can read details of SPECK algorithm in :

https://en.wikipedia.org/wiki/Speck_(cipher)


Solution

  • i forgot initial pt1 pt2 in enc function and ct1 ct2 in dec function

    in enc func:

    public static byte[] enc(byte[] piecedata,byte[] k1,byte[] k2){
    byte[] result=new byte[16];
    byte[] pt1,pt2;
    pt1=Arrays.copyOfRange(piecedata, 0, 8);
    pt2=Arrays.copyOfRange(piecedata, 8, 16);
    for(int si = 0; si < 32; si++) {
         k1=generateKey(k1,k2,si);
         k2=generatekey2(k1,k2);
        pt1=circularBitShiftingRight(pt1,8);
        pt1=plus(pt1,pt2);
        for(int i=0;i<8;i++)
            pt1[i]=(byte) (pt1[i]^k2[i]);
        pt2=circularBitShiftingLeft(pt2,3);
        for(int i=0;i<8;i++)
            pt2[i]=(byte) (pt1[i]^pt2[i]);
       }    
    System.arraycopy(pt1,0,result,0,8);
    System.arraycopy(pt2,0,result,8,8);
    return result;
    } 
    

    and in dec func :

    public static byte[] dec(byte[] pieceofdata,byte[] keys){
    byte[] result=new byte[16];
    byte[] key;
    byte[] ct1,ct2;
    int m;
    ct1=Arrays.copyOfRange(pieceofdata, 0, 8);
    ct2=Arrays.copyOfRange(pieceofdata, 8, 16);
    for(int i=0;i<32;i++){
        m=256-((i+1)*8);
          key=Arrays.copyOfRange(keys,m,m+8);
        for(int j=0;j<8;j++)
            ct2[j]=(byte) (ct1[j]^ct2[j]);
        ct2=circularBitShiftingRight(ct2,3);
        for(int j=0;j<8;j++)
            ct1[j]=(byte) (ct1[j]^key[j]);
        for(int j=0;j<8;j++)
            ct1[j]=(byte) (ct1[j]-ct2[j]);
        ct1=circularBitShiftingLeft(ct1,8); 
    }   
    System.arraycopy(ct1, 0, result, 0, 8);
    System.arraycopy(ct2, 0, result, 8, 8);
    return result;}