第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在Java中加密字符串

如何在Java中加密字符串

慕哥6287543 2019-07-05 12:24:15
如何在Java中加密字符串我需要的是加密字符串,這將顯示在2D條形碼(PDF-417),所以當(dāng)有人有一個想法來掃描它將沒有任何可讀性。其他所需經(jīng)費:不應(yīng)該很復(fù)雜它不應(yīng)由RSA、PKI基礎(chǔ)設(shè)施、密鑰對等組成。它必須足夠簡單,以擺脫人們四處窺探,并易于解密,其他公司有興趣獲得這些數(shù)據(jù)。他們打電話給我們,我們告訴他們標(biāo)準(zhǔn)或給他們一些簡單的密鑰,然后可以用來解密。也許這些公司可以使用不同的技術(shù),所以最好堅持一些標(biāo)準(zhǔn),而這些標(biāo)準(zhǔn)并不與某些特殊的平臺或技術(shù)掛鉤。你有什么建議?是否有一些Java類正在執(zhí)行encrypt() & decrypt()在達(dá)到高安全標(biāo)準(zhǔn)的過程中沒有太多的復(fù)雜性?
查看完整描述

3 回答

?
喵喔喔

TA貢獻(xiàn)1735條經(jīng)驗 獲得超5個贊

我建議使用一些廣泛可用的標(biāo)準(zhǔn)對稱密碼,如DES3 DES俄歇..雖然這不是最安全的算法,但是有大量的實現(xiàn),您只需要將密鑰給應(yīng)該解密條形碼中信息的任何人。密碼你想在這里工作。

讓我們假設(shè)要加密的字節(jié)在

byte[] input;

接下來,你需要鑰匙初始化向量字節(jié)

byte[] keyBytes;byte[] ivBytes;

現(xiàn)在,您可以為您選擇的算法初始化密碼:

// wrap key data in Key/IV specs to pass to cipherSecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);// create the cipher with the algorithm you choose
// see javadoc for Cipher class for more info, e.g.Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

加密應(yīng)該是這樣的:

cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);byte[] encrypted= new byte[cipher.getOutputSize(input.length)];
int enc_len = cipher.update(input, 0, input.length, encrypted, 0);enc_len += cipher.doFinal(encrypted, enc_len);

解密如下:

cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);byte[] decrypted = new byte[cipher.getOutputSize(enc_len)];
int dec_len = cipher.update(encrypted, 0, enc_len, decrypted, 0);dec_len += cipher.doFinal(decrypted, dec_len);


查看完整回答
反對 回復(fù) 2019-07-05
?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗 獲得超8個贊

我正在使用Sun的Base 64編碼器/解碼器,它將在Sun的JRE中找到,以避免lib中的另一個JAR。從使用OpenJDK或其他JRE的角度來看,這是很危險的。除此之外,我還應(yīng)該考慮在編碼器/解碼器中使用ApacheCommonslib嗎?

public class EncryptUtils {
    public static final String DEFAULT_ENCODING = "UTF-8"; 
    static BASE64Encoder enc = new BASE64Encoder();
    static BASE64Decoder dec = new BASE64Decoder();

    public static String base64encode(String text) {
        try {
            return enc.encode(text.getBytes(DEFAULT_ENCODING));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }//base64encode

    public static String base64decode(String text) {
        try {
            return new String(dec.decodeBuffer(text), DEFAULT_ENCODING);
        } catch (IOException e) {
            return null;
        }
    }//base64decode

    public static void main(String[] args) {
        String txt = "some text to be encrypted";
        String key = "key phrase used for XOR-ing";
        System.out.println(txt + " XOR-ed to: " + (txt = xorMessage(txt, key)));

        String encoded = base64encode(txt);       
        System.out.println(" is encoded to: " + encoded + " and that is decoding to: " + (txt = base64decode(encoded)));
        System.out.print("XOR-ing back to original: " + xorMessage(txt, key));
    }

    public static String xorMessage(String message, String key) {
        try {
            if (message == null || key == null) return null;

            char[] keys = key.toCharArray();
            char[] mesg = message.toCharArray();

            int ml = mesg.length;
            int kl = keys.length;
            char[] newmsg = new char[ml];

            for (int i = 0; i < ml; i++) {
                newmsg[i] = (char)(mesg[i] ^ keys[i % kl]);
            }//for i

            return new String(newmsg);
        } catch (Exception e) {
            return null;
        }
    }//xorMessage}//class


查看完整回答
反對 回復(fù) 2019-07-05
  • 3 回答
  • 0 關(guān)注
  • 1580 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號