radius pap 加/解密算法实现-golang

pap 认证

pap: Password Authentication Protocol 口令认证协议
PAP认证过程非常简单,二次握手机制,使用明文格式发送用户名和密码。 相对于chap, 没有增加随机数加密,可以无限次尝试。 不如chap安全
上述的明文传递密码, 指的是 认证客户端 —> 认证服务端 交互时, 但是在radius协议中, 认证终端(比如办公pc) 与 认证服务器(比如radius服务器)之间, 还存在NAS设备(比如交换机)。
NAS与radius服务器的交互,并不是明文传递密码 !!! 具体可参考 RFC2865

pap 加/解密 golang 实现

算法原理

S: 共享密钥
RA: 认证请求码, 即字段 authenticator
算法实现关键是: 按每16字节均分, 进行异或位运算 (不足16字节的补齐 0)

基于golang 实现

注意: 为屏蔽隐私信息,博客代码上传时, SharKey变量 已经替换

package main
import("fmt""crypto/md5""encoding/hex"
)const (size     int = 16
)//SharKey 为交换机配置与radius服务器使用的共享密钥,使用时根据实际配置决定
var SharKey = []byte{'*','*', '*', '*', '*', '*', '*', '*', '*', '*'}var AllZero = [size]byte{}//pap 加密
/*RA: 认证请求码, 即字段 authenticatorpassword: 待加密的明文密码
return:加密的密文密码error
*/
func encodePap(RA []byte, password []byte ) ([]byte, error) {b0Byte := append(SharKey, RA...)b0 := md5Sum(b0Byte)remainderCount := size - len(password) % sizeif remainderCount == size {remainderCount = 0}password = append(password, AllZero[0:remainderCount]...)bi := make([]byte, 0)res := make([]byte, 0)for i := 0; i < len(password) / size; i ++ {//fmt.Println("here:", i, len(password), remainderCount)btmp := make([]byte, 0)if i == 0 {for j :=0 ; j < size; j++ {tmp := password[i*size + j]  ^  b0[j]res = append(res, tmp)btmp = append(btmp, tmp)}}if i != 0 {for j :=0 ; j < size; j++ {tmp := password[i*size + j]  ^  bi[j]res = append(res, tmp)btmp = append(btmp, tmp)}}biByte := append(SharKey, btmp...)bi = make([]byte, 0)bi = md5Sum(biByte)}return res, nil
}//pap 解密
/*
params:RA: 认证请求码, 即字段 authenticatorpassword: 已加密的密文密码
return:解密的明文密码error
*/
func decodePap(RA []byte, password []byte) ([]byte, error) {b0Byte := append(SharKey, RA...)b0 := md5Sum(b0Byte)remainderCount := size - len(password) % sizeif remainderCount == size {remainderCount = 0}password = append(password, AllZero[0:remainderCount]...)bi := make([]byte, 0)res := make([]byte, 0)for i := 0; i < len(password) / size; i ++ {//fmt.Println("here:", i, len(password), remainderCount)btmp := make([]byte, 0)if i == 0 {for j :=0 ; j < size; j++ {tmp := password[i*size + j]  ^  b0[j]res = append(res, tmp)btmp = append(btmp, tmp)}}if i != 0 {for j :=0 ; j < size; j++ {tmp := password[i*size + j]  ^  bi[j]res = append(res, tmp)btmp = append(btmp, tmp)}}biByte := append(SharKey, password[i*size : i*size + size]...)bi = make([]byte, 0)bi = md5Sum(biByte)}return res, nil
}func md5Sum( item []byte) []byte  {h := md5.New()h.Write(item)return h.Sum(nil)
}func main(){ra :=[]byte{0x60, 0x33, 0x21, 0x55, 0x60, 0xb3, 0x61, 0x70, 0xa7, 0x2d, 0xf3, 0x32, 0xc2, 0x64, 0x17, 0x8e}var cryptoPass = []byte{0x62, 0x7d, 0x3e, 0xa0, 0x68, 0x80, 0xca, 0x32, 0x31, 0x29, 0x22, 0x63, 0x2a, 0x7b, 0xb0, 0x8e}var pass = []byte{'p','a', 's', 's'}enpass, err := encodePap(ra, pass)fmt.Println(err)fmt.Println(   hex.EncodeToString(enpass)  )depass, err := decodePap(ra, cryptoPass)fmt.Println(err)fmt.Println(  string(depass)  )ra2 :=[]byte{0xfc, 0x55, 0x6b, 0xde, 0x54, 0xad, 0xc9, 0x0f, 0xa5, 0x76, 0x8a, 0xa2, 0xaa, 0xdc, 0x11, 0x8d}var cryptoPass2 = []byte{0x2c, 0x96, 0x62, 0xc8, 0xce, 0xa8, 0x7c, 0xbd, 0x84, 0x2b, 0xbb, 0x2d, 0xeb, 0x43, 0x3c, 0xfe,0x70, 0x0e, 0xb5, 0x5e, 0xbd, 0x3e, 0x6e, 0xe7, 0x52, 0xbf, 0x5d, 0x64, 0x69, 0x24, 0xf8, 0xe5}var pass2 = []byte{'1', '2', '3', '4', '5', '6', '7', '8', '1', '2', '3', '4', '5', '6', '7', '8', '1', '2', '3', '4', '5', '6'}//2c 96 62 c8 ce a8 7c bd 84 2b bb 2d eb 43 3c fe 70 0e b5 5e bd 3e 6e e7 52 bf 5d 64 69 24 f8 e5enpass2, err := encodePap(ra2, pass2)fmt.Println(err)fmt.Println(   hex.EncodeToString(enpass2)  )depass2, err := decodePap(ra2, cryptoPass2)fmt.Println(err)fmt.Println(   string(depass2)  )ra3 :=[]byte{0x02, 0x1f, 0xd3, 0xfe, 0x10, 0x77, 0xd4, 0xf4, 0x77, 0xd3, 0x5c, 0x08, 0x59, 0xda, 0x19, 0x27}var cryptoPass3 = []byte{0xcc, 0xfd, 0x4d, 0xa0, 0x9d, 0x82, 0xa8, 0xd5, 0xfd, 0xb6, 0x68, 0xf4, 0x71, 0x93, 0x05, 0x79}var pass3 = []byte{'1', '2', '3', '4', '5', '6', '7', '8', '9'}enpass3, err := encodePap(ra3, pass3)fmt.Println(err)fmt.Println(   hex.EncodeToString(enpass3)  )depass3, err := decodePap(ra3, cryptoPass3)fmt.Println(err)fmt.Println(  string(depass3)  )
}

运行实例

radius 服务器抓包如下:(密码大于 16 字节)

代码运行结果(代码运行了多组密码的加解密):

wireshark 抓包结果, 与代码运算结果一致。 代码符合算法加解密要求

参考文档

(1) RFC 2865
(2) http://t.zoukankan.com/voipman-p-5345320.html
(3) https://praneethwifi.in/2021/07/30/radius-pap-authentication-radius-frames-verification-with-wireshark/?msclkid=617ac92ebc8611ec9a92d47c4ef0f2a2

radius pap 加/解密算法实现-golang相关推荐

  1. .Net/C# 实现: FlashFXP 地址簿中站点密码的加解密算法

    参阅 CCFer & TLFer : kanbol 的 Java 代码翻译修改而成: kanbol 说: 之前在TLF写过一个程序自动更新flashfxp的地址簿,也就是修改sites.dat ...

  2. (转)base64编码(严格说来,base64不算作加解密算法)

    [README] 1.本文转自: Java base64加密解密 - xuwc - 博客园参考: https://www.cnblogs.com/luguo3000/p/3940197.html ht ...

  3. C语言实现DES加解密算法

    C语言实现DES加解密算法 DES加解密 DES加解密 #include <stdio.h> #include <stdlib.h> #include <string.h ...

  4. C语言实现AES加解密算法

    C语言实现AES加解密算法 AES加解密 AES加解密 #include <stdio.h> #include <stdint.h> #include <memory.h ...

  5. C语言实现TEA系列加解密算法

    C语言实现TEA系列加解密算法 TEA加解密 XTEA加解密 XXTEA加解密 TEA加解密 #include <stdio.h> #include <stdint.h>//加 ...

  6. KP-ABE基于属性的加密加解密算法及Access Tree构建

    注:本文加解密算法构建完全参考经典ABE paper: Attribute-Based Encryption for Fine-Grained Access Control of Encrypted ...

  7. js des加密 java_Java实现与JS相同的Des加解密算法完整实例

    本文实例讲述了Java实现与JS相同的Des加解密算法.分享给大家供大家参考,具体如下: 这里演示java与js实现相同的des加解密算法,不多说,不废话,直接上代码 一.java实现 package ...

  8. Python中的AES加解密算法

    AES加密的参数及其条件:这个AES加密的主要坑就在于这些条件,首先AES加密有几个参数 秘钥:加密的时候用秘钥,解密的时候需要同样的秘钥才能解出来 明文:需要加密的内容 模式:aes 加密常用的有E ...

  9. python中凯撒密码_python实现凯撒密码、凯撒加解密算法

    凯撒密码的原理:计算并输出偏移量为3的凯撒密码的结果 注意:密文是大写字母,在变换加密之前把明文字母都替换为大写字母 def casar(message): # *************begin* ...

最新文章

  1. 优点和阵列的缺点,并且一个链表
  2. 高性能mysql的事物隔离级别
  3. 在GridView中的批量删除!
  4. visualstudio2015无法打开包括文件stdio.h等
  5. 会议中的Meeting App
  6. Redis分布式锁抽丝剥茧
  7. 使用ASP.NET Core 3.x 构建 RESTful API - 4.1 面向外部的Model
  8. eclipse中ctrl+h默认打开是JavaSearch,怎么设置成默认打开是FileSearch
  9. React Router V6 新特性
  10. 远程访问dmz和虚拟服务器的设置
  11. h5 监听浏览器被切换到后台或者手机锁屏再次唤起事件
  12. iOS - OC 基本语法
  13. SQL中的Having与Where的区别(面试常问)
  14. 自控专业工程设计用标准及规范
  15. Python安装pygame教程
  16. 【资料】分享北京某培训机构全部学习课程加个人的一些学习上的建议
  17. 吕布机器人评测_999元的吕布机器人到底怎么样?听听玩家们怎么说
  18. matlab将z域变为s域,时域、S域、Z域转换
  19. ESP8266 WIFI kill 2021版教程(小白0基础)
  20. php 获取照片信息,PHP读取照片信息

热门文章

  1. ppt文字提取转word
  2. 怎么破解电脑系统管理员密码?黑客用cmd批处理命令
  3. 所谓的特征值和特征向量
  4. JS导出Word细节设置
  5. 软件开发之版本控制方式
  6. webpack css-loader style-loader scss-loader cssloader模块化
  7. 有利可图的NFT,NA公链(Nirvana Chain)NAC公链怎么面对高额Gas费的?
  8. 若依@Excel注解自动获取导出字段,字典解析
  9. Microsoft Powershell 介绍
  10. SAR学习笔记后续-phased工具箱介绍