Skip to content

Commit

Permalink
feat: add randomCipheringData
Browse files Browse the repository at this point in the history
  • Loading branch information
lotharking committed Oct 11, 2024
1 parent c378149 commit ebde40b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
package io.twentysixty.sa.client.model.message;

import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
public class Ciphering implements Serializable {

private static final long serialVersionUID = -8660299956191649637L;
private String algorithm;
private Parameters parameters;

public String getAlgorithm() {
return algorithm;
}

public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}

public Parameters getParameters() {
return parameters;
}

public void setParameters(Parameters parameters) {
this.parameters = parameters;
}

@Override
public String toString() {
return "Ciphering [algorithm=" + algorithm + ", parameters=" + parameters + "]";
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
package io.twentysixty.sa.client.model.message;

import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
public class Parameters implements Serializable {

private static final long serialVersionUID = -3591319454008944749L;
private String key;
private String iv;

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getIv() {
return iv;
}

public void setIv(String iv) {
this.iv = iv;
}

@Override
public String toString() {
return "Parameters [key=" + key + ", iv=" + iv + "]";
}
}
21 changes: 21 additions & 0 deletions src/main/java/io/twentysixty/sa/client/util/Aes256cbc.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.twentysixty.sa.client.util;

import io.twentysixty.sa.client.model.message.Ciphering;
import io.twentysixty.sa.client.model.message.Parameters;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.jboss.logging.Logger;
Expand Down Expand Up @@ -40,4 +44,21 @@ public static byte[] decrypt(String key, String iv, byte[] encrypted) throws Exc
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}

public static Ciphering randomCipheringData() throws Exception {
Ciphering c = new Ciphering();
Parameters p = new Parameters();
p.setKey(randomKey(32));
p.setIv(randomKey(16));
c.setAlgorithm(cI);
c.setParameters(p);
return c;
}

private static String randomKey(int size) throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(alg);
keyGen.init(size * 8);
SecretKey secretKey = keyGen.generateKey();
return ISOUtil.dumpString(secretKey.getEncoded());
}
}

0 comments on commit ebde40b

Please sign in to comment.