yejianliang123456
yejianliang123456
2016-11-30 06:35
采纳率: 100%
浏览 1.6k

下面Java的AES加密,用c#怎么实现?要一样的结果哦

public static String encrypt(String content, String key) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] byteRresult = cipher.doFinal(byteContent);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteRresult.length; i++) {
String hex = Integer.toHexString(byteRresult[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • xiekuntarena
    月明星稀灬 2016-11-30 09:26
    已采纳

    或者更方便的,做一个JAVA-WebService接口,每次由C#通过http请求调用进行加密得出密文。

    点赞 评论
  • robin8624
    robin8624 2016-11-30 09:09

    你那个KEY每次都变,结果能一样吗?!

    点赞 评论
  • xiekuntarena
    月明星稀灬 2016-11-30 09:27

    用接口啊,JNI技术,不必用C#实现,可以用JAVA实现,然后用C#调用。

    点赞 评论

相关推荐