下面Java的AES加密,用c#怎么实现?要一样的结果哦 5
publicstaticStringencrypt(Stringcontent,StringsKey){SecretKeysecretKey=null;KeyGenera...
public static String encrypt(String content, String sKey) {
SecretKey secretKey = null;
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(sKey.getBytes());
kgen.init(128, secureRandom);
secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
return ByteUtil.parseByte2HexStr(result);
} 展开
SecretKey secretKey = null;
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(sKey.getBytes());
kgen.init(128, secureRandom);
secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
return ByteUtil.parseByte2HexStr(result);
} 展开
追问
怎样实现不是重点,
重点是要各上面的JAVA加密输出一样的结果,java传进去的key经过一大堆处理的。
某彩票的接口是用java写的,我们是用c#来调用,这样就出现了aes加密结果不一致的问题
追答
对java不太懂,不好意思,不过重点应该在这一句 SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询