本文整理汇总了Java中java.security.SecureRandom.setSeed方法的典型用法代码示例。如果您正苦于以下问题:Java SecureRandom.setSeed方法的具体用法?Java SecureRandom.setSeed怎么用?Java SecureRandom.setSeed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.SecureRandom
的用法示例。
在下文中一共展示了SecureRandom.setSeed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: secureRandomHashing
import java.security.SecureRandom; //导入方法依赖的package包/类
public byte[] secureRandomHashing(long blockNumber) {
final SecureRandom secureRandom;
try {
secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(seedForThisBlock(blockNumber));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
final byte[] ret = new byte[EncConstants.BLOCKSIZE];
secureRandom.nextBytes(ret);
//
// secureRandom.longs(EncConstants.BLOCKSIZE / 8).forEach(value -> {
// });
return ret;
}
示例2: AesDecrypt
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* aes解密-128位
*/
public static String AesDecrypt(String encryptContent, String password) {
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
keyGen.init(128, secureRandom);
SecretKey secretKey = keyGen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(hex2Bytes(encryptContent)));
} catch (Exception e) {
logger.error("AesDecrypt exception", e);
return null;
}
}
示例3: createSecureRandom
import java.security.SecureRandom; //导入方法依赖的package包/类
private static SecureRandom createSecureRandom()
{
/*
* We use our threaded seed generator to generate a good random seed. If the user has a
* better random seed, he should use the constructor with a SecureRandom.
*/
ThreadedSeedGenerator tsg = new ThreadedSeedGenerator();
SecureRandom random = new SecureRandom();
/*
* Hopefully, 20 bytes in fast mode are good enough.
*/
random.setSeed(tsg.generateSeed(20, true));
return random;
}
示例4: main
import java.security.SecureRandom; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(0);
sr.nextInt();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
new ObjectOutputStream(bout).writeObject(sr);
SecureRandom sr2 = (SecureRandom) new ObjectInputStream(
new ByteArrayInputStream(bout.toByteArray())).readObject();
int i1 = sr.nextInt();
sr2.setSeed(1);
int i2 = sr2.nextInt();
if (i1 == i2) {
throw new Exception("output should not be the same");
}
}
示例5: main
import java.security.SecureRandom; //导入方法依赖的package包/类
public void main(Provider p) throws Exception {
// Skip this test for providers not found by java.security.Security
if (Security.getProvider(p.getName()) != p) {
System.out.println("Skip test for provider " + p.getName());
return;
}
SecureRandom r;
try {
r = SecureRandom.getInstance("PKCS11", p);
System.out.println("SecureRandom instance " + r);
} catch (NoSuchAlgorithmException e) {
System.out.println("Provider " + p +
" does not support SecureRandom, skipping");
e.printStackTrace();
return;
}
r.setSeed(System.currentTimeMillis());
byte[] buf = new byte[16];
byte[] ser = toByteArray(r);
System.out.println("Serialized Len = " + ser.length);
SecureRandom r2 = fromByteArray(ser);
System.out.println("Deserialized into " + r2);
r2.nextBytes(buf);
System.out.println("Done");
}
示例6: main
import java.security.SecureRandom; //导入方法依赖的package包/类
@Override
public void main(Provider p) throws Exception {
SecureRandom random;
try {
random = SecureRandom.getInstance("PKCS11");
} catch (NoSuchAlgorithmException e) {
System.out.println("Provider " + p + " does not support SecureRandom, skipping");
e.printStackTrace();
return;
}
byte[] b = new byte[32];
random.nextBytes(b);
System.out.println(toString(b));
random.setSeed(b);
random.nextBytes(b);
System.out.println(toString(b));
System.out.println("OK");
}
示例7: generateSalt
import java.security.SecureRandom; //导入方法依赖的package包/类
private String generateSalt() {
byte[] salt = new byte[KEY_LENGTH];
try {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(System.currentTimeMillis());
sr.nextBytes(salt);
return Arrays.toString(salt);
} catch (Exception e) {
salt = DEFAULT_PASSWORD_SALT.getBytes();
}
return Base64.encodeToString(salt, Base64.DEFAULT);
}
示例8: encrypt
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* 加密
*
* @param content �?要加密的内容
* @return
*/
public static String encrypt(String content) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance(AES);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(KEY.getBytes());
kgen.init(AES_LENGTH, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
Cipher cipher = Cipher.getInstance(AES);// 创建密码�?
byte[] byteContent = content.getBytes(ENCODING_UTF8);
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始�?
byte[] result = cipher.doFinal(byteContent);
return new BASE64Encoder().encode(result); // 加密
}
示例9: randomBytes
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
*
* @param lgt length wanted
* @return secure random bytes of length lgt
*/
public static byte[] randomBytes(int lgt) {
SecureRandom rd = new SecureRandom();
rd.setSeed(System.nanoTime());
byte[] bt = new byte[lgt];
rd.nextBytes(bt);
return bt;
}
示例10: generate
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Generates a cryptographically secure API key of the specified length.
* @param chars the length of the API key to generate
* @return a String representation of the API key
*/
public static String generate(int chars) {
final SecureRandom secureRandom = new SecureRandom();
final char[] buff = new char[chars];
for (int i = 0; i < chars; ++i) {
if ((i % 10) == 0) {
secureRandom.setSeed(secureRandom.nextLong());
}
buff[i] = VALID_CHARACTERS[secureRandom.nextInt(VALID_CHARACTERS.length)];
}
return new String(buff);
}
示例11: getRawKey
import java.security.SecureRandom; //导入方法依赖的package包/类
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
// SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法
SecureRandom sr;
if (android.os.Build.VERSION.SDK_INT >= 17) {
sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
} else {
sr = SecureRandom.getInstance("SHA1PRNG");
}
sr.setSeed(seed);
kgen.init(256, sr); //256 bits or 128 bits,192bits
SecretKey s_key = kgen.generateKey();
byte[] raw = s_key.getEncoded();
return raw;
}
示例12: AesDecryptByBytes
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* AES解密
* @param encryptBytes 待解密的byte[]
* @param decryptKey 解密密钥
* @return 解密后的
* @throws Exception
*/
public static byte[] AesDecryptByBytes(byte[] encryptBytes,byte[] decryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(decryptKey);
kgen.init(128,secureRandom);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return decryptBytes;
}
示例13: AesEncryptToBytes
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* AES加密
* @param content 待加密的内容
* @param encryptKey 加密密钥
* @return 加密后的byte[]
* @throws Exception
*/
public static byte[] AesEncryptToBytes(byte[] content,byte[] encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(encryptKey);
kgen.init(128,secureRandom);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
return cipher.doFinal(content);
}
示例14: testEncrypt
import java.security.SecureRandom; //导入方法依赖的package包/类
/**
* Tests encryption / decryption by attempting to encrypt and decrypt the bytes forming this class definition and
* comparing it with the unencrypted bytes.
*
* @throws Exception
* an exception
*/
public void testEncrypt() throws Exception
{
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
final SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
final byte[] seed = getClass().getName().getBytes("UTF-8");
random.setSeed(seed);
keyGen.initialize(1024, random);
final KeyPair pair = keyGen.generateKeyPair();
final ByteArrayOutputStream buff = new ByteArrayOutputStream(2048);
final OutputStream encrypting = new EncryptingOutputStream(buff, pair.getPublic(), random);
final ByteArrayOutputStream plainText1 = new ByteArrayOutputStream(2048);
final InputStream in = getClass().getResourceAsStream(getClass().getSimpleName() + ".class");
final byte[] inbuff = new byte[17];
int bytesRead;
while ((bytesRead = in.read(inbuff)) != -1)
{
encrypting.write(inbuff, 0, bytesRead);
plainText1.write(inbuff, 0, bytesRead);
}
in.close();
encrypting.close();
plainText1.close();
final InputStream decrypting = new DecryptingInputStream(new ByteArrayInputStream(buff.toByteArray()), pair
.getPrivate());
final ByteArrayOutputStream plainText2 = new ByteArrayOutputStream(2048);
while ((bytesRead = decrypting.read(inbuff)) != -1)
{
plainText2.write(inbuff, 0, bytesRead);
}
assertTrue(Arrays.equals(plainText1.toByteArray(), plainText2.toByteArray()));
}
示例15: getSecretKey
import java.security.SecureRandom; //导入方法依赖的package包/类
public static SecretKeySpec getSecretKey(String secretKey) throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance(ENCRYPT_TYPE);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(secretKey.getBytes());
kgen.init(AES_DEFAULT_KEY_SIZE, random);
SecretKey sk = kgen.generateKey();
byte[] enCodeFormat = sk.getEncoded();
return new SecretKeySpec(enCodeFormat, ENCRYPT_TYPE);
}