亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Java 中使用 SecureRandom

在 Java 中使用 SecureRandom

米脂 2022-06-30 18:26:46
我試圖在 Java 中導入 SecureRamdom 但是import java.security.SecureRandom;不工作。我在 Eclipse 中使用 Java SE 8。有誰知道如何導入它?
查看完整描述

2 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

當然,這是可能的。請看下面的代碼:


import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.util.Arrays;

import java.util.logging.Level;

import java.util.logging.Logger;


/**

 *

 * @author Momir Sarac

 */

public class SecureRandomExample {


    public static void main(String[] args) {


        try {

            // obtain a strong SecureRandom implementation from securerandom.strongAlgorithms property of java.security.Security

            // class

            SecureRandom secureRandom = SecureRandom.getInstanceStrong();

            // print the provided and algorithm obtained for this secureRandom 

            System.out.println("" + secureRandom.getProvider() + "\n" + secureRandom.getAlgorithm());

            //generate 16-long seed bytes

            //generate a given number of seed bytes (to seed other random number generators, for example):

            byte[] bytes = secureRandom.generateSeed(16);

            //print obtained bytes as string from array 

            System.out.println(Arrays.toString(bytes));

            //to get random bytes, a caller simply passes an array of any length, which is then filled with random bytes:

            secureRandom.nextBytes(bytes);

            //print obtained bytes as string from array 

            System.out.println(Arrays.toString(bytes));


        } catch (NoSuchAlgorithmException ex) {

            Logger.getLogger(SecureRandomExample.class.getName()).log(Level.SEVERE, null, ex);

        }

    }


}


查看完整回答
反對 回復 2022-06-30
?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

事實證明它確實有效。出于某種原因,即使它有效,eclipse 也只是將其突出顯示為錯誤。



查看完整回答
反對 回復 2022-06-30
  • 2 回答
  • 0 關注
  • 246 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號