为了账号安全,请及时绑定邮箱和手机立即绑定

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 开发者encodingAESKey * @param clientId 开发者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 开发者token * @param encodingAesKey 开发者encodingAESKey * @param clientId 开发者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个字节的网络字节序 @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个字节的网络字节序 @param orderBytes 字节码 @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位随机字符串,网络字节序和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个赞

发的问题格式清晰,能给别人一种看起来就很舒服的感觉是对问题的最起码尊重,你这样问打眼一看就给关了,没人会回复你的。

查看完整回答
反对 回复 2018-12-16
?
烙印99

TA贡献1829条经验 获得超13个赞

受教?不知道怎样提才比较合适

查看完整回答
反对 回复 2018-12-16
?
慕容森

TA贡献1853条经验 获得超18个赞

已经解决啦!不过还是感谢?

查看完整回答
反对 回复 2018-12-16
  • 5 回答
  • 0 关注
  • 592 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信