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

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

java代碼翻譯成C#

java代碼翻譯成C#

慕桂英3389331 2018-12-06 18:16:56
private static Charset CHARSET = Charset.forName("utf-8"); private Base64 base64 = new Base64(); private byte[] aesKey; private String token; private String clientId; /** * 構造函數 @param encodingAesKey 開發(fā)者encodingAESKey * @param clientId 開發(fā)者clientId @throws TpException 異常錯誤信息 */ public AesEncryptUtil(String encodingAesKey, String clientId) throws TpException { int encodingAesKeyLength = 43; if (encodingAesKey.length() != encodingAesKeyLength) { throw new TpException(TpErrorEnum.ILLEGAL_AES_KEY_ERROR); } this.clientId = clientId; aesKey = Base64.decodeBase64(encodingAesKey + "="); } /** * 構造函數 @param token 開發(fā)者token * @param encodingAesKey 開發(fā)者encodingAESKey * @param clientId 開發(fā)者clientId @throws TpException 異常錯誤信息 */ public AesEncryptUtil(String token, String encodingAesKey, String clientId) throws TpException { int encodingAesKeyLength = 43; if (encodingAesKey.length() != encodingAesKeyLength) { throw new TpException(TpErrorEnum.ILLEGAL_AES_KEY_ERROR); } this.token = token; this.clientId = clientId; aesKey = Base64.decodeBase64(encodingAesKey + "="); } /** * 生成4個字節(jié)的網絡字節(jié)序 @param sourceNumber 文本長度 @return orderBytes */ private byte[] getNetworkBytesOrder(int sourceNumber) { byte[] orderBytes = new byte[4]; orderBytes[3] = (byte) (sourceNumber & 0xFF); orderBytes[2] = (byte) (sourceNumber >> 8 & 0xFF); orderBytes[1] = (byte) (sourceNumber >> 16 & 0xFF); orderBytes[0] = (byte) (sourceNumber >> 24 & 0xFF); return orderBytes; } /** * 還原4個字節(jié)的網絡字節(jié)序 @param orderBytes 字節(jié)碼 @return sourceNumber */ private int recoverNetworkBytesOrder(byte[] orderBytes) { int sourceNumber = 0; int length = 4; int number = 8; for (int i = 0; i < length; i++) { sourceNumber <<= number; sourceNumber |= orderBytes[i] & 0xff; } return sourceNumber; } /** * 隨機生成16位字符串 @return 隨機串 */ public String getRandomStr() { String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; Random random = new Random(); StringBuilder stringBuild = new StringBuilder(); int randStrLength = 16; for (int i = 0; i < randStrLength; i++) { int number = random.nextInt(base.length()); stringBuild.append(base.charAt(number)); } return stringBuild.toString(); } /** * 對明文進行加密 @param text 待加密明文 @return 加密后base64編碼的字符串 @throws TpException 異常錯誤信息 */ public String encrypt(String randomStr, String text) throws TpException { ByteGroup byteCollector = new ByteGroup(); byte[] randomStrBytes = randomStr.getBytes(CHARSET); byte[] textBytes = text.getBytes(CHARSET); byte[] networkBytesOrder = getNetworkBytesOrder(textBytes.length); byte[] clientIdBytes = clientId.getBytes(CHARSET); byteCollector.addBytes(randomStrBytes); byteCollector.addBytes(networkBytesOrder); byteCollector.addBytes(textBytes); byteCollector.addBytes(clientIdBytes); byte[] padBytes = PKCS7Encoder.encode(byteCollector.size()); byteCollector.addBytes(padBytes); byte[] unencrypted = byteCollector.toBytes(); try { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES"); IvParameterSpec iv = new IvParameterSpec(aesKey, 0, 16); cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); byte[] encrypted = cipher.doFinal(unencrypted); return base64.encodeToString(encrypted); } catch (Exception e) { throw new TpException(TpErrorEnum.ENCRYPT_AES_ERROR); } } /** * 對密文進行解密 @param text 需要解密的密文 @return 解密得到的明文 @throws TpException 異常錯誤信息 */ public String decrypt(String text) throws TpException { byte[] original; try { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES"); IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16)); cipher.init(Cipher.DECRYPT_MODE, keySpec, iv); byte[] encrypted = Base64.decodeBase64(text); original = cipher.doFinal(encrypted); } catch (Exception e) { throw new TpException(TpErrorEnum.DECRYPT_AES_ERROR); } String xmlContent; String fromClientId; try { // 去除補位字符 byte[] bytes = PKCS7Encoder.decode(original); // 分離16位隨機字符串,網絡字節(jié)序和ClientId byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20); int xmlLength = recoverNetworkBytesOrder(networkOrder); xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), CHARSET); fromClientId = new String(Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET); } catch (Exception e) { throw new TpException(TpErrorEnum.ILLEGAL_BUFFER_ERROR); } return xmlContent; } /** * 加密機密demo * @param args */ public static void main(String[] args) { String clientId = "1172"; String key = "ql1234fnhsjndyskemdlsowerfvmjhxswedfrtgbqwe"; AesEncryptUtil aesEncryptUtil = new AesEncryptUtil(key, clientId); String enData = aesEncryptUtil.encrypt(aesEncryptUtil.getRandomStr(), "test"); String deData = aesEncryptUtil.decrypt(data); }
查看完整描述

5 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

AES解密

查看完整回答
反對 回復 2018-12-16
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

發(fā)的問題格式清晰,能給別人一種看起來就很舒服的感覺是對問題的最起碼尊重,你這樣問打眼一看就給關了,沒人會回復你的。

查看完整回答
反對 回復 2018-12-16
?
烙印99

TA貢獻1829條經驗 獲得超13個贊

受教?不知道怎樣提才比較合適

查看完整回答
反對 回復 2018-12-16
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

已經解決啦!不過還是感謝?

查看完整回答
反對 回復 2018-12-16
  • 5 回答
  • 0 關注
  • 610 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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