我刚开始学习golang,以下是java实现的aes加密和解密,如何使用Golang来实现?
public static byte[] encrypt(String clear, String raw) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(raw.getBytes());
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
//System.out.println(Base64.encodeBase64(skey.getEncoded()));
SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES");
//System.out.println();
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
return cipher.doFinal(clear.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] decrypt(byte[] encrypted, String raw) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(raw.getBytes());
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
return cipher.doFinal(encrypted);
} catch (Exception e) {
e.printStackTrace(); }
return null;
}