首页新闻找找看学习计划

java代码翻译成C#

-4
悬赏园豆:50 [已解决问题] 解决于 2018-11-26 14:24

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);
}

知秋一枼的主页 知秋一枼 | 初学一级 | 园豆:159
提问于:2018-11-21 14:19
< >
分享
最佳答案
0

AES解密

知秋一枼 | 初学一级 |园豆:159 | 2018-11-22 14:10
其他回答(3)
1

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

徒然喜欢你 | 园豆:1599 (小虾三级) | 2018-11-21 16:36

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

支持(0) 反对(0) 知秋一枼 | 园豆:159 (初学一级) | 2018-11-21 16:40
0

都什么年代了,还有要翻译代码的...知道代码是干啥的,然后去GitHub里翻啊...

不就是一个AES加密嘛...给你个全套的

其中包括MD5,RSA,DES,SHA,AES,RC4。自取需要的部分。

各种加密方法

需要注意一点,由于使用的都是base64编码。而base64在网络环境下,尤其是作为URL一部分的时候,会与一些关键字诸如+/=冲突,所以提供了 EscapeBase64SpeChar  RecoveryBase64SpeChar 两个方法,来去掉关键字。当然,如果仅把加密后的数据当作字符串数据传递,那么就可以不使用这两个方法。

收获园豆:50
写代码的相声演员 | 园豆:565 (小虾三级) | 2018-11-26 13:45

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

支持(0) 反对(0) 知秋一枼 | 园豆:159 (初学一级) | 2018-11-26 14:23
0

都是编程语言java可以调C#,C#也能调Java的。ikvm

Jachs | 园豆:264 (菜鸟二级) | 2019-07-04 10:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册