Search in sources :

Example 81 with KeyGenerator

use of javax.crypto.KeyGenerator in project openzaly by akaxincom.

the class AESCrypto method generateTSKey.

/**
 * 随机生成AES加密解密KEY
 *
 * @return
 */
public static byte[] generateTSKey() {
    try {
        KeyGenerator kg = KeyGenerator.getInstance("AES");
        kg.init(128);
        SecretKey secretKey = kg.generateKey();
        return secretKey.getEncoded();
    } catch (Exception e) {
        logger.error("generate ts key error", e);
    }
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator)

Example 82 with KeyGenerator

use of javax.crypto.KeyGenerator in project openzaly by akaxincom.

the class AESCrypto method generateTSKey.

/**
 * 通过key生成AES加密解密key
 *
 * @param key
 * @return
 */
public static byte[] generateTSKey(String key) {
    try {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(key.getBytes());
        kgen.init(128, secureRandom);
        SecretKey secretKey = kgen.generateKey();
        return secretKey.getEncoded();
    } catch (Exception e) {
        logger.error("generate ts key error by key=" + key, e);
    }
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator)

Example 83 with KeyGenerator

use of javax.crypto.KeyGenerator in project openzaly by akaxincom.

the class AESCrypto method decrypt.

public static byte[] decrypt(byte[] content, String password) throws Exception {
    // 创建AES的Key生产者
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(password.getBytes()));
    // 根据用户密码,生成一个密钥
    SecretKey secretKey = kgen.generateKey();
    // 返回基本编码格式的密钥
    byte[] enCodeFormat = secretKey.getEncoded();
    // 转换为AES专用密钥
    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
    // 创建密码器
    Cipher cipher = Cipher.getInstance("AES");
    // 初始化为解密模式的密码器
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] result = cipher.doFinal(content);
    // 明文
    return result;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator)

Example 84 with KeyGenerator

use of javax.crypto.KeyGenerator in project Signal-Android by signalapp.

the class MasterSecretUtil method generateEncryptionSecret.

private static byte[] generateEncryptionSecret() {
    try {
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        generator.init(128);
        SecretKey key = generator.generateKey();
        return key.getEncoded();
    } catch (NoSuchAlgorithmException ex) {
        Log.w("keyutil", ex);
        return null;
    }
}
Also used : SecretKey(javax.crypto.SecretKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyGenerator(javax.crypto.KeyGenerator)

Example 85 with KeyGenerator

use of javax.crypto.KeyGenerator in project faf-java-server by FAForever.

the class UniqueIdServiceTest method toUid.

/**
 * This implements the client-side, legacy UID generation.
 */
private static String toUid(String string) throws IOException, NoSuchAlgorithmException, InvalidCipherTextException {
    // Step 1: Load public key
    AsymmetricKeyParameter publicKey = loadPublicKey();
    // Step 2: Generate 16 bytes initialization vector, encode base 64
    byte[] initVector = new byte[16];
    new SecureRandom().nextBytes(initVector);
    byte[] base64InitVector = Base64.getEncoder().encode(initVector);
    // Step 3: Generate AES key 16 bytes, encrypt it, encode base 64
    KeyGenerator aesKeyGen = KeyGenerator.getInstance("AES");
    aesKeyGen.init(128);
    SecretKey aesKey = aesKeyGen.generateKey();
    byte[] rsaEncryptedAesKey = rsaEncrypt(aesKey.getEncoded(), publicKey);
    byte[] rsaEncryptedBsa64EncodedAesKey = Base64.getEncoder().encode(rsaEncryptedAesKey);
    // Step 4: AES-encrypt the json string, encode base 64
    // Prefix the JSON string with char '2' to indicate the new uid data format for the server
    String jsonString = "2" + string;
    // Insert trailing bytes to make len(json_string) a multiple of 16
    int jsonLength = jsonString.length();
    int trailLength = ((((jsonLength / 16) + 1) * 16) - jsonLength);
    // Because someone thought manual padding would be necessary
    jsonString = jsonString + new String(new char[trailLength]).replace("\0", "x");
    byte[] encryptedString = aesEncrypt(initVector, jsonString.getBytes(UTF_8), aesKey.getEncoded());
    byte[] encryptedStringBase64 = Base64.getEncoder().encode(encryptedString);
    // Step 5: put message together
    byte[] messageBytes = Bytes.concat(new byte[] { (byte) trailLength }, base64InitVector, encryptedStringBase64, rsaEncryptedBsa64EncodedAesKey);
    return Base64.getEncoder().encodeToString(messageBytes);
}
Also used : SecretKey(javax.crypto.SecretKey) AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) SecureRandom(java.security.SecureRandom) Matchers.anyString(org.mockito.Matchers.anyString) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

KeyGenerator (javax.crypto.KeyGenerator)464 SecretKey (javax.crypto.SecretKey)343 Test (org.junit.Test)106 ArrayList (java.util.ArrayList)104 SecureRandom (java.security.SecureRandom)99 Document (org.w3c.dom.Document)98 InputStream (java.io.InputStream)95 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)93 ByteArrayOutputStream (java.io.ByteArrayOutputStream)87 NodeList (org.w3c.dom.NodeList)82 Cipher (javax.crypto.Cipher)79 ByteArrayInputStream (java.io.ByteArrayInputStream)75 XMLStreamReader (javax.xml.stream.XMLStreamReader)68 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)68 DocumentBuilder (javax.xml.parsers.DocumentBuilder)62 Key (java.security.Key)58 QName (javax.xml.namespace.QName)47 IOException (java.io.IOException)45 SecurePart (org.apache.xml.security.stax.ext.SecurePart)40 SecretKeySpec (javax.crypto.spec.SecretKeySpec)39